15.4 C
New York
Tuesday, September 19, 2023
HomeLinux50 Simple and Useful Examples of Find Command in Linux

50 Simple and Useful Examples of Find Command in Linux

One of the main reasons behind the global popularity enjoyed by Linux and BSD systems today is their default tools and utilities. Linux offers some of the best methods of searching and accessing files directly from the command line. The find command in Linux is one such utility that allows Linux users to search their system for particular types of files effectively. It is a command-line utility which allows powerful searching functionalities and can be a compelling tool in the hand of seasoned users. If you want to master the find command in detail and accelerate your Linux skills, you have come to the right place.

Everyday Examples of Find Command in Linux


We believe the best way to learn Linux commands is to try them out yourself. The commands shown below should work just fine on every Linux distributions despite your desktop environment. So, you can quickly try some find command in Linux by opening a terminal using Ctrl + T and copy-pasting the commands from here. Try to utilize these commands on your day to day computing as much as possible to master them in a short time.

Structure of the Find Command


The find command in Linux has the below structure.

find [paths] [expression] [actions]

Here paths denote the directory where you won’t “find” to search for the specified files. The expression will allow you to filter your files based on some criteria while actions will let you execute shell commands on files. The default action is print, which simply prints the files matched by expression in any path. Find shows the files recursively, meaning it will first traverse every directory and then print out the results accordingly.

Basic Linux Find commands


You can utilize some basic find commands to get a feel of what find can do.  These commands are pretty straightforward and don’t require any prior experience with other command-line utilities.

basic find command in Linux

1. List All Files Present Inside the Current Directory

$ find

This will print all the files present in the current directory. If the current directory contains additional directories, it will show them also. This command is equivalent to the command

$ find -print.

2. List All Files Present in a Specific Directory

$ find /folder

This command will print all the files present inside the directory /folder. You can use this command to list all the files of a specific path in your Linux system.

3. Search for a Specific File

$ find -name test.txt

This command searches for a file called test.txt inside your current directory and in every other sub-directory. Use this command when you are searching for specific files.

4. Search for a Specific File in a Directory

$ find /Docs -name test.txt

This command will search for the file named test.txt in the folder called /Docs. You can use both absolute paths and relative paths when using this command.

5. Search for a File in Multiple Directories

$ find /opt /usr /var -name foo.scala -type f

You can use the Linux find command when searching for a file in multiple directories at the same time. Simply put the directory names one after another followed by a space when searching.

6. Search for a File Ignoring Case

$ find -iname test.txt

This command will search for the file test.txt without matching the case. So if you’ve two files called test.txt and Test.txt, it will display both files. The -iname option allows the find command to do this.

7. Search for Folders Inside the Current Directory

$ find -type d

This command will list every directory you have under your current working directory. You can add the name option for listing out specific directories.

8. Search for A Specific Folder in a Directory

$ find /home -type d -name users

This command will search for a folder called users inside the /home directory. You can add the -iname option instead of -name to search without respect to the case.

9. Search for PHP Files Using Name

$ find -type f -name test.php

This command will list the PHP file called test.php present inside the current working directory.

10. Search for all PHP Files

$ find -type f -name “*.php”

This find command in Linux will print out every PHP file you have inside your current working directory. You can add the path option before the type for listing PHP files present only in a specific directory.

11. Find All Symbolic Links

$ find /usr -type l

The above command will search for every symbolic link you have inside your current directory and print them out accordingly.

12. Search for Files With Different Extensions

$ find . -type f \( -name “*cache” -o -name “*xml” -o -name “*html” \)

The above find command searches for files named cache with different extensions. You can make find search for additional extensions by adding the name option followed by the -o flag.

Find Files Based on Permission


The find command allows Linux users to search for files based on their permission status. This will come in handy when your system has multiple users and you need to make sure nobody gets unauthorized access to your data.

13. Find Files That Have 777 Permission Set

$ find -type f -perm 0777 -print

This command will list all the files in the current working directory that have their permission set to 777. These are the files that any user can read, write, and execute.

14. Find Files Without 777 Permissions

$ find / -type f ! -perm 777

This find command in Linux will only search for those files that have permissions set other than 777. You can replace / with any other location for narrowing down your search results.

15. Find SGID Files with 644 Permissions

$ find / -perm 2644

This find command will only look for SGID files which have their permission status set to 644. SGID files allow temporary access to files that you don’t own or have access to.

16. Find All Sticky Bit Files with 551 Permissions

$ find / -perm 1551

Sticky Bit files are set of files or folders that can be only renamed or deleted by only the user who created them or the root user. This command will show all Sticky Bit files in your system with 551 permissions.

17. Find All SUID Files

$ find / -perm /u=s

SUID files allow temporary ownership of a filegroup to users other than the owner of the filegroup or root user. This find command will list all of the SUID files you have in your current Linux machine.

18. Find All SGID Files

$ find / -perm /g=s

SGID files are similar to SUID files in many ways except that when files with SGID permissions are run, the execution takes place as if the original owner was running the process. This find command lists every single SGID files irrespective of their permission status.

19. Find Read-Only Files

$ find / -perm /u=r

Read-only files prohibit Linux users from writing to them or executing them. They can be only written or executed by the files owner or the root user. This find command will display all the read-only files your machine currently have.

20. Find All Executable Files

$ find / -perm /a=x

Executable files are simply files that can be executed such as binary files. The above Linux “find command” will search the system for every such file and list them accordingly.

21. Find All Files with 777 Permissions and chmod to 644

$ find / -type f -perm 0777 -print -exec chmod 644 {} \;

The above find command searches all files that have 777 permission associated with them and will change their permission status to 644 using the chmod command. Only you can now read or write to the files with 644 permission.

22. Find All Directories with 777 Permissions and chmod to 755

$ find / -type d -perm 777 -print -exec chmod 755 {} \;

This Linux find command will search for every directory that has 777 permission and will change their permission status to 755. What this does, in essence, is allow full permissions only for the owner and read and execute permission for other users.

Search Specific Files Using Find


Find can be used for searching specific files quite effectively. You can use find for selecting files based on some criteria and perform shell operations like file deletion on them.

find command in Linux for searching specific files

23. Find a File and Remove It

$ find -type f -name “test.txt” -exec rm -f {} \;

This Linux “find” command is used when you need to delete a file from a list of many files. In this case, it first finds the file called test.txt in the current directory and removes it using rm- f.

24. Find Multiple Files and Remove them at Once

$ find -type f -name “*.mp3” -exec rm -f {} \;

Find command is useful for searching large arrays of specific filetypes and removing them at once. The above command searches your system for .mp3 files and deletes them without any prompt. You can add the interactive flag -i with the rm part for getting a prompt each time a deletion occurs.

25. Find All Empty Files in The System

$ find /tmp -type f -empty

Empty files can hog up your system resources in a very short time. Use the above command to list all the empty files using the find command. You can delete these files by adding -exec rm -f {} \; just like the above command.

26. Find All Empty Folders in The System

$ find /tmp -type d -empty

This command will list all empty folders residing inside the /tmp directory. You can use this to find empty folders in any other directory and can also delete them if you want like you did earlier.

27. Find All Hidden Folders in The System

$ find /home -type f -name “.*”

Hidden folders are usually prefixed by a single dot(.) in Linux systems. Use the above command to list out all the hidden folders you have inside your /home directory.

Find Files Based on User


Find command is also useful for searching files based on user groups. You can search for specific files for certain user groups and modify file permissions very quickly using Linux find commands.

28. Find A File that Belongs to User

$ find / -user root -name test.txt

You can use the find command in Linux for searching a single file owned by a specific user. The above command searches for a file called test.txt in the / directory that belongs to the user root.

29. Find All Files that Belongs to User

$ find /home -user username

The above Linux command searches for all files in the /home directory that belongs to the user “username”. You will need to replace “username” with your Linux username for finding all the files that belong to you.

30. Find All Files that Belongs to a Group

$ find /home -group programmer

Linux files usually belong to some groups. The above Linux command allows you to search for all the files that belong to a particular group called “programmer” and prints them in the terminal. Replace “programmer” with the group name you want to search for.

31. Find Specific Files for a User

$ find /home -user bob -iname “*.txt”

You can use the find command to search for specific files that belong to a user. The above command does this and lists out all the .txt files which belong to the user bob. Replace bob with your username and .txt with any other filetypes for finding files of a certain type that belong to you.

Find Files Based on Time


Find also allows sysadmins to monitor their system effectively. It allows searching for files based on modification time, access time, and so on.

32. Find All Files That Have Been Modified in Last 50 Days

$ find / -mtime 50

The find command allows users to search for files that have been modified within a given time. The above command will print out all the files in your system, which have been modified 50 days back.

33. Find All Files That Have Been Accessed in Last 50 Days

$ find / -atime 50

The -atime option shows the files that been accessed within a defined timeframe. The above command lists all the files of your system that have been accessed 50 days back.

34. Find All Files Modified in the Last 50-100 Days

$ find / -mtime +50 –mtime -100

The find command in Linux allows users to search for all the files modified in a given range of time. The + and – operator is used in conjunction with -mtime for doing this. The above command finds all the files modified by you in the last 50 to 100 days.

35. Find All Files Changed in Last 1 Hour

$ find / -cmin -60

This command will find and list all the files that have been changed in the last hour. You can replace / with a specific directory for narrowing down your searches. Change 60 to any other number like 120 for finding files that have been changed in that time(2 hours for 120).

36. Find All Files Modified in Last 1 Hour

$ find / -mmin -60

The above command will show all the files that have been modified within the last 1 hour. Switch 60 to any other number for changing the timeframe required by your purpose.

37. Find All Files Accessed in Last 1 Hour

$ find / -amin -60

This command display all the files accessed by you in the last hour. As with the two preceding commands, feel free to change 60 for your desired outcome.

Find Files Based on Size


Sometimes you’ll need to search files based on their sizes. Find also comes in handy in this respect. You can add different options for searching files based on size more accurately.

38. Find Files of Size 50 MB

$ find / -size 50M

This “find command” in Linux prints out all the files you have over 50 MB of size. Replace / with your desired directory and 50M with any other size for narrowing down your search results more effectively.

39. Find All Files Over 100 MB

$ find / -size +100M

The above command will list out all the files you have over the 100 MB mark inside your / directory. You can change 100M with other file sizes for getting your desired result.

40. Find Files Between 50MB to 100MB

$ find / -size +50M -size -100M

Sometimes you’ll need to find files within a specified range of size. The above command will display all the files you have between the size 50MB to 100MB. Change the optional parameters for matching any specific search criteria.

41. Delete All Files Over 500 MB

$ find /Movies -size +500M -exec rm -rf {} \;

The find command is useful for searching files over a specific limit and deleting them instantly from the terminal. Suppose you’ve got some old movies lying around in a folder and want to delete them at one go. The above command will let you do precisely this. Make sure to replace /Movies with the folder name where your files reside.

42. Find Largest Files

$ find . -type f -exec ls -s {} \; | sort -n -r | head -5

The above find command will print out the 5 largest files you have in your current working directory and under its sub-directories.

43. Find Smallest Files

$ find . -type f -exec ls -s {} \; | sort -n | head -5

You can also use the find command in Linux to display the smallest files. This command prints out the 5 smallest files you’ve got under your current directory.

Miscellaneous Find Commands


The find command in Linux offers many additional capabilities like finding files based on the text they contain, searching and deleting files, finding files based on patterns, and so on. The below commands demonstrate some of these abilities in short.

44. Find and Delete Specific Files

$ find / -type f -name *.mp3 -size +10M -exec rm {} \;

This Linux “find command” enables users to find all .mp3 files in their system that occupy more than 10 MB space and delete them. You can replace .mp3 with any other filetype and the size parameter for specific types of files.

45. Find Files that Don’t Match a Pattern

$ find /home -type f -not -name “*.html”

The above find command in Linux will search for all the files in the /home directory that doesn’t end in .html. The -not option allows “find” to do this.

46. Find Files by Text inside The File

$ find . -type f -name “*.java” -exec grep -l StringBuffer {} \;

You can use grep to find files based on the text they contain. The above Linux “find command” searches for .java files that contain StringBuffer inside them. Adding the -i flag to grep will make this search to ignore case.

47. Find and Copy Files

$ find . -type f -name “*.mp3” -exec cp {} /home/MusicFiles \;

Find can be used for finding certain files and copying them to a new location. The above command finds all .mp3 files in the current directory and copies them to the folder /home/MusicFiles.

48. Find and Move Files

$ find . -type f -name “*.jpg” -exec cp {} /home/Pictures \;

Find can also be used for moving files effectively. The above command searches every .jpg file you have under your current directory and moves them to the directory /home/Pictures.

49. Find and Tar Files

$ find . -type f -name “*.java” | xargs tar cvf myfile.tar

You can use find to search for some specific files and archiving them into tarballs. The above command finds all .java files in the current directory and compresses them into a tar file called myfile.tar.

50. Filtering Error Messages

$ find [paths] [expression] [actions] 2>/dev/null

Sometimes you may face errors like ‘Permission Denied’ or something else while trying out some find commands. You can redirect these errors to /dev/null, as shown above.

Ending Thoughts


The find command in Linux is one of the most useful command-line tools you can use for effective monitoring of your system or frequent file processing. Smart implementations of various “find commands” will make you a Linux power user in no time. Our editors have tried their best to outline the most useful find commands for your day to day use. Utilize these commands in your everyday computing to get the most benefit out of your Linux system.

Mehedi Hasan
Mehedi Hasan
Mehedi Hasan is a passionate enthusiast for technology. He admires all things tech and loves to help others understand the fundamentals of Linux, servers, networking, and computer security in an understandable way without overwhelming beginners. His articles are carefully crafted with this goal in mind - making complex topics more accessible.

1 COMMENT

Comments are closed.

You May Like It!

Trending Now