8

This question already has an answer here:

As a standard for my file/dir naming I use something like putting date and tags string into the file name separated by underscore, eg:

2015_03_10_usa_nyc_vacations_album_pictures

For a long time I have used software's like Recoll, but usually it gives much more records than I need. Since my naming is pretty consistent I think there should be a way to scriptsize it using find command.

Although I know how to perform find searches for single conditions I am not sure how to find multiple criteria/conditions. That is, how do I find all the files whose names satisfy certain conditions. Let consider the following examples that contain

  1. {2015} AND { {album} OR {picture} }
  2. {album} AND {vacations} AND NOT {2015}

marked as duplicate by mosvy, Jeff Schaller Jun 15 at 1:12

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 2
    Did you see the -a (and) and -o (or) operators to find in the documentation? Of course you can combine them with any other find operator like -name or -newer. – Celada Mar 10 '15 at 9:08
5

It's usually doesn't worth to type stuff like

find /dir -regex '.*2015.*\(album.*\|picture.*\)'

don't waste your time, use grep and UNIX concept of one tool for each task:

find /dir|egrep '2015.*(album|picture)'

This is:

  • type less - get result faster
  • more readable (no \, unnecessary .*)
  • may be faster (piped commands mean that kernel can balance between two CPU, yes I tested right now in my ordinary console)
  • will be same in case if you wish to read file list from file or from other source
3

for the first one

 find DIR \( -name 2015\* -a \( -name \*album\* -o -name \*picture\* \) \) -delete

where

  • you have to escape * to avoir expansion
  • you have to escape ( and ) to avoir subshell
  • replace -delete by whatever you like

the second one is left as homework (hint \! )

  • I would not suggest -delete, but hey this is the more comprehensive answer. Maybe use -print or -exec ls -al {} + – asoundmove Nov 17 '15 at 0:24
3

For the first one find with -regex would be good:

find /dir -regex '.*2015.*\(album.*\|picture.*\)'

For the second one that:

find . -name "*album*" -a -name "*vacations*" -a -not -name "*2015*"

Not the answer you're looking for? Browse other questions tagged or ask your own question.