find command to search files with multiple extensions in Linux

In this article, we cover how to search files with multiple extensions using find command-line utility. There are times when we look for certain types of files like images, documents, text etc. It is easier to differentiate between file types by taking their extension in consideration. The names of files would be different but their extension would stay the same. So, an image file may end with .png or .jpg

Similarly, we can use other extensions as well to search for related file types.

Search files with multiple extensions using find command

First, we cover how to search for files which end with a specific extension. For instance, if we are looking for files which end with jpg extension in the Pictures directory then –

find /home/$USER/Pictures -name *.jpg

where, /home/$USER/Pictures is the target directory. And, *.jpg looks for all the files which end with extension jpg.

And, we can execute the same command again for different file types. But, what if we could get it done in one go. In such case, we won’t have to execute the command multiple times for different file types or files that end with different extensions.

For that, we use Logical OR operator within the command itself. One of benefits of using Logical OR operator is that, it helps combine output of both the statements. In our case, we put -o option for Logical OR command in the following command.

find /home/$USER/Pictures  -name *.png -o -name *.jpg

This time around, we look for both jpg and png image files within the target directory.

At times, the output is large enough that we need to save it somewhere to analyze it later. To redirect output of the command to a file –

find /home/$USER/Pictures -name *.png -o -name *.jpg > filename

In conclusion, we have covered how to search files with multiple extensions using find command in Linux.

Similar Posts