Finding files using ‘find’ command in Linux

Here’s a list of most useful linux “find files” commands i have summarized recently.

Finding files with filename

Find file by exact name
$ find / -name 'script.php'

Find file by name starting with ‘index’ (case-sensitive)
$ find /home/arvind -name 'index*'

Find file by name starting with ‘index’ (no case ; note the use of ‘i’ with -name. “-name” means consider only the file name in the ‘find’ command)
$ find /home/arvind -iname 'index*'

Find files within a given directory ; no directory parameter specified
$ find -name index*

Searching by Access or Modification times

Find files that are present in the directory /home/arvind and its subdirectoires which end in .php and which have been accessed in the last 10 minutes
$ find /home/arvind -amin -10 -name '*.php'
Find files that are present in the directory /home/arvind and its subdirectoires which end in .php and which have been accessed in the last 10 hours
$ find /home/arvind -atime -10 -name '*.php'
Find files that are present in the directory /home/arvind and its subdirectoires which end in .php and which have been modified in the last 10 minutes
$ find /home/arvind -mmin -10 -name '*.php'
Find files that are present in the directory /home/arvind and its subdirectoires which end in .php and which have been modified in the last 10 hours
$ find /home/arvind -mtime -10 -name '*.php'

File Size related searches

Find within a directory called /home/arvind, only those avi files that have a size less than 50000 Kilobytes ( < 50MB)
$ find /home/arvind -name '*.avi' -size -50000k
Find from the / directory for any file that is larger than 5000k (> 5MB)
$ find / -size +5000k

Extending search with and, or and/or negation(!) operators

Find files that have their names beginning with ‘mango’ and whose size is greater than 5000 kilobytes (> 5 MB).
$ find -name 'mango*' -and -size +5000k
Find files that are greater than 5MB, but they should not have ‘mango’ as the starting of their filenames
$ find /home/arvind -size +5000k ! -name "mango*"
Find files that begin with ‘mango’ in their names or all the files that are greater than 5 MB in size
$ find /home/arvind -name 'mango*' -or -size +5000k

Skipping mounted file systems from a search

Find files starting with the letters ‘star’ in their filenames. The only difference is that the mounted filesystems would not be searched for this time
$ find / -mount -name 'star*'

Taking actions on searched files

Alright, you command the system to look for files it would list them to you with full path. However you can command the system to fetch you more file details with the use of -exec paramater. For example using ls -l with a find command so you could other file details like filesize, user group/permissions etc. The words following the -exec option is the command that you want to execute i.e. ls -l here. {} is an indicator that the filenames returned should be substituted here. \ before ; indicates the end of the -exec command. The following command finds all the files on your system that start with ‘mango’ and would then execute the ls -l command on searched files.
$ find / - name 'orange*' -exec ls -l {} \;

Option to log error messages

You can log errors returned by find operation into a file or even you can can simply discard these messages. Like the following.

Find file by exact name and discard all error messages
$ find / -name 'script.php' 2>/dev/null

Find file by exact name and log error messages to find_errors.log file
$ find / -name 'script.php' 2>/home/arvind/find_errors.log

One thought on “Finding files using ‘find’ command in Linux

  1. I used to be very happy to search out this net-site.I needed to thanks to your time for this excellent read!! I positively having fun with every little bit of it and I’ve you bookmarked to check out new stuff you blog post.

Leave a Reply

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