Linux Administrator

Top 25 Linux Find Command Examples

Linux find command is a very useful command to search for file using command line. It is used to find files. Based on various search criteria like user ownership, permission, modification date and time, file size etc., Find command is available in most Linux distros by default. That’s why you do not need to install any package.

In this tutorial I will explain top 25 Linux Find command examples.

Suggested Read: Top 25 Linux/UNIX Commands Examples

Find Command Basic Syntax

find location comparison-criteria search-term

Find Command Examples

1. Listing All Files in Current Directories and Sub Directory

Using this command you can list all the files in the current directory and its sub directory.

# find
.
./xyx.txt
./subdirctory
./subdirecotry/howfile.php
./index.php

Same command as mention above.

# find .
# find . -print

2. Searching Specific Directory and Path

Follow the below command to search specific directory or path.

# find ./testing
./testing
./testing/xyz.txt
./testing/subdirectory
./testing/subdir/how.filephp
./testing/index.php

Below command will search for file by their name.

$ find ./testing -name "xyz.txt"
./testing/xyz.txt

You can also use wildcards.

# find ./test -name "*.php"

./testing/subdirectory/howfile.php
./testing/index.php

Ignoring the case

Some time you need to ignore the case when searching for file name.
Follow the below command to ignore the case. Just use “iname” instead of “name“.

# find ./test -iname "*.php"

./testing/subdirectory/howfile.php
./testing/index.php

3. Limit Depth of Directory Traversal

When we run find command it by default travels down the entire directory tree recursively, which take much time as expected and tome consuming. Using “maxdepth” option we will not go to more than 2 or 3 levels down in the sub directories.

# find ./testing -maxdepth 2 -name "*.php"
./testing/subdirtory/howfile.php
./testing/index.php

# find ./testing -maxdepth 1 -name *.php
./testing/index.php

As above in second example uses maxdepth of 1, which means it will not go lower than 1 level deep.

4. Inverting the Match

We can exclude the file from the search. It is also possible to search for file that do not match a given name or pattern.

# find ./testing -not -name "*.php"
./testing
./testing/xyz.txt
./testing/subdiretory

In above example we found all files that do not have .php extension.Find also support the exclamation mark in place of not.

# find ./testing ! -name "*.php"

5. Combine Multiple Search

You can use multiple criteria when specifying name and inverting.

# find ./testing -name 'abc*' ! -name '*.php'
./testing/xyz.txt
./testing/xyz

Above find command will look for files that begin with xyz in their names and leave the .php extension file.

OR Operator

# find -name '*.php' -o -name '*.txt'
./xyz.txt
./subdirectory/howfile.php
./xyz.php
./index.php

In the above command will search for files ending in either .php or .txt extension.

6. Searching Only Files and Directories

You can also search only Files and Directories using below commands.

# find ./testing -name xyz*
./testing/xyz.txt
./testing/xyz

Only Files

# find ./testing -type f -name "xyz*"
./testing/xyz.txt

Only Directories

# find ./testing -type d -name "xyz*"
./testing/xyz

7. Searching Multiple Directory Together

You can also search string in Two Directory, lets say you want to search inside 2 separate directories.

# find ./testing ./directory2 -type f -name "xyz*"
./testing/xyz.txt
./directory2/xyzdefg.txt

8. Finding Hidden Files

Hidden files begin with a “.” period in Linux. Using find command it is easy to find hidden files.

# find ~ -type f -name ".*"

9. Finding Files With Permissions

Find command is also used to find files with a specific permissions using the “perm” option.

# find . -type f -perm 0664
./xyz.txt
./subdirectory/howfile.php
./xyz.php
./index.php

In the above command searches for files with the permission 0664

10. Finding the Files With suid and sgid bit set

Following command will find all files with the permission 644 and sgid bit set.

# find / -perm 2644

Similarly you can use 1664 for sticky bit set.

11. Finding Read-Only Files

You can find all read-only files using below command.

# find /opt -maxdepth 1 -perm /u=r
/opt
/opt/thunderbird
/opt/brltty
/opt/dkms
/opt/phpmyadmin
... output truncated ...

12. Finding Executable Files

Following below command you can find all executable files.

# find /bin -maxdepth 2 -perm /a=x
/bin
/bin/preseed_command
/bin/mount
/bin/zfgrep
/bin/tempfile
... output truncated ...

13. Finding Files Belonging to Particular User

Find all single files which belongs to Sagar user.

# find . -user sagar
.
./xyz.txt
./xyz
./subdirectory
./subdirectory/howfile.php
./index.php

We can also find the file name along with user criteria,

# find . -user sagar -name '*.php'

14. Searching Files Belongs to Particular Group

You can also find files that belongs to a particular group.

# find /var/www/html -group dev_group

15. Finding Files Modified N Days Back

You can find all the files which are modified 20 Days back.

# find / -mtime 20

16. Finding Files Accessed in Last N Days

You can also find the files that were accessed in the last 20 Days.

# find / -atime 20

17. Finding Files Modified in a Range of Days

Find all files that were modified between 20 to 50 days ago.

# find / -mtime +20 –mtime -50

18. Finding Files Changed In Last N Minutes

Find files modified within the last 30 Minute.

# find /home/sagar -cmin -30

19. Finding Files Which Modified in Last 30 Minutes

Find all the file which are modified in last 30 Minutes.

# find / -mmin -30

20. Finding Files Which Accessed in Last 1 Hour

Find all the Files which are accessed in last 1 hour.

# find / -amin -60

21. Finding Files of Given Size

You can find all 1MB files.

# find / -size 1M

22. Finding Files in Size Range

You can also find the files which is greater than 10MB and less than 50MB.

# find / -size +10M -size -50M

23. Find The Smallest and Largest Files

Using following command you can find 8 largest files in the current directory and its sub-directories using sort and head command.

For largest files

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

For smallest files

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

Similarly we can shows the smallest files.

24. Finding Empty Files and Directories

Follow the below command to find the empty files and directories using with “empty” options.

For Empty Files:

# find /tmp -type f -empty

For Empty Directories:

# find ~/ -type d -empty

25. Listing The All Found Files

Using ls command we can list all files of find’s command output .

# find . -exec ls -ld {} \;
drwxrwxr-x 4 dev dev 4096 Aug 11 19:01 .
-rw-rw-r-- 1 dev dev 0 Aug 11 16:25 ./xyz.txt
drwxrwxr-x 2 dev dev 4096 Aug 11 16:48 ./xyz
drwxrwxr-x 2 dev dev 4096 Aug 11 16:26 ./subdirectory
-rw-rw-r-- 1 dev dev 0 Aug 11 16:26 ./subdirectory/howfile.php
-rw-rw-r-- 1 dev dev 29 Aug 11 19:13 ./xyz.php
-rw-rw-r-- 1 dev dev 0 Aug 11 16:25 ./index.php

Deleting All Matching Files and Directories

Using following command you can delete all php files from /tmp directory.

# find /tmp -type f -name "*.php" -exec rm -f {} \;

You can also delete files larger than 50MB.

# find /home/sagar/directory -type f -name *.php -size +50M -exec rm -f {} \;

I hope this article will help to find the files and directories in Linux and UNIX. If you have any queries and problem please comment in comment section.

Thanks:)

Thank you! for visiting LookLinux.

If you find this tutorial helpful please share with your friends to keep it alive. For more helpful topic browse my website www.looklinux.com. To become an author at LookLinux Submit Article. Stay connected to Facebook.

About the author

mm

Santosh Prasad

Hi! I'm Santosh and I'm here to post some cool article for you. If you have any query and suggestion please comment in comment section.

Leave a Comment