LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 02-03-2008, 12:09 PM   #1
migla
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Rep: Reputation: 0
Append md5sum or similar to filename, batch copy from multiple directories to one


How do I add the md5sum (or equivalent) of each file in a big bunch of files to each filename?

[Edit:] I had posted this on ubuntuforums as well a few hours ago, and now that seems an uncool thing to have done, since I might make goodhearted people do unnecessary work for me.

Also, using exif-data seems more sensible. An answer I got about that was as follows:

Quote:
exiv2 rename -r "%m-%d-%y@%H:%M:%S " *.*
That would probably need to be modified to include the filtering with the find command... Or just putting DSCN* instead of *.* (allthough *.* would not rename files that have no exif-data, I presume...)[/Edit]

Actual use case:

Quote:
I have lots of photos on 2 computers, most of the files being on both, but under different directory structures and some filenames possibly being duplicate for different photos even one particular computer but in different subfolders.

I want to get organized and start syncing, but first I want to make sure all photos have unique filenames and can be in one big fat directory.
By appending some unique hash to the filename I could do something like the following command to copy all photos to one dir (all my photos start with "DSCN"):
Code:
find . -type f -name \DSCN* -exec cp {} /home/username/bigfatphotodir/ \;
I don't know whether that would overwrite files, which it should do once I've got the hashes appended, since then all name-dupes will also be actual dupes.

I guess the md5sum calculating and hash-appending should be put into that command somehow.

Last edited by migla; 02-03-2008 at 03:02 PM.
 
Old 02-03-2008, 01:01 PM   #2
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,541
Blog Entries: 54

Rep: Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924
If these are digital cam images wouldn't it be "better" to look at and append the EXIF dates or something like that?
 
Old 02-03-2008, 02:00 PM   #3
migla
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
If these are digital cam images wouldn't it be "better" to look at and append the EXIF dates or something like that?
That would probably make sense. I'd be happy with answers concerning either case.

[Edit:] By the way, if someone is fluent in Python or Perl or whatever, a script of any persuasion would be ok. (I'm mostly interesting in learning Bash or Python, but not more than in getting this to work.)

Last edited by migla; 02-03-2008 at 02:11 PM.
 
Old 02-03-2008, 06:11 PM   #4
unSpawn
Moderator
 
Registered: May 2001
Posts: 27,541
Blog Entries: 54

Rep: Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924Reputation: 2924
Quote:
Originally Posted by migla View Post
[Edit:] I had posted this on ubuntuforums as well a few hours ago, and now that seems an uncool thing to have done, since I might make goodhearted people do unnecessary work for me.
You're free (as in speech) to do as you please. Still, questioning your own action gets you my respect, and a script.
YMMV(VM) as usual:

Code:
#!/bin/sh 
# For testing. Comment the line below out to make it work after you tested the script. If nukes a small
# island in the Pacific, finds that life in this universe is a mistake which must be corrected or
# simply breaks you must repent by finding out which TV series featured a craft which sported the 
# phrase "Abandon all hope" and where that phrase originated from. 
set -envx
# Dirname you mounted the medium on:
SOURCE_DIR="/media/flash"
# Dirname you write to:
TARGET_DIR="/home/username/bigfatphotodir"
# exiftime from the "exiftags" package
which exiftime >/dev/null 2>&1 || echo { "${0//*\//} [FATAL]: no exiftime installed, exiting."; exit 1; }
# We have explosive.
find "${SOURCE_DIR}" -type f -name DSCN\* | while read SOURCE; do
 # Get the files' creation time:
 SOURCE_TIME=($(exiftime -s ' ' -tc "${SOURCE}"))
 # Mangle it so we don't get spaces and colons:
 SOURCE_TIME="${SOURCE_TIME[2]}${SOURCE_TIME[3]}"; SOURCE_TIME=${SOURCE_TIME//:/}
 # Do "basename", then concat strings so the filename we intend to write to can be checked:
 DEST="${SOURCE//*\//}"; DEST="${DEST}${SOURCE_TIME}"
 # If the filename is not present write, else report.
 [ ! -f "${TARGET_DIR}/${DEST}" ] && cp "${SOURCE}" "${DEST}" ||\
  echo { "${0//*\//} [INFO]: already got \"${DEST}\"."
done
exit 0
 
Old 02-03-2008, 06:27 PM   #5
migla
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
I ran the following command:
Code:
find . -type f -name \DSCN* -exec exiv2 -r':basename:_%m-%d-%y@%H:%M:%S' rename {} \;
...and now all my pictures have a name similar to "DSCN1253_05-07-06@12:22:34.JPG"
I should have thought about what order I wanted the date displayed first, since I'd preferred yymmdd, but that's ok. It did also tell me "Warning: Makernote: Pointer to next IFD is out of bounds; ignored." about a few hundred times. Whatever that means...

But anyway, problem solved. Now I'll just need to use find with the cp command to move all into one dir. But I'll do that tomorrow. First I'll make sure everything really is allright and also test it first on some testfiles.
 
Old 02-03-2008, 06:54 PM   #6
migla
LQ Newbie
 
Registered: Jan 2007
Posts: 5

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by unSpawn View Post
You're free (as in speech) to do as you please. Still, questioning your own action gets you my respect, and a script.
Wow! Thanks for the script! (I didn't notice at first that you had replied when I posted my last post.)

I'll be sure to look very closely at that when I wake up. It looks like I could learn a lot from it. I'm sure going to look into regular expressions now and learn to mangle the colons out of what I did to my filenames with my command in the previous post. Thanks again!

Exited about learning new things, but must get some sleep.

Last edited by migla; 02-03-2008 at 07:11 PM.
 
  


Reply

Tags
batch, exif, md5sum, rename


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Copy files from multiple directories into one directory MadRabbit Linux - Newbie 8 02-07-2014 08:56 PM
Copy one file to multiple vhost directories thetawaverider Linux - Newbie 5 03-24-2006 05:57 PM
batch append string to the end of a determined line in text files osio Programming 6 06-30-2005 10:28 AM
Copy one file to multiple directories cmfarley19 Linux - General 9 11-10-2004 01:57 PM
find and copy files into multiple directories avargas22 Linux - Newbie 2 04-01-2004 12:11 PM


All times are GMT -5. The time now is 10:29 PM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration