10

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}
Pierre
  • 131
  • 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 09:08

3 Answers3

10

for the first one

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

where

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

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

Archemar
  • 31,554
7

It's usually not worth it to type something like:

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

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

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

Advantages:

  • type less, get results faster
  • more readable (no \, unnecessary .*)
  • may run faster–piped commands allow the kernel to balance between two CPUs, yes I tested right now in my ordinary console)
  • acts similarly in case you want to read from a list of files or other sources
gena2x
  • 2,397
5

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*"
chaos
  • 48,171
  • 3
    Just in case someone didn´t know the meaning, -a stands for AND (likewise, -o stands for OR). Source: http://man7.org/linux/man-pages/man1/find.1.html – Tiago Cardoso May 04 '18 at 23:44