Find Command Exclude Directories From Search Pattern

https://‮ww‬w.lautturi.com
Find Command Exclude Directories From Search Pattern

To exclude directories from a search pattern when using the find command, you can use the -type option with the d argument to specify that you only want to search for files, not directories.

Here is an example of how to use the find command to search for files with a .txt extension in the current directory, excluding directories:

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

This command will search for files with a .txt extension in the current directory and all subdirectories, but will not include directories in the search.

You can also use the -prune option to exclude specific directories from the search. For example, to exclude the node_modules directory from the search, you can use the following command:

find . -type d -name "node_modules" -prune -o -type f -name "*.txt"

This command will search for files with a .txt extension in the current directory and all subdirectories, but will exclude the node_modules directory and all its subdirectories from the search.

Keep in mind that the find command is a powerful tool that can be used to search for files and directories based on a wide range of criteria. You can use the various options available with the find command to fine-tune your search and exclude directories as needed.

Created Time:2017-10-28 20:14:18  Author:lautturi