LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Automatic way to rename files (https://www.linuxquestions.org/questions/linux-newbie-8/automatic-way-to-rename-files-4175504430/)

Siliskor 05-09-2014 04:20 AM

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.

eklavya 05-09-2014 05:24 AM

Try this
Quote:

$ rename 's/\[01-10-12\]\ /20121001_/g' /path/of/the/directory/*
Suppose your files are in /home/john/snaps, the command would be
Quote:

$ rename 's/\[01-10-12\]\ /20121001_/g' /home/john/snaps/*
It will rename all files and directories in which [01-10-12] is appeared in their filename.
There is a space character after [01-10-12]
If there is no space and files are like this
Quote:

[01-10-12]01
[01-10-12]02
[01-10-12]03
Use following command.
Quote:

$ rename 's/\[01-10-12\]/20121001_/g' /path/of/the/directory/*
FOLLOW grail's post 3rd one

grail 05-09-2014 06:30 AM

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_/'`
Now if we ignore what would happen with the square brackets, the fact that there is a space between ']' and '01.jpg' means that cp now sees this as 2 files and according to cp logic
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
do
  cp -p "$old" $(sed -r 's/.(..).(..).(..)[^0-9]*/20\3\2\1_/' <<< "$old")
done

Things I changed:

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)

Siliskor 05-10-2014 07:17 AM

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.

kishor joshi 05-10-2014 09:13 AM

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.

grail 05-10-2014 09:42 AM

Please remember to mark as SOLVED once you have a solution.


All times are GMT -5. The time now is 06:00 AM.