If you know what you’re looking for, then the Linux find command is usually the best tool to use. (If you don’t know what you’re looking for, then the first step is to figure that out.) The linux find command searches recursively by default (it will enter sub-directories) and considers every file it comes across. If the file matches a filter, it will perform one or more actions on the file. The command line specifies the starting folder, the filter and the actions to perform. The default action is -print which will print the name of the file to stdout. The following examples demonstrate some basic uses of find on Linux:
Find every file and folder | > find . . ./folder0 ./folder0/file0.x ./folder1 ./folder1/file1.x ./script.sh |
Find a specific file | > find . -name “file0.x” ./folder0/file0.x |
Find all files with an extension | > find . -name “*.x” ./folder0/file0.x ./folder1/file1.x |
Find recently modified files | > touch folder1/file1.x > find . -mmin -5 folder1/file1.x |
All of these examples demonstrate using find stand-alone. The find command can also be used with other programs by piping the output. This is useful when automating specific tasks, as the following examples demonstrate:
Delete all .bak files in a folder | > find . -name “*.bak” | xargs rm |
Count all .x files in a folder | > find . -name “*.x” | wc -l |
Change all .sh files to executable | > find . -name “*.sh” | xargs chmod 744 |
All of these commands pipe the output of find to another command which then operates on all of the files that were found. Note that in most cases these commands will not generate any output if they succeed.