LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices

Reply
 
Search this Thread
Old 03-26-2010, 03:38 PM   #1
cupofnestor
LQ Newbie
 
Registered: Oct 2007
Posts: 27

Rep: Reputation: 0
Bash script rename files based on directory name


I'm pretty new to bash scripting, but I really want to wrap my head around it.

What I'm trying to do is: From directory "A": Go in to all subdirectories and rename all files within icrementally according to the directory name. SO:

|-- Varian
| |-- FB1-page132.pdf.png
| |-- FB1-page133.pdf.png
| |-- FB1-page134.pdf.png
| |-- FB1-page135.pdf.png

Becomes:

|-- Varian
| |-- Varian-1.png
| |-- Varian-2.png
| |-- Varian-3.png
| |-- Varian-4.png

I'm trying this script which I hacked together, but it is tripping over spaces in filename. Is there any way to retain the spaces? Or should I just replace them with underscores?



Code:
 #!/bin/sh
 for i in $(ls -d */ | awk '{print $0}'); do  
 		cd $i
 		for j in ls;
 		do
 			sName =$(ls  $i | awk '{print $i NR".png"}')
 			rename $j $sName
 		done
 		cd ../
 done
 
Old 03-26-2010, 04:02 PM   #2
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
try this instead:
Code:
#!/bin/sh

for i in $(ls -d */ | awk '{print $0}'); do  
  cd $i
  ifs=$IFS
  IFS='\
'
  for j in $(ls); do
    sName =$(ls  $i | awk '{print $i NR".png"}')
    rename $j $sName
  done
 IFS=$ifs
 cd ../
done
If you want more information you can read up on ifs (internal field separator) over here http://mindspill.net/computing/linux...haracters.html (note here i'm saving and reverting the IFS variable back in case it had something 'nonstandard' in it... if there's nothing special you can just do IFS='\[enter]' and unset IFS)

Last edited by rweaver; 03-26-2010 at 04:06 PM.
 
Old 03-29-2010, 08:27 AM   #3
cupofnestor
LQ Newbie
 
Registered: Oct 2007
Posts: 27

Original Poster
Rep: Reputation: 0
Great, hopefully that is exactly what I needed! I've already found the Field Separator in awk extremely helpful in my projects. It's the little things (like this) that make Linux so amazing... but I'll tell you a secret: I'm actually using OSX at the moment! Thanks for the reference as well: hopefully next time I won't have to ask.

Quote:
Originally Posted by rweaver View Post
try this instead:
Code:
#!/bin/sh

for i in $(ls -d */ | awk '{print $0}'); do  
  cd $i
  ifs=$IFS
  IFS='\
'
  for j in $(ls); do
    sName =$(ls  $i | awk '{print $i NR".png"}')
    rename $j $sName
  done
 IFS=$ifs
 cd ../
done
If you want more information you can read up on ifs (internal field separator) over here http://mindspill.net/computing/linux...haracters.html (note here i'm saving and reverting the IFS variable back in case it had something 'nonstandard' in it... if there's nothing special you can just do IFS='\[enter]' and unset IFS)
 
Old 03-29-2010, 08:29 AM   #4
i92guboj
Gentoo support team
 
Registered: May 2008
Location: Lucena, Córdoba (Spain)
Distribution: Gentoo
Posts: 4,036

Rep: Reputation: 372Reputation: 372Reputation: 372Reputation: 372
Please, take a look at this: http://mywiki.wooledge.org/ParsingLs

Parsing the output of ls will probably bring you trouble in the long term.
 
Old 03-30-2010, 08:10 AM   #5
cupofnestor
LQ Newbie
 
Registered: Oct 2007
Posts: 27

Original Poster
Rep: Reputation: 0
Hey, that's very interesting. I'd never thought about the pitfalls of ls, I'd just seen it done so many times: especially where awk is concerned. The pitfalls article is great as well, any other recommendations for a fledgling bash-er?

Quote:
Originally Posted by i92guboj View Post
Please, take a look at this: http://mywiki.wooledge.org/ParsingLs

Parsing the output of ls will probably bring you trouble in the long term.
 
Old 03-30-2010, 12:18 PM   #6
rweaver
Senior Member
 
Registered: Dec 2008
Location: Louisville, OH
Distribution: Debian, CentOS, Slackware, RHEL, Gentoo
Posts: 1,833

Rep: Reputation: 163Reputation: 163
Quote:
Originally Posted by i92guboj View Post
Please, take a look at this: http://mywiki.wooledge.org/ParsingLs

Parsing the output of ls will probably bring you trouble in the long term.
Actually using the ifs will solve most if not all the problems you typically run into with parsing ls... to be fair though, even the ifs is a bit of a kludge work-around (although useful in many areas.)

Find is definitely a better solution and using read isn't a bad idea either.
 
Old 03-30-2010, 09:08 PM   #7
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,239

Rep: Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024Reputation: 2024
As is also replacing spaces in filenames with underscores. The default in *nix is that params are space separated, so just about all the tools/cmds assume that. It'll definitely simplify your life to get rid of them. You'll notice that all the files (I've ever seen) issued as part of Unix do not have spaces in them...
 
Old 03-31-2010, 08:20 AM   #8
cupofnestor
LQ Newbie
 
Registered: Oct 2007
Posts: 27

Original Poster
Rep: Reputation: 0
Understood, I personally never use spaces. I'm working with someone else's data, on OSX. I'm generating XHTML from the filenames, too, so I'd have to change back to spaces later, anyway.

Quote:
Originally Posted by chrism01 View Post
As is also replacing spaces in filenames with underscores. The default in *nix is that params are space separated, so just about all the tools/cmds assume that. It'll definitely simplify your life to get rid of them. You'll notice that all the files (I've ever seen) issued as part of Unix do not have spaces in them...
 
  


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 On
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Bash script to rename photos in directory & all subdirectories shy_guest Linux - Software 7 09-02-2009 01:40 PM
Bash script to rename photos in directory & all subdirectories shy_guest Linux - Software 1 09-02-2009 05:28 AM
copy and rename the existing directory using BASH script neo2k Linux - Software 3 08-20-2008 10:43 AM
To rename files in a directory should I use Bash script or a Perl Script ? jamtech Programming 7 01-22-2008 11:25 PM
Bash script to traverse directory tree and rename files intramaweb Programming 3 10-08-2006 12:51 PM


All times are GMT -5. The time now is 11:46 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration