0

I would like to know if this is possible:

I have a large number of .mov files in a folder. I would like to know if I can copy the files to a separate folder but include the md5sum in the new filenames, after the actual name but before the file extension. So for example, if once of the files was named johnnycash.mov I would like it to end up looking something like this: johnnycash_8cbda72b2ce62f4a69a0b454ccccbe4.mov

I a noobie and this kind of thing is maybe a bit above my level but I would appreciate if anyone has any workable solution.

Cheers!

3 Answers 3

1

This problem is probably best approached in two steps:

  1. Copy the files to the new folder
  2. Rename them according to their md5sums

Step 1 is easy:

cp -r folder_with_movs new_folder

Step 2 needs a bit of scripting:

cd new_folder
for i in *.mov; do
    bn="${i%.*}"
    ext="${i##*.}"
    md5=$(md5sum "$i" | awk '{ print $1 }')
    mv -v "$i" "${bn}_${md5}.${ext}"
done
1

As there are only extensions ".mov" this makes it somewhat easier than catching all possible extensions:

for file in *.mov; do
 SUM=$(md5sum "$file" | awk '{print $1}')
 cp "$file" <destination>/$(echo "$file" | sed -e "s/.mov/_${SUM}.mov/")
done

Do a loop over all mov files.

Compute md5sum, take only the first column from the output (this awk statement always comes in handy, now you've seen it)

Copy the file to the new destination while doing some renaming: sed exchanges the suffix ".mov" with the string "_" then the md5sum, and then the extension ".mov", so it will be conserved.

Be aware of spaces in filenames (better avoid them).

1

Something like that might work. Its very rough but will move your stuff with md5sum on filename..

#!/bin/bash

CURRENTDIRECTORY=xxx
NEWDIRECTORY=xxx

for i in `ls $CURRENTDIRECTORY`; do
    if [ -f $i ]; then
        md5=`md5sum $i | awk '{print $1}'`
        name=$md5-$i
        cp $i $NEWDIRECTORY/$name
    fi
done

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .