Combining multiple find commands into one
I have a bash file with the following commands to copy books from my books folder into my toread / Read Later folder
find /books -name '*.pdf' -exec cp -n {} /toread \;
find /books -name '*.epub' -exec cp -n {} /toread \;
find /books -name '*.azw*' -exec cp -n {} /toread \;
find /books -name '*.mobi' -exec cp -n {} /toread \;
I want to get rid of the repetitiveness of these commands and batch the actions into one?
-o
in find
expression is logical "or". There's a quirk though: juxtaposition (which is an implied "and" operator) takes precedence over the -o
operator. For this reason you often need parentheses. They should be escaped or quoted, otherwise they will be interpreted by the shell:
find /books \( -name '*.pdf' -o -name '*.epub' -o -name '*.azw*' -o -name '*.mobi' ')' -exec cp -n {} /toread \;
Note I deliberately escaped the opening parenthesis and quoted the closing one, just to show the two ways.
Without parentheses the -exec
part would apply only to *.mobi
files (compare this question).