How to search filesystem for files based on a filename extension

The following config will few examples on how to quickly search a filesystem for files based on file extension. For this we only need two command line tools find and grep. First, lets search for all files with a filename extension .sh recursively within /home/ directory:

$ find ~/ -type f | grep "\.sh$"

Using the find command we have searched for all files within ~/ user home directory and used grep to print only those files ending with .sh filename extension. In the following example we will search for all files with filename extension .sh, .txt and .py:

$ find  ~/ -type f | grep -E "\.sh$|\.txt$|\.py$"

Using the above example we can also perform an action on each file. For example the following linux command will remove all files from user home directory ~/ with filename extension .sh, .txt and .py:

$  find  ~/ -type f | grep -E "\.sh$|\.txt$|\.py$" | xargs -I {} rm {}


Comments and Discussions
Linux Forum