find all but skip one folder - linux

I have a command that looks through all my sub folders for files, however I want it to skip a folder from the search and I'm not sure what is the right way to do this:
find -name '*.avi' -o -name '*.mkv' -o -name '*.mp4' -o -name '*.vob'
I want it to not look into the folder name: secure
I tried:
find -name '*.avi' -o -name '*.mkv' -o -name '*.mp4' -o -name '*.vob' --skip 'secure'
but it does not work.
Thanks for your help in advance.

There is no --skip argument in GNU find. But you can do what you want using the -path and -prune expressions. The syntax is a little weird: you use -path ./secure -prune as a term which you then OR with the rest of the expression. So in your case:
find . -name '*.avi' -o [...] -o -path ./secure -prune
Note that this will still return the directory ./secure in the results, but nothing inside it.

What about the following?
find \( -name '*.avi' -o -name '*.mkv' -o -name '*.mp4' -o -name '*.vob' \) -a -not -path './secure*'

Related

linux find command: exclude path from result

I used the find command to find all python files in a folder, and exlude some folder.
find . -type d \( -path ./venv -o -path ./virtualenv -o -path ./lib \) -prune -o -iname '*.py'
It's give the following output:
...
./tests/test_a.py
./venv
How can I tweak the find command to exclude ./venv in the resultset?
If you don't specify any actions, find will implicitly use -print for any file that matches your expression. -prune always evaluates as true (in addition to preventing further recursion), which is why ./venv still ends up being printed.
To avoid that, simply add a -print to your other branch:
find . -type d \( -path ./venv -o -path ./virtualenv -o -path ./lib \) -prune \
-o -iname '*.py' -print
Now there's an action, so find no longer prints everything that happens to evaluate as true, and instead only prints the files you're explicitly looking for.

Linux Find command- exclude find based on file name

I feel like this is a ridiculously easy question but I cannot find a simple regex answer for this. Basically, I am trying to use find to get a list of all files in my system with some exclusions. One of these exclusions is any file that ends in .Foo.cs, or any file named FooInfo.cs. I have successfully excluded a couple directories from my search, but cannot seem to exclude these two files. I've tried using -name, but would -name even work for this? Below is my expression. Thanks.
find . ! -name 'FooInfo.cs' ! -name '*.Foo.cs' -type d \( -name Foo-o -name 2Foo -o -name 2_Foo \) -prune -o -type f ! -size 0 \( -name "*.java" -o -name "*.cs" -o -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" -o -name "*.c" -o -name "*.h" -o -name "*.scala" -o -name "*.css" -o -name "*.html" -o -name "*.bat" -o -name "*.js" \) -exec realpath {} \;| xargs grep -L CUSTOMERINFO | sed -e 's/$/\r/g' >> ../output.txt
So I'm not sure why, but I ended up fixing this by changing the order of what I'm excluding. Instead of excluding at the very beginning, the following worked (moving the ! -name '.FOO.cs' and ! -name '.fooinfo.cs' to right after the declaration type -f).
I'm assuming this worked because they are files so they must be flagged with type -f. But please comment and correct below if you know why.
find . -type d \( -name Foo-o -name 2Foo -o -name 2_Foo \) -prune -o -type f ! -size 0 ! -name 'FooInfo.cs' ! -name '*.Foo.cs' \( -name "*.java" -o -name "*.cs" -o -name "*.cpp" -o -name "*.cxx" -o -name "*.cc" -o -name "*.c" -o -name "*.h" -o -name "*.scala" -o -name "*.css" -o -name "*.html" -o -name "*.bat" -o -name "*.js" \) -exec realpath {} \;| xargs grep -L CUSTOMERINFO | sed -e 's/$/\r/g' >> ../output.txt

Find different types of files and move to a specific directory

Finding *.mkv and *.mp4 works
find /home6/movies/ -name '*.mp4' -o -name '*.mkv'
but moving them for some reason partially fails and moves only mkv files
find /home6/movies/ -name '*.mp4' -o -name '*.mkv' -exec mv {} /home6/archive/ \;
Am I using an incorrect find switch "-o" for this task?
Looks like you need to surround the or expression in parentheses so the exec applies to both matches.
This is a similar question: `find -name` pattern that matches multiple patterns
find /home6/movies/ \( -name '*.mp4' -o -name '*.mkv' \) -exec mv {} /home6/archive/ \;

Unable to use "-prune" option from find command

I don't want to find "passwd" under "/etc" directory, but I do want to find rest of the "passwd", I am trying following,
sudo find / -name '/etc' -prune -o -name 'passwd' -print
this is the output I get,
/home/previous_cache/1_0_59/httpd-2.4.7/srclib/apr/passwd
/home/1_0_59/httpd-2.4.7/srclib/apr/passwd
/etc/pam.d/passwd
/etc/passwd
/etc/cron.daily/passwd
/usr/share/bash-completion/completions/passwd
/usr/share/lintian/overrides/passwd
/usr/share/doc/passwd
/usr/bin/passwd
this is the output I expect,
/home/previous_cache/1_0_59/httpd-2.4.7/srclib/apr/passwd
/home/1_0_59/httpd-2.4.7/srclib/apr/passwd
/usr/share/bash-completion/completions/passwd
/usr/share/lintian/overrides/passwd
/usr/share/doc/passwd
/usr/bin/passwd
I referred this, good info about prune, but could not solve my problem
Try this:
find / -path '/etc' -prune -o -name 'passwd' -print
try -path instead of -name
sudo find / -path /etc -prune -o -name 'passwd' -print

linux find prune

I am trying to exclude two directories; vntvdone and downloading, but for some reason, my command still goes in there and outputs the file within it:
find -name '*.avi' -o -name '*.mkv' -o -name '*.mp4' -o -name '*.VOB' -o -path './downloading' -prune -o -path './vntvdone' -prune
I am also running into problems where if a folder/directory that has .mp4 in it, it also gets treated as a file as well... how can we do this search only for files and not folders?
I find it easier to use ! than use prune. I've assumed the starting path for find is '.' The example omits it.
find . \( -type d -a ! -name 'downloading' -a ! -name 'vntdone' -a ! -name '.' \) -o -name \*.avi -o -name \*.mkv -o -name \*.mp4 -o -name \*.VOB

Resources