Top 10 Linux find command examples for searching files

If you work on UNIX or Linux, the find command is a powerful tool that allows us to quickly and easily scan through our file system to find files and directories that meet a certain criteria.

We can even perform actions on those results that we get back such as list, sort, delete and apply other custom Linux commands.

The find command is an excellent tool in your daily arsenal for Linux or Mac systems and will give you greater control of the command line search.


Lets look at common find command examples :

1. Find command to list all the files in a directory and its sub directories.

To begin with, lets try searching for all images with .png extension in our current directory.

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

Here we are simply trying to search for png image files in the current directory.

  • -name is used to specify a pattern with the name of the file
  • -type is used to search for just files (and not directories option -d)

Lets see it in action

As you can see it lists all the files with the extension .png

A thing to note about the find command is that by default it searches the current directory and its sub directories by default.

Also, if the command syntax is correct there is no output shown which is normal behaviour and it means that the command ran successfully.

2. Find command to list all large or small files.

Say you want to search for files over a specific file size. In order to search for such files we use the -size option in the command to specify the size in either kilobytes or megabytes as follows:

find . -type f -size +100k

Here we have specified that the file to be searched for in the current & sub-directories to be over atleast 100 KB in size or more.

You can substitute ‘M‘ in place of ‘‘k’ in the above command to change the file size to megabytes with an appropriate number before it. Notice its a capital case ‘M’.

Lets see it in action

  • -exec option is to specify a custom Linux command which is applied to the results returned from the search.
  • “ls -lh”  used to list the files found in the long form listing with their size. This is a regular Linux command.

The same -exec option can be used to remove files as well if required. Lets see exactly that.

3. Find command to remove/delete all the files in a directory and sub directory.

Now suppose you want to delete all the files with a .png extension.  The find command can be modified to our needs as below.

find . -name "*.png" -type f  -exec rm -rf {} \;

After finds returns with some files, delete the files using the command “rm -rf” from the file system. Here ‘{}’ denotes all the files returned in the search. Note that the ‘\;’ is used to signify the end of the command.

Lets look at a slight variation, say you want to delete all files starting with specific characters, have a look at the command below.

find . -name "flyer*" -type f -delete \;

The above command is a variation in which we are searching for all files which start with the word ‘flyer‘ and delete them if found.

Similarly, we can specify any file pattern for the -name parameter and have fine grained control over the results.

This is a list of files for demonstration purposes. After the find above  runs it should delete all the files above.

Therefore, as you can see by changing the pattern of the -name parameter we can narrow down the list of files to be deleted to our will.

CAUTION: Use -exec option carefully as you may delete unwanted files. Its always a good idea to use the find command without -exec option first to see a list of results before applying any removal, copy or cut commands via -exec.

In our case we use ls -lh first to check the files that are output and then use rm -rf to remove those very files.

5. Find command to remove all 0 size or empty files.

Alternatively, you can use the following syntax to remove files which are of size 0 using the Linux command rm which is used to remove files from the system.

find . -name '*.jpg' -size 0 -print0 | xargs -0 rm

In this case xargs is a special syntax which is used to define the arguments to the command rm. Furthermore, a bit about xargs which is a linux command :

  • It reads data from standard input (stdin) and executes the command (supplied to it as an argument) one or more times based on the input read.
  • Any blanks and spaces in the input are treated as delimiters, while blank lines are ignored.

4. Find command to remove all empty directories.

Say you have loads of directories on your system which are lying around empty. You can remove them from the system using find.

In order to search for such directories we use the -empty option in the command to specify them.

find /tmp/content/ -type d -empty -exec rmdir {} \;

In the above command we have type specified ‘d’ for directories.

We could have also used ‘rm -r’ in the -exec options instead of rmdir. In either case the directories from the result would be deleted from the system.

4. Find files / directories and copy them to a new location.

Next, if you want to find files in single or group of directories and copy them in one go to another location, the find command can come to your rescue. Lets see the below command.

find /source -name ‘*2011*.xml’ -exec cp {} /destination \;

In the above command I am searching for all files which have the year 2011 in the file names and copying them subsequently to the destination folder.

Notice that we are using the regular cp (copy) command in Linux to copy the resultant files to the new destination.

Hence, if it were not for this command you would have to go to each directory search and copy the files manually.

5. Find and rename the file extensions of a group of files.

Sometimes we may want to recursively change the extension of a large group of files to a new one. This can be easily done by find :

find . -name "*.asp" -exec rename 's/.asp$/.php/' '{}' \;

Now lets see the syntax of the command. The first part just searches for all files in the current directory with the extension “.asp”.

The magic happens in the rename option. A pattern is being used to search for files ending with “.asp” and substituting it with the “.php” extension each time. ‘s/.asp$/.php/’ is also known as a regular expression.

Regular expressions are used in most programming languages to search for patterns in data. Here we are using it to find a pattern among the file names.

6. Exclude a directory from the find syntax

There maybe a time when you would like to exclude a directory or a group of directories from the find command. Use the -prune switch for this in your command as follows.

find . -path ./misc -prune -o -name '*.txt' -print

In the above command the misc directory is being excluded from the output of the find command which is printing the results through the -print option on the console.

The sequence of the command is as follows

Get all files from current path > exclude misc from the results > find all *.txt files > print them on the console.

Lets see it in action

The above shows us the results of the find command with the entire directory listing.

As we can see above there are multiple directories in the location /tmp/content.

Here we shall run the commands above to show you the output of those commands. There are 4 text files placed randomly in all the folders. Also atleast one of those files is placed in the misc folder.

  • In the first command we can see how many text files are returned.
  • Next we run the command with prune and without -print option to just get the files from all the directories excluding misc. However, as you can see misc is also shown in the results. This is because the -prune option works alongside -print option to exclude misc from the results.
  • Finally, we run the command with -print which allows us to only see the file output from the command.

For further information you can run the following command to understand prune.

man find | grep prune

Alternatively, here is an example with multiple directories:

find . -type d ( -path dir1 -o -path dir2 -o -path dir3 ) -prune -o -print

Here we exclude dir1, dir2 and dir3, since in the find expressions it is an action, that acts on the criteria -path dir1 -o -path dir2 -o -path dir3 (if dir1 or dir2 or dir3 ) exist. Add to this the option type -d. Further action is -o -print or just print the listing.

7. Find command to send files to a remote server using rsync

There may be a need to selectively send some files from your system to another Linux, Unix or Mac OS system. We will use the well known Linux utility rsync for this purpose.

If you don’t know about rsync its a utility used for transferring files to and from remote systems. Rsync uses ssh (secure shell) which is a secure protocol used to access remote Linux and now Windows systems.

To find a file and send it to a remote server using rsync look at the following example.

find /videos -name "*.mpg" -type f -exec rsync -avzn {} root@192.168.3.204:/samba/traga/  \;

In the above command:

  • We search for all *.mpg files in the folder /videos.
  • The rsync command is part of the -exec option.
  • The syntax of rsync requires us to provide the source path and the destination path for the files.
  • In this case we use the curly brace syntax {} as a place holder for the results returned from the search of the *.mpg files.
  • {} is also acts as the source path for the rsync command.
  • The root@192.168.3.204 is the part where we specify the target system user and its ip address.
  • The path (/samba/traga/) after that is the path on the target system.

Running the above command will copy all files from the results of find into the target system no matter where those files are on your system. This is great because you don’t need to individually specify each source file, all that is taken care of by find.

8. Delete files between two dates

If you would like to delete files which are between two dates, you can use the following commands

touch -d "2010-11-21 23:59:59" /tmp/date.start;
touch -d "2010-11-23 00:00:00" /tmp/date.end;
find ./ -type f -newer /tmp/date.start ! -newer /tmp/date.end -exec rm {} \;
Explanation
  • The touch command is used to create a file date.start in the /tmp directory. The date on this file will be 2010-11-21 23:59:59 which is a Linux time-stamp for the starting date.
  • Another touch command is used to create a file date.end in the /tmp directory. The date on this file will be 2010-11-23 00:00:00 which is a Linux time-stamp for the ending date.
  • The find command is then used to find the files between the start and end date and thereafter remove them using the rm command.

9. Delete files x days old using find command.

Say you want to find the images on your computer older than 30 days and subsequently run some commands on them. Here is how you do exactly that.

find /path/to/files/ -type f -name '*.jpg' -mtime +30 -exec ls -lah {} \;
  • The first part of this command is used to search for all files with extension .jpg.
  • The -mtime option is used to specify the date of the file we are searching for as a numeric integer before and after the specific time.
  • In our case -mtime is used to specify files which are older than 30 days.
  • After find gets a list of such files from the path intended it then runs a ls -lah on them to display their sizes.

Next we can run the following command to remove them off the system.

find /path/to/files/ -type f -name '*.jpg' -mtime +30 -exec rm {} \;

10. Finding files containing specific text

Lastly, our find command comes to the rescue again ! There are many times I need to search a specific text in a group of text files. Now these text files are scattered around on my system and going to each directory is not an option. Sure I could use something like recoll (a utility for database type indexing of files) for this, but the whole database keeping thing is a tedious task runner for the CPU.

So here is a simple way of using find command to search for files containing some specific text.

find -name "*.txt" -type f| xargs grep "cardio"

we are using xargs here again to grep each file from the results of the find command and then within that file search for the string “cardio”.

That’s it folks ! The 10 very common uses of the find command for our day to day needs !

Other common uses of find command in Linux :

Some times we have to take a backup of our files, or search them by date or just delete files of a specific extension from a directory to another location. These are some common uses of the excellent find command which is installed on most Linux & Mac systems today.

To summarize, the reason I want to show how to use this tool is because sometimes we see people who write scripts that are more complex than they need to be whether that’s in Python or another language when really what they are trying to do can sometimes be done a lot more easily using the find command.

If you want to learn more about the find command you can use the following command to lookup all the options it provides

man find

So go ahead and look at the examples above, run them on your computer and bang away !

Leave a Comment