How to search multiple directories with find command

The “find” command is a powerful command-line utility that can be used to search for files and directories in one or more directories.

Here’s an example of how to search for files with the “.txt” extension in the directories “/home/user/dir1” and “/home/user/dir2”:

find /home/user/dir1 /home/user/dir2 -name "*.txt"

This command uses the “find” command with the paths “/home/user/dir1” and “/home/user/dir2” followed by the “-name” option and the argument “*.txt” to search for files with the “.txt” extension in both directories.

You can also search for files in multiple directories and their subdirectories by using the -path option and -prune options together, for example:

find /home/user/dir1 /home/user/dir2 -path "*/.git" -prune -o -name "*.txt" -print

This command uses the “find” command with the paths “/home/user/dir1” and “/home/user/dir2” followed by the “-path” option and the argument “*/.git” to exclude the subdirectories named .git, then uses the “-prune” option, followed by the “-o” option to search for files with the “.txt” extension in both directories and their subdirectories.

Please note that these commands are for a basic usage and you should adapt them according to your specific requirements. Also, before making any changes, it’s recommended to have a backup plan in case something goes wrong, and also to test the changes before applying them to your production environment.

Leave a Comment