find -name *.xyz -o -name *.abc -exec to Execute on all found files, not just the last suffix specified
find
works by evaluating the expressions you give it until it can determine the truth value (true or false) of the entire expression. In your case, you're essentially doing the following, since by default it ANDs the expressions together.
-name "*.xyz" OR ( -name "*.abc" AND -exec ... )
Quoth the man page:
GNU find searches
the directory tree rooted at each given file name by evaluating the
given expression from left to right, according to the rules of precedence (see section OPERATORS), until the outcome is known (the left
hand side is false for and operations, true for or), at which point
find moves on to the next file name.
That means that if the name matches *.xyz
, it won't even try to check the latter -name
test or -exec
, since it's already true.
What you want to do is enforce precedence, which you can do with parentheses. Annoyingly, you also need to use backslashes to escape them on the shell:
find ./ \( -name "*.xyz" -o -name "*.abc" \) -exec cp {} /path/i/want/to/copy/to \;
Delete content of all folders with certain name
To get the *
in -exec rm -r "{}/*"
expanded you would need to run a shell instead of executing rm
directly, but with this option you must be careful not to introduce a command injection vulnerability. (see https://unix.stackexchange.com/a/156010/330217)
Another option is to use -path
instead of -name
find . -path '*/build/*' -exec rm -r "{}" \; -prune
Option -prune
is to avoid descending into a directory that has been removed before.
Find command, linux
find -type f \( -name "*.avi" -o -name "*.mp4" \) -exec md5sum {} + > checklist.chk
See here for explanation
Finding all files that have whitespace in last few lines
You have to escape the +
inside grep expression, ex. see gnu grpe manual.
Using < <(...)
shell redirection with process substitution is unneeded here, just pipe it |
.
The following works. Notice how I needed to double escape \\+
, because \\
is expanded to \
inside "
braces.:
find . -type f -exec sh -c 'tail -n 5 "$1" | grep -q " \\+$" && printf "%s\n" "$1"' -- {} \;
However when using xargs
you can do it in parallel -P0
, also I like the debugging with -t
better. For strange filenames add -print0
and -0
options to find
and xargs
.
find . -type f | xargs -n1 sh -c 'tail -n 5 "$1" | grep -q " \\+$" && printf "%s\n" "$1"' --
Find all files with a filename beginning with a specified string?
Use find
with a wildcard:
find . -name 'mystring*'
Related Topics
What Context Does the Scheduler Code Run In
Rename All Files in Folder to Numbered List 1.Jpg 2.Jpg
Rtmp: Is There Such a Linux Command Line Tool
How to Search for Files and Zip Them in One Zip File
What Happens After a Packet Is Captured
What Is the Advantage of Using Supervisord Over Monit
How to List Specific Type of Files in Recursive Directories in Shell
How to Install Maven into Red Hat Enterprise Linux 6
Stracing to Attach to a Multi-Threaded Process
What Is .D File After Building with Make
Svn In-Place Import and Checkout
View Multi-Core or Mlti-Cpu Utlization on Linux
Linux: How to Force a Specific Network Interface to Be Used
How to Start Postgresql Service on Centos 7
When Should I Write a Linux Kernel Module
Linux "Free -M": Total, Used and Free Memory Values Don't Add Up