LinuxQuestions.org
Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
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 10-27-2014, 03:49 AM   #1
aerospades
LQ Newbie
 
Registered: Oct 2014
Posts: 6

Rep: Reputation: Disabled
Need help creating a batch renaming script.


Heya, guys!

I have a /ton/, and I mean a tonof photos that I have to rename, and I was wondering if there was a way to automate the process of removing the first 6 digits of every filename in a folder?

I'm using Raspbian, strangely enough, if that makes any difference!

Thanks in advance, all! <3
 
Old 10-27-2014, 04:32 AM   #2
nisagnel
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Centos
Posts: 28

Rep: Reputation: 2
Hi aerospades,

You can use the following bash script to remove first 6 letters of all files in a directory.just change the directory path with your path in the rename_dir variable.
run the script from anywhere (if you run the script in the same directory,which you want to rename the files,it will rename this script also..i think)

#!/bin/bash
rename_dir="/root/Desktop/ton"
for file in `ls -A $rename_dir`
do
newfile=`echo $file | sed 's/^......//g'`
mv $rename_dir/$file $rename_dir/$newfile
done

Hope this will help you.
 
1 members found this post helpful.
Old 10-27-2014, 05:06 AM   #3
aerospades
LQ Newbie
 
Registered: Oct 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Thanks a bunch!

Just out of curiosity, what part of it dictates how many letters are removed?

and I'm guessing that this moves the files around during the process. Is there a way to leave them where they are and just process the names? (Super limited space, big files)

Again, thanks c:
 
Old 10-27-2014, 05:38 AM   #4
aerospades
LQ Newbie
 
Registered: Oct 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Also, it seems to be having a spot of trouble with files that have a space in the filename!

Any way to tackle this? o:
 
Old 10-27-2014, 05:44 AM   #5
pan64
LQ Guru
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 9,587

Rep: Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816Reputation: 2816
yes, you will find a simple solution, just remember: you need not use that construct: for file in `ls -A $rename_dir` but
Code:
ls -A $rename_dir | while read file
do
....
done
 
Old 10-27-2014, 05:59 AM   #6
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Re post #3: each '.' represents 1 'character' at that position. The '^' means match starting at the beginning of the string in qn.
Incidentally, 'mv' is actually the Linux rename cmd, so its an in-place cmd .

sed http://www.grymoire.com/Unix/Sed.html
 
1 members found this post helpful.
Old 10-27-2014, 06:04 AM   #7
aerospades
LQ Newbie
 
Registered: Oct 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Argh, I'm still not getting it, and it's spitting out errors ;__;

I can't get it to stop herniating over filename spaces D:

What would the complete script be? ;__;
 
Old 10-27-2014, 08:18 AM   #8
nisagnel
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Centos
Posts: 28

Rep: Reputation: 2
Hi aerospades,

I will try and let you know.
 
Old 10-27-2014, 09:16 AM   #9
aerospades
LQ Newbie
 
Registered: Oct 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Awesome!
 
Old 10-27-2014, 09:55 AM   #10
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,520

Rep: Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106
A simpler approach that will also work on filenames with spaces:

Code:
for i in /root/Desktop/ton/*.jpg; do
   mv "$i" "${i:6}"
done
If you want to test it before you run it (always a good idea), put "echo" in front of the mv, eg:
Code:
echo mv "$i" "${i:6}"
Then make sure it's behaving as expected before removing the echo and running it for real.

Last edited by suicidaleggroll; 10-27-2014 at 09:57 AM.
 
1 members found this post helpful.
Old 10-27-2014, 09:56 PM   #11
aerospades
LQ Newbie
 
Registered: Oct 2014
Posts: 6

Original Poster
Rep: Reputation: Disabled
Hey, guys!

Finally got it working with:

Code:
 

rename_dir="/root/Desktop/ton"

#Change the for-loop separator to handle spaces:
OLDIFS=$IFS
IFS=`echo -en "\n\b"`

for file in `ls -A $rename_dir`; do
	newfile=`echo $file | sed 's/^......//g'`
	mv "$rename_dir/$file" "$rename_dir/$newfile"
done

#Restore IFS:
IFS=$OLDIFS

Thanks for the help! c:
 
Old 10-28-2014, 03:02 AM   #12
nisagnel
LQ Newbie
 
Registered: Mar 2010
Location: India
Distribution: Centos
Posts: 28

Rep: Reputation: 2
Glad that you have solved.
And thanks for sharing it.
 
Old 10-29-2014, 02:41 AM   #13
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,396

Rep: Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395Reputation: 2395
Re filename space(s): in *nix generally, the default convention for any prog/cmd is that input params are space separated... hence your problem.
So, spaces-in-names are syntactically legal, but, as you've seen, not advised

Frankly I've never seen one in any file supplied by a *nix supplier; either OS or App and all(?) experienced *nix techs avoid them like the plague (where possible).
 
Old 10-29-2014, 07:53 AM   #14
suicidaleggroll
LQ Guru
 
Registered: Nov 2010
Location: Colorado
Distribution: OpenSUSE, CentOS
Posts: 5,520

Rep: Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106Reputation: 2106
Quote:
Originally Posted by chrism01 View Post
Frankly I've never seen one in any file supplied by a *nix supplier; either OS or App and all(?) experienced *nix techs avoid them like the plague (where possible).
The only one I can think of is VirtualBox, they throw spaces all over the place.
 
  


Reply


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
Creating a batch of new users and adding ssh keys using a script mattjjs Linux - Newbie 5 02-13-2014 10:46 AM
batch renaming folders goltoof Linux - Newbie 5 11-13-2013 12:54 AM
Batch file renaming Eddie Adams Linux - Newbie 13 08-12-2010 02:56 PM
help in batch renaming aapkae Linux - Newbie 5 02-13-2008 06:49 PM
renaming batch of files linux_ub Linux - Newbie 6 10-27-2004 09:41 PM

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

All times are GMT -5. The time now is 03:00 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration