List of files that exclude some files

Posted on 2007-10-06
Last Modified: 2012-05-05
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.
Question by:mpaull
    LVL 84

    Expert Comment

    perl -le '@mod{map/([^.]+)/,<*.mod>}=(); print for grep!exists $mod{(/([^.]+)/)[0]},<*.jpg>;'
    LVL 84

    Expert Comment

    find .  -name '*.jpg' \! -exec bash -c 'test -f `expr substr "$0" 1 \( length "$0" - 4 \)`.mod' {} \;  -print
    LVL 9

    Accepted Solution

    >>>find .  -name '*.jpg' \! -exec bash -c 'test -f `expr substr "$0" 1 \( length "$0" - 4 \)`.mod' {} \;  -print
    It might be overkill to call bash  for each jpg found using exec, especially when there are many jpgs...

    @OP, in bash, for one directory. I leave you to find a way for recursive.

    for i in *.[jm][op][gd]
       if [ -f ${prefix}.jpg -a -f ${prefix}.mod ];then           
            echo "$i" #do your image processing here.


    Author Comment

    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]

    LVL 9

    Expert Comment

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

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

    for more details see

    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.


    Author Comment

    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.

    Featured Post

    Looking for New Ways to Advertise?

    Engage with tech pros in our community with native advertising, as a Vendor Expert, and more.

    Join & Write a Comment

    This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
    Introduction We as admins face situation where we need to redirect websites to another. This may be required as a part of an upgrade keeping the old URL but website should be served from new URL. This document would brief you on different ways ca…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now