find how to exclude path - bash

I want to find all files in /usr/ but not in /usr/share
from this post Exclude a sub-directory using find
I tried:
find /usr -type f -not -path /usr/share -print
-> print files from /usr/share
from this post How to exclude a directory in find . command
I tried:
find /usr -path /usr/share -prune -print
->outputs nothing altough there are files in /usr/bin
I also tried:
find /usr -path ! /usr/share -type f -print
-> outputs an error

Drop -print and negate -path ... -prune:
find /usr ! \( -path '/usr/share' -prune \) -type f

By default, a boolean and is used to join operators and primaries. So find /usr -path /usr/share -prune -print is equvialent to find /usr -path /usr/share -prune -a -print. But you want to print things that are not pruned. So use:
find /usr -path /usr/share -prune -o -print
If you want to limit the search to regular files:
find /usr -path /usr/share -prune -o -type f -print

Related

find and delete folder and/or zip file in a directory [duplicate]

I was trying to get a list of all python and html files in a directory with the command find Documents -name "*.{py,html}".
Then along came the man page:
Braces within the pattern (‘{}’) are not considered to be special (that is, find . -name 'foo{1,2}' matches a file named foo{1,2}, not the files foo1 and foo2.
As this is part of a pipe-chain, I'd like to be able to specify which extensions it matches at runtime (no hardcoding). If find just can't do it, a perl one-liner (or similar) would be fine.
Edit: The answer I eventually came up with include all sorts of crap, and is a bit long as well, so I posted it as an answer to the original itch I was trying to scratch. Feel free to hack that up if you have better solutions.
Use -o, which means "or":
find Documents \( -name "*.py" -o -name "*.html" \)
You'd need to build that command line programmatically, which isn't that easy.
Are you using bash (or Cygwin on Windows)? If you are, you should be able to do this:
ls **/*.py **/*.html
which might be easier to build programmatically.
Some editions of find, mostly on linux systems, possibly on others aswell support -regex and -regextype options, which finds files with names matching the regex.
for example
find . -regextype posix-egrep -regex ".*\.(py|html)$"
should do the trick in the above example.
However this is not a standard POSIX find function and is implementation dependent.
You could programmatically add more -name clauses, separated by -or:
find Documents \( -name "*.py" -or -name "*.html" \)
Or, go for a simple loop instead:
for F in Documents/*.{py,html}; do ...something with each '$F'... ; done
This will find all .c or .cpp files on linux
$ find . -name "*.c" -o -name "*.cpp"
You don't need the escaped parenthesis unless you are doing some additional mods. Here from the man page they are saying if the pattern matches, print it. Perhaps they are trying to control printing. In this case the -print acts as a conditional and becomes an "AND'd" conditional. It will prevent any .c files from being printed.
$ find . -name "*.c" -o -name "*.cpp" -print
But if you do like the original answer you can control the printing. This will find all .c files as well.
$ find . \( -name "*.c" -o -name "*.cpp" \) -print
One last example for all c/c++ source files
$ find . \( -name "*.c" -o -name "*.cpp" -o -name "*.h" -o -name "*.hpp" \) -print
I had a similar need. This worked for me:
find ../../ \( -iname 'tmp' -o -iname 'vendor' \) -prune -o \( -iname '*.*rb' -o -iname '*.rjs' \) -print
My default has been:
find -type f | egrep -i "*.java|*.css|*.cs|*.sql"
Like the less process intencive find execution by Brendan Long and Stephan202 et al.:
find Documents \( -name "*.py" -or -name "*.html" \)
Braces within the pattern \(\) is required for name pattern with or
find Documents -type f \( -name "*.py" -or -name "*.html" \)
While for the name pattern with and operator it is not required
find Documents -type f ! -name "*.py" -and ! -name "*.html"
#! /bin/bash
filetypes="*.py *.xml"
for type in $filetypes
do
find Documents -name "$type"
done
simple but works :)
I needed to remove all files in child dirs except for some files. The following worked for me (three patterns specified):
find . -depth -type f -not -name *.itp -and -not -name *ane.gro -and -not -name *.top -exec rm '{}' +
This works on AIX korn shell.
find *.cbl *.dms -prune -type f -mtime -1
This is looking for *.cbl or *.dms which are 1 day old, in current directory only, skipping the sub-directories.
find MyDir -iname "*.[j][p][g]"
+
find MyDir -iname "*.[b][m][p]"
=
find MyDir -iname "*.[jb][pm][gp]"
What about
ls {*.py,*.html}
It lists out all the files ending with .py or .html in their filenames

Exclude directories with find command and executing a script on other directories

I currently have a directory structure that I need to be able to roll through each of 100 or so directories and run a script on them individually while excluding this check on a handful of other directories.
This is what I have been using in the past:
find ./OTHER/ -maxdepth 2 -wholename '*_*/*.txt' -execdir /files/bin/other_process {} +
I would like to exclude certain directories from this check and have not found a sufficient answer to this problem.
This has been my best attempt (or two) at the problem:
find ./OTHER/ \( -path ./OTHER/X???_EXCLUDE_THIS -prune -o -path ./OTHER/X???_IGNORE_THIS -prune -o \) -type d \(-name *_*/*.txt \) -execdir /files/bin/other_process {} +
I get:
find: paths must precede expression ./OTHER/A101_EXCLUDE_THIS/
This is the return that I get on nearly every variation that I have used.
This has been my best attempt (or two) at the problem:
find ./OTHER/ \( -path ./OTHER/X???_EXCLUDE_THIS -prune -o -path ./OTHER/X???_IGNORE_THIS -prune -o \) -type d \(-name *_*/*.txt \) -execdir /files/bin/other_process {} +
Errors in this attempt:
\(-name: There must be a space after \(.
-name *_*/*.txt: -name is for base of file name; use -path here.
*_*/*.txt: You should quote such patterns to prevent expansion by the shell.
-o \): -o does not belong at the end of an expression; you mean \) -o. But you don't need parentheses here.
-type d: Since you want to find regular files *.txt, you must not look for a directory.
With those errors corrected, it works:
find ./OTHER/ -path './OTHER/X???_EXCLUDE_THIS' -prune -o -path './OTHER/X???_IGNORE_THIS' -prune -o -path '*_*/*.txt' -execdir echo {} +

Quickly find and print all files in a folder, recursively, excluding ones from `node_modules` and `.git`

Consider the following folder structure starting in some root folder
/root/
/root/.git
/root/node_modules
/root/A/
/root/A/stuff1/
/root/A/stuff2/
/root/A/node_modules/
/root/B/
/root/A/stuff1/
/root/A/stuff2/
/root/B/node_modules/
...
Now I am in /root and I'd like to find all my own files inside it.
I have a small number of my own files and the huge number of files inside node_modules and .git.
Because of that, traversing node_modules and filtering it out is unacceptable, as it takes too much time. I want the command to never enter the node_modules or .git folder.
For excluding files only directly from the folder of the search:
find . -not \( -path './.git' -prune \) -not \( -path './node_modules' -prune \) -type f
If you want to exclude certain paths that are in subfolders, you can do that too using * wildcard.
Say you have node_modules too inside stuff1 and stuff2, and additionally dist and lib folders:
find . -not \( -path './.git' -prune \) -not \( -path './node_modules' -prune \) -not \( -path './*/node_modules' -prune \) -not \( -path './*/dist' -prune \) -not \( -path './*/lib' -prune \) -type f
Tested on Windows using git bash 1.9.5
It seems however it does not work properly on Windows when a filter like -name '*.js' is passed. The workaround could be to not use -name and pipe to grep instead.
Kudos to #Daniel C. Sobral

find command: list every directory and subdir, except .git or .hg dir and subdirs

I tried to use this command in my work dir
find . -type d \( ! -name .git -prune \) -o \( ! -name .hg -prune \)
Does not seem to work?
removing prune will only exclude .git or .hg directories, not their subdir
find . \( -name .git -o -name .hg \) -prune -o -type d -print
My answer is essentially the same as #Barmar's, which I have upvoted:
find . \( -name .git -o -name .hg \) -prune -o \( -type d -print \)
A little explanation about this command might be helpful to you:
Here -o means OR. When find finds a file's name matching .git or .hg, it stops the further search due to -prune option and evaluates to true, hence skips the other -o branch(which is directory printing). That's why only those directories not containing .git or .hg will show up.
You may also refer to this question on SO: How to use '-prune' option of 'find' in sh?

Exclude a sub-directory using find

I have directory structure like this
data
|___
|
abc
|____incoming
def
|____incoming
|____processed
123
|___incoming
456
|___incoming
|___processed
There is an incoming sub-folder in all of the folders inside Data directory. I want to get all files from all the folders and sub-folders except the def/incoming and 456/incoming dirs.
I tried out with following command
find /home/feeds/data -type d \( -name 'def/incoming' -o -name '456/incoming' -o -name arkona \) -prune -o -name '*.*' -print
but it is not working as expected.
Ravi
This works:
find /home/feeds/data -type f -not -path "*def/incoming*" -not -path "*456/incoming*"
Explanation:
find /home/feeds/data: start finding recursively from specified path
-type f: find files only
-not -path "*def/incoming*": don't include anything with def/incoming as part of its path
-not -path "*456/incoming*": don't include anything with 456/incoming as part of its path
Just for the sake of documentation: You might have to dig deeper as there are many search'n'skip constellations (like I had to). It might turn out that prune is your friend while -not -path won't do what you expect.
So this is a valuable example of 15 find examples that exclude directories:
http://www.theunixschool.com/2012/07/find-command-15-examples-to-exclude.html
To link to the initial question, excluding finally worked for me like this:
find . -regex-type posix-extended -regex ".*def/incoming.*|.*456/incoming.*" -prune -o -print
Then, if you wish to find one file and still exclude pathes, just add | grep myFile.txt.
It may depend also on your find version. I see:
$ find -version
GNU find version 4.2.27
Features enabled: D_TYPE O_NOFOLLOW(enabled) LEAF_OPTIMISATION SELINUX
-name only matches the filename, not the whole path. You want to use -path instead, for the parts in which you are pruning the directories like def/incoming.
find $(INP_PATH} -type f -ls |grep -v "${INP_PATH}/.*/"
By following answer for How to exclude a directory in find . command:
find . \( -name ".git" -o -name "node_modules" \) -prune -o -print
This is what I did to exclude all the .git directories and passed it to -exec for greping something in the
find . -not -path '*/\.*' -type f -exec grep "pattern" [] \;
-not -path '*/\.*' will exclude all the hidden directories
-type f will only list type file and then you can pass that to -exec or whatever you want todo

Resources