Find Command in Linux

FREE Online Courses: Knowledge Awaits – Click for Free Access!

In this article, you will learn what the find command in Linux is and the different options used with the find command. You will also learn how to use these options to filter out the results you desire.

What is the find command in Linux?

The find command is a simple yet powerful tool that helps you find a particular file within a directory. You might mistake the find command for doing a mere job of just finding a file provided the file name, well it’s more than that. The find command finds files based on different criteria like permissions, user ownership, modification, size, date/time, and more.

The find command can be made even more powerful by combining it with other commands like grep and sed using a pipe symbol (a function that passes in the output of 1 function as the input of the other.)

The find command searches for files and directories in a directory hierarchy based on a user-given expression and can perform user-specified action on each matched file.

In a nutshell, The find command is a powerful command-line utility that finds files and directories that meets certain criteria. We can even perform certain actions on the results we get back.

Why use Linux find command?

Say you created a file yesterday and forgot the exact location of where you created it, say you even forgot what format you even saved it as. Find command comes to the rescue in situations like these. You can even directly do actions once you find your desired file or directory.

Syntax of Find command in Linux

The find tool is already preinstalled in most of the distributions of Linux, which means you can directly start giving your commands using the syntax given below:

find <option> <path> <expression> <what to search>

1. The options field is where you enter different options which control the treatment of symbolic links, debugging options, and optimization methods.

2. The path field is where you specify the starting directory or the directories where the find command will search the files.

3. The expression filed is where you enter made-up options, search patterns, and actions separating them by operators.

Options used with Linux find command

Before we look into examples, let us understand the different options that are used with the find command.

1. -exec <command>

This option returns 0 for its exit status upon successful command execution of the file being searched which meets the given criteria.

2. -ok <command>

This option world the same as the ‘-exec’ command, except the user is prompted first.

3. -inum <numb>

This option searches for the files with the inode number you specify. Inode number is a unique number that exists for all the files in Linux and UNIX type systems.

4. -links <number>

This option searches for files with the number of links you have specified

5. -name

This option searches for the files that are specified by the name you give.

6. -iname

This option ignores the case and searches for the files that are specified by the name you give.

7. -newer file

This option searches for the files that were modified or created after the file Whose name you have entered.

8. -perm

This option searches for the file based on the permissions you have entered. you must note that the permission you entered is in the octal form.

9. -print

This option displays the pathname of the files which is found by using the rest of the criteria you have specified.

10. -empty

This option searches for empty files and directories

11. -user <name>

This option searches for files owned by the user name or ID with the name you have specified.

12. -size <n>

This option searches for a file of ‘n’ blocks. Blocks mean the size of the file for example 100m represents 100 MB. The number followed by C can also be used to measure the size in characters.

13. -size <+n>

This option searches for a file of size greater than ‘n’ blocks. for example, +100M, will search for files that are larger than hundred megabytes.

14. -size <+n>

This option searches for a file of size greater than ‘n’ blocks. for example, +100M, will search for files that are larger than hundred megabytes. K means Kilobytes, G means Gigabytes, and so on.

15. \<criteria\>

This option will be true if the criteria you enter are true. this command is used for grouping criteria combined with logical or, or logical and.

16. !<criteria>

This option will be true if the criteria you enter are false. this command is used for grouping criteria combined with logical or, or logical and.

Examples of the Linux find command

Now that you have understood what the find command is used for and have also seen its syntax and options let us look at some examples using the find command in Linux.

How to find files using file names?

Let us see some examples of how to find files just using file names.

1. Searching in the current directory

find . -name dataflair.txt

This command will search the .txt file with the name ‘dataflair’ in the current directory as “.” represents “current directory”

2. Searching in the home directory

find /home -name dataflair.txt

This command will search the .txt file with the name ‘dataflair’ in the home directory. Note that the “/” represents the root directory.

3. Searching by ignoring case

find /home -iname dataflair.txt

This command will search the .txt file with the name ‘dataflair’ in the home directory by ignoring the case of the name we have entered. Hence using the flag “i” we can search files like DataFlait.txt, DATAFLAIR.txt, and so on.

4. Searching directories by name

find /home d -name dataflair

This command will search the directory with the name ‘dataflair’ in the home directory as we have entered the flag “d”.

4. Searching for all the formats of the file

find /home -name dataflair.*

Since we used a wild card, this command will search all formats of the file named dataflair, hence we can match files like dataflair.php, dataflair.c, dataflair.sh, dataflair.txt, and many more.

How to search files based on permissions?

We can also search files based on their permissions by giving their octal form. Here is a quick revision of the table:

Binary NotationOctal NotationPermissionmeaning
0000No permission
0011–xOnly execute
0102-w-Only write
0113-wxWrite and execute
1004r–Only read
1015r-xRead and execute
1106rw-Read and write
1117rwxRead write and execute

If you want to get a better understanding of permissions, check out my article on chmod.

1. Finding file with permission 777

find / -type f -perm 0777 -print

This command will find the files with permission 777 a k a with permissions of “rwxrwxrwx”

2. Finding file without permission 641

find / -type f ! -perm 777 -print

This command will find all the files without the permission 644 a k a without the permissions of “rw-r—-x”

3. Finding only readable files

find / -type f -perm 400-print

This command will find all the files with the permission 400 a k a without the permissions of “r——–”. Where only the user can read the file.

4. Finding only executable files

find / -perm / a=x

This command will find all the files with the permission of only executing for all the users (owner, group, and others)

5. Finding file with permission 777 and replacing it with 700

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

This command will find the files with the permission 777 and then executes chmod (change mode) and replaces the permission to 700.

6. Finding all empty directories

find /tmp -type d -empty

This command will find all empty directories under a certain path

7. Finding all empty files

find /tmp -type f -empty

This command will find all empty directories under a certain path

8. Finding and removing a single file

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

This command will find a single text file called “dataflair” and remove it.

9. Finding and removing multiple files

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

This command will find all the text files in the specified directory and remove them.

10. Finding all hidden files

Find /tmp -type f -name “.*”

This command will find all the hidden files.

How to Search files based on date and time?

1. Finding files last modified 30 days ago

find / -mtime 30

This command will find all the files that were modified 30 days back.

2. Finding files last accessed 15 days ago

find / -atime 15

This command will find all the files that were accessed 15days back.

3. Finding files last modified 15 to 30 days ago

find / -mtime +15 -mtime -30

This command will find all the files that were modified more than 15days back (+15) and less than 30days back (-30).

4. Finding files changed in the last hour

find / -cmin - 60

This command will find all the files that were changed in the last hour.

5. Finding files modified in the last hour

find / -mmin - 60

This command will find all the files that were modified in the last hour.

6. Finding files accessed in the last hour

find / -amin - 60

This command will find all the files that were accessed in the last hour.

How to search files based on owner or group?

1. Finding a single file based on the user

find / -user root -name dataflair.txt

This command finds all or single files called ‘dataflair.txt’ under the” / root” directory of owner root.

2. Finding all files based on the user

find /home -user dataflair

This command finds all the files that belong to user dataflair under the “/home” directory.

3. Finding all files based on a group

find /home -group programmer

This command finds all the files belonging to the group “programmer” under the “/home” directory.

4. Finding particular files of a user

find /home -user dataflair -iname “*.txt”

This command finds all the .txt files of user “dataflair” under the “/home” directory.

How to search files based on file size?

1. Finding files with a size of 30MB

find / -size 30M

This command finds files with a size of 30 megabytes

2. Finding files between 30MB and 50MB

find / -size +30G -size -50G

This command finds files that are greater than 30 gigabytes (+30G) and less than 50 gigabytes (-50G)

3. Find and delete all 100MB files

find / -type f -size 100M -exec rm -f {}\;

This command finds all files that are of size 100 megabytes and deletes them.

4. Finding and deleting specific files

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

This command finds all the .mp3 files that are greater than 10 megabytes (+10) and deletes.

Summary

As you have seen, the find command is extremely useful to not only do a simple search for a file but also search for a file using many different criteria and in turn perform an action on it. You have seen the different examples of how to use the find command to its fullest, by searching based on date/time, Owner, size of the file, group, and name of the file.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

Your email address will not be published. Required fields are marked *