folder1 contains only .flv files.

folder2 contains only .mp4 files.

All .flv files in folder1 SHOULD have a corresponding file ending in .mp4 in folder2 :

/folder1/123.flv and /folder2/123.mp4.

I need a script that will find any .flv's that are missing a .mp4 and execute a script that will run mencoder to create the corresponding .mp4. I've done my best, this is what I've come up with but am stuck with the IF THEN statements.

for i in `find /folder1 -type f -iname '*.flv' -printf "%f\n" | 
    sed 's/.flv//g'` ; do 
    if [[ -n $(find /folder2/$i.mp4 -exec grep \-q 0 {} \;) ]] then
        /scripts/script.sh $i  fi ; done

The bash script needs to be executed like so:

/scripts/script.sh $i 

so the filename without any extension.

  • 1
    looks like you have a semi-colon (or a newline) missing before the then and the fi. – cas Nov 9 '15 at 1:09

Try this:

find folder1 -type f -iname '*.flv' -exec bash -c \
    'f=${1#folder1/}; [ -f "folder2/${f%.flv}.mp4" ] || 
     scripts/script.sh "${1%.flv}"' none {} \;

This will work file's whose names contain whitespace or other difficult characters.

How it works

  • find folder1 -type f -iname '*.flv'

    This finds the flv files

  • -exec bash -c '...'

    For every flv file found this tells find to run bash which runs the script in quotes (see below).

  • f=${1#folder1/}

    This creates a variable f which has the name of the flv file without the leading directory.

  • [ -f "folder2/${f%.flv}.mp4" ]

    This tests to see whether a corresponding mp4 file exists.

  • ||

    This tells bash to execute the command which follows only if the preceding command returned false (in other words, if no such mp4 file was found).

  • scripts/script.sh "${1%.flv}"'

    This runs your script on those flv files that are missing mp4 files. The files are supplied to script.sh with the .flv part removed.

  • none {}

    This tells bash to assign the string none to $0 (this placeholder is never used) and to assign the name of the file found by find, denoted {}, to shell variable $1.

  • \;

    This signals the end of the find's -exec command.

  • 1
    Do you get paid to do this, because you should. Thanks a million – JCats Nov 9 '15 at 2:06

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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