Linux find command with examples

Linux find command with examples

In this article, we will see Linux find command with examples.

Find Command is one of the most important and frequently used command command-line utility in Unix-like operating systems. Linux find command is used to find list of files and directories.

Linux find commands conditions are like permissions, users, groups, file type, date, size, and other possible criteria.

Linux find command syntax:

find [-H] [-L] [-P] [-D debugopts] [-Olevel] [starting-point...] [ex‐ pression]

Linux find command examples:

1. Find file using -name in current working directory.

find . -name ls.txt

Where .(dot means current directory).

The above command checks the file current directory and its sub directory.

Linux find command with examples

2. Find files using -name in current working directory without case sensitive.
We have to add i flag with -name as shown below. i means ignore case

find . -iname ls.txt

Output:

./training/ls.txt ./ls.txt ./LS.txt ./scripts/ls.txt

3. Find files under user home directory.

find ~ -iname ls.txt

Linux find command with examples

4. Find directory using name.

To find directories in Linux, we have to use -type and d option.

find -type d -name training

5. Find c file with in current directory

find . -type f -name switch.c

The above command finds switch.c in current directory and its sub directories as well.

6. Find all python files in current directory.

find . -type f -name "*.py"

7. Find all empty files in current directory.

find . -type f -empty

8. Find all empty directories in /home/r2schools directory.

find /home/r2schools -type d -empty

9. Find files with 777 permissions in Linux.

find . -type f -perm 777

10. Find files without 777 permissions in Linux.

find . -type f ! -perm 777

11. Find Files with 777 Permissions and chmod to 644.

Find all 777 permission files and use chmod command to set permissions to 644.

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

Where ‘r’ means read only files.

12. Find Directories with 777 Permissions and Chmod to 744

find . -type d -perm 777 -print -exec chmod 744 {} \;

13. Find and remove a file using Linux find command.

find . -type f -name "LS.txt" -exec rm -f {} \;

14. Find all files belongs to a user.

To find all files that belong to user r2shcools under /home/ directory.

find /home -user r2schools

15. Find files in multiple directories.

find /home/r2schools /home/r2schools/training -name "ls.txt"

16. Find all file types other than the mentioned type.

Here, we search all files except .txt files.

find . -not -name "*.txt"

17. Find all read only files.
Following query searches read only files in root directory and its sub directories as well.

find /root -perm /u=r

18 . Find all executable files.

Following query searches executable files in current and its sub directories as well.

find . -perm /a=x

19. Find the files size is greater than 50M.

If we want want to search a file for which we know the size is greater than the certain size, then we can use ‘-size‘ option.

find . -size +50M

20. Find the file with exact size.

If we want want to search a file for which we know the exact size, then we can use ‘-size’ option.

find . -size 50M

21. Find the files size is less than 50M.

If we want want to search a file for which we know the size is less than the certain size, then we can use ‘-size‘ option.

find . -size -50M

Simply remember with size option of find command to know the size of a file.

-size +(number)M means greater than the certain size.
-size -(number)M means less than the certain size.
-size (number)M no symbol means exact size.

22. Find files modified N days ago.

For example, we want to locate all the files that have been modified 10 days ago. We can accomplish that using -mtimeoption in find command

Following query checks in current working directory(.)

find . -mtime 10

Following query checks in entire Linux file system(/)

find / -mtime 10

23. Find files that have been accessed N days ago.

Similarly like above example, we can also locate files that have been accessed 10 days ago using -atime

Following query checks in current working directory(.)

find . -atime 10

Following query checks in entire Linux file system(/)

find / -atime 10

24. Find Modified Files in Last 1 Hour

find / -mmin -60

-atime means access time.
-mtime means modified time.
-cmin means change time in minutes.
-mmin means modified time in minutes.
-amin means access time.

25. Find largest and smallest files.

To list largest or smallest file, we will combine sort command with find command & if we further want to list top 5 of those largest files, we will combine head command.

To list top 5 files in the current directory, command is

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

To list top 5 files in the entire Linux file system, command is

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

To list small top 5 files in the current directory, command is

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

To list small top 5 files in the entire Linux file system, command is

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

26. Find all the files matching a criteria & delete them.

We might be required to locate and delete files matching a criteria. To do this with find command run the following command.

Notice: once removed a file cant be done undo. Please very careful to remove any file.

Following query finds the matching criteria and prints the matching result then it removes them.

find . -type f -name '*r2schools.*' -exec ls -l {} \; -exec rm -f {} \;

27. Find all the files size greater than 100M in current working directory and delete them.

find . -type f -size +100M -exec ls -l {} \; -exec rm -f {} \;

28. Find Specific Files and Delete

Find all .mp3 files with more than 25MB and delete them using one single command.

Following query finds all mp3 files in current working directory whose size is greater than 25MB and lists them on the terminal then deletes them.

find . -type f -name '*.mp3' -size +25M -exec ls -l {} \; -exec rm {} \;

If you want check in entire File System replace .(dot) with /

find / -type f -name '*.mp3' -size +25M -exec ls -l {} \; -exec rm {} \;

29. Find the manual of find command using

man find

So, we have covered as many examples as possible for Linux find command.