Automatic way to rename files
Hey all,
I am trying to rename the family photos, I am not happy with the current file name structure they are listed as ATM. Basically they are listed as follows... [01-10-12] 01 [01-10-12] 02 [01-10-12] 03 I'd like to list them as follows... 20121001_01 20121001_02 20121001_03 Someone on a different forum gave me the following solution... for OLD in *.jpg do cp -p $OLD `echo $OLD | sed 's/.\(..\)-\(..\)-\(..\). /20\3\2\1_/'` done When I try and run the code though I get the following error... cp: target ‘20110320_01.JPG’ is not a directory I have a feeling it is something extremely simple like adding a directory path into the code or something like that but I can't figure out for the life of me how to amend it. I should also mention I am running the shell script in the same folder as the photos are in. Can someone help me amend the code or suggest another way at renaming these files? I am using Slackware and am quite a beginner with all things Linux. Thanks all. I really appreciate the help. |
Try this
Quote:
Quote:
There is a space character after [01-10-12] If there is no space and files are like this Quote:
Quote:
|
Yes rename is probably the way to go, however, you are assuming the date is the same for all jpegs which looking at the original code I would suggest is not the case.
Also, this would also depend on Slackware having the perl version of rename installed as the default version that comes with util-linux does not have the same level of ability. So if we stick with the original example, you were close but forgot a golden rule in bash ... quote - quote - quote. If we were to take the first example file and place the output into your script, the cp line would look as follows: Code:
cp -p [01-10-12] 01.jpg `echo [01-10-12] 01.jpg | sed 's/.\(..\)-\(..\)-\(..\). /20\3\2\1_/'` if you are copying more than one file, you must be copying them into a directory ... which you are not as the renamed copy of your file does not exist as a directory ... hence the error So here is your code with quotes and some changes to make it all a little more obvious: Code:
for old in *.jpg 1. Double quotes around variable names to suppress any splitting on whitespace or any shell centric items being expanded (ie [0-9] means any of the number 0 to 9 can be in this spot) 2. $() in place of back ticks. Main reason is it is a lot more readable that guessing the difference between ' and `. Also, as your scripts get bigger and you may need to nest expansions inside each other and back ticks then become a nightmare 3. -r switch in sed just eliminates the need to escape the round brackets when saving information 4. The regex is up to you, but this version will not be concerned about the possibility of a space or not as per above post 5. <<< is a bash construct called a here document and I chose this over the echo just to keep the file name in its regular spot for sed (echo will also work fine) |
Thanks a lot eklavya and grail, I really appreciate the help and in-depth answers you have given me.
It works! Thanks again. I've learnt a lot. |
Try GPRename
Install GPRename thru Synaptic.It is batch renamer for files(any type)
You may also install Shotwell For Sorting the photos by date use Shotwell.(install it thru Synaptic). Shotwll makes the albums Year wise- Monthwise and date wise.It removes duplicate photos. Both applications are wonderful and having GUI. |
Please remember to mark as SOLVED once you have a solution.
|
All times are GMT -5. The time now is 06:00 AM. |