I want to recursively find all directories and files inside a path but want to exclude some other directories and files that match given patterns. I then want to execute further commands on the search result, maybe cp
, mv
, rsync
or rm
the result of find
.
Suppose I want to prune entire node_modules
, .git
and build
directory and also prune enhancers
directory but would like to include in the search result a file with -name xyz.js
that is present at unknown depth inside the enhancers
directory.
With the command below I am able to achieve the desired result except include xyz.js
file in the search result. The command is very long.
find . -path "./node_modules" -prune -o -path "./.git*" -prune -o -path "./build" -prune -o -path "*/enhancers" -prune -o -print | grep 'xyz.js'
--root
|--node_modules
|--public
|--build
|--.git
|--.gitignore
|--.env
|--package.json
|--src
|--history
|--store
|--middleware
|..
|..
|--enhancers
|..
|--xyz.js
In the schematic directory structure above |..
stands for unknown depth(level) inside the directory.
In the shown directory structure, I want to include all the files in the search result except,
- the entire
node_modules
directory - entire
.git
directory .gitignore
file- entire
enhancers
directory, but includexyz.js
file in search result
Is there a shorter way to do it? Major confusion is between regexp and glob patterns.
If I substitute ./.git*
with ./\.git*
, then also it works.
If I use more than one *
in */enhancers
, then also it works.
What pattern rules to search for files and directories are applicable in find
command?