Welcome to the most active Linux Forum on the web.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 05-09-2014, 05:20 AM   #1
LQ Newbie
Registered: May 2014
Posts: 11

Rep: Reputation: Disabled
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...


Someone on a different forum gave me the following solution...

for OLD in *.jpg
cp -p $OLD `echo $OLD | sed 's/.\(..\)-\(..\)-\(..\). /20\3\2\1_/'`

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.
Old 05-09-2014, 06:24 AM   #2
Registered: Mar 2013
Posts: 633

Rep: Reputation: 141Reputation: 141
Try this
$ rename 's/\[01-10-12\]\ /20121001_/g' /path/of/the/directory/*
Suppose your files are in /home/john/snaps, the command would be
$ 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
Use following command.
$ rename 's/\[01-10-12\]/20121001_/g' /path/of/the/directory/*
FOLLOW grail's post 3rd one

Last edited by eklavya; 05-09-2014 at 09:20 AM.
Old 05-09-2014, 07:30 AM   #3
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,760

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
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:
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:
for old in *.jpg
  cp -p "$old" $(sed -r 's/.(..).(..).(..)[^0-9]*/20\3\2\1_/' <<< "$old")
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)
1 members found this post helpful.
Old 05-10-2014, 08:17 AM   #4
LQ Newbie
Registered: May 2014
Posts: 11

Original Poster
Rep: Reputation: Disabled
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.
Old 05-10-2014, 10:13 AM   #5
kishor joshi
Registered: Jan 2013
Location: Pune,India
Distribution: Linux Mint18.3,Cinnamon, 64-bit
Posts: 56
Blog Entries: 1

Rep: Reputation: Disabled
Thumbs up 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.

Last edited by kishor joshi; 05-10-2014 at 10:14 AM. Reason: Spelling corrections
Old 05-10-2014, 10:42 AM   #6
LQ Guru
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 9,760

Rep: Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050Reputation: 3050
Please remember to mark as SOLVED once you have a solution.


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
bash script automatic file rename if exists AutoC Programming 4 06-23-2015 04:31 PM
Application to eliminate doubles in files and rename changed files with date? S. Chapelin Linux - Software 6 01-16-2011 03:02 AM
Trouble with making a bash script to read in different files and rename output files. rystke Linux - Software 1 05-07-2009 09:00 AM
[SOLVED] automatic rename files mythcat Programming 11 10-20-2008 08:34 AM
text files and their automatic backup files AGazzaz Linux - General 1 07-01-2006 08:38 AM > Forums > Linux Forums > Linux - Newbie

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

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