LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
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 05-09-2014, 04:20 AM   #1
Siliskor
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...

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

Rep: Reputation: 142Reputation: 142
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

Last edited by eklavya; 05-09-2014 at 08:20 AM.
 
Old 05-09-2014, 06:30 AM   #3
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

Rep: Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191Reputation: 3191
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)
 
1 members found this post helpful.
Old 05-10-2014, 07:17 AM   #4
Siliskor
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, 09:13 AM   #5
kishor joshi
Member
 
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 09:14 AM. Reason: Spelling corrections
 
Old 05-10-2014, 09:42 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,007

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


Reply



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

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 01:10 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration