Link to home
Start Free TrialLog in
Avatar of mpaull
mpaullFlag for United States of America

asked on

List of files that exclude some files

Hi All,

I wanted to see if there was an easy way to use say the find command in linux to build a list of files that I can process with Imagemagick

Reason is I am periodically searching a folder e.g. /foo/* for jpg files that have not been processed. A processed file will have an accompanying with the same filename but different extension. This file denotes that the jpg was previously processed. I want to then ignore this file the next time I process the folder. So, I would like to be able to:

List all jpg files that do not have an accompanying file. E.g. in a folder, their contains files, a.jpg, b.jpg, c.jpg, d.jpg, d.mod. I would like a list of files in that folder that would exclude the d.jpg file, leaving a.jpg, b.jpg, c.jpg.

Want to be able to process a folder recursively.

Either throw all the files to a list file, to then process through with a loop or possibly execute the command inline with the search. E.g. the command I can run to process and replace the existing jpg file is mogrify quality 50 /path/to/foo.jpg > /path/to/foo.compressed

Any help would be greatly appreciated.
Avatar of ozo
ozo
Flag of United States of America image

perl -le '@mod{map/([^.]+)/,<*.mod>}=(); print for grep!exists $mod{(/([^.]+)/)[0]},<*.jpg>;'
find .  -name '*.jpg' \! -exec bash -c 'test -f `expr substr "$0" 1 \( length "$0" - 4 \)`.mod' {} \;  -print
ASKER CERTIFIED SOLUTION
Avatar of ghostdog74
ghostdog74

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of mpaull

ASKER

Hi ghostdog74

That works great, got it working recursively. Without understanding a whole lot about how the above works, could you perhaps explain it a little? I wanted to do the same thing with now files that have the .mpeg extension, also placing a .mod file when its processed. I could not get it to work with a .mod extension, made it work with .modv by replacing the first for loop with for i in *.[mm][op][ed][gv]

Avatar of ghostdog74
ghostdog74

hi
i am not very good at explanation, however, perhaps you can try this on your terminal

# i=test.jpg
# echo ${i%%.*}
test

for more details see http://tldp.org/LDP/abs/html/parameter-substitution.html

i am sure you know what a for loop is, so i will skip that.
the if statements checks for existence of a jpg as well as a mod file. If they exists, that means they are already processed, so continue the for loop to read in the next file.

Avatar of mpaull

ASKER

Thanks ghostdog74. I figured out how your code was working, and made some adjustments. The key part that I got from your code was "if [ -f <filename> -a -f <filename>.mod ]". I did not think to do it this way and I did not know about the -a option. Thanks again.