LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
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 09-08-2009, 11:39 PM   #1
smaddox
LQ Newbie
 
Registered: Sep 2009
Posts: 11

Rep: Reputation: 0
Question Renaming directories with Regex? Possible?


I spent the last hour trying to figure out how to do what seems like it should be a simple directory rename.

I have the following directories in the CWD:

Code:
2008F - EE325K - Antennas and Wireless Propagation
2008F - EE348 - Lasers and Optical Engineering
2009F - EE396V - Physical Principles of Electronic Materials
2009S - EE383V - Electromagnetic Metamaterials
2009S - EE464 - Senior Design Project
other.random.directory.or.Some.File
And I want to rename them to:

Code:
2008 Fall - EE325K - Antennas and Wireless Propagation
2008 Fall - EE348 - Lasers and Optical Engineering
2009 Fall - EE396V - Physical Principles of Electronic Materials
2009 Spring - EE383V - Electromagnetic Metamaterials
2009 Spring - EE464 - Senior Design Project
other.random.directory.or.Some.File
(Note: this is just a representative sample)

The closest I got was the following command:

Code:
ls|grep 200|sed -e 's/2008F/2008 Fall/g' -e 's/2009F/2008 Fall/g' -e 's/2009S/2009 Spring/g'
but all this does is print out the new names. Is there a way to feed it and the prior name into mv?

Alternatively (possibly preferrably) is there a better way to do this? Consider it a puzzle =)

Thanks!
 
Old 09-09-2009, 12:39 AM   #2
kbp
Senior Member
 
Registered: Aug 2009
Posts: 3,790

Rep: Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653Reputation: 653
Something like:

Code:
for file in $(ls -1)
do
  if [[ "${file}" =~ '200[89][FS].*' ]]
  then
     newfilename=$(sed .... )
     mv $file $newfilename
  fi
done
(not tested at all)

btw.. thats a (one) not an (ell) in the first line

cheers

kbp
 
Old 09-09-2009, 01:38 AM   #3
Tinkster
Moderator
 
Registered: Apr 2002
Location: earth
Distribution: slackware by choice, others too :} ... android.
Posts: 23,067
Blog Entries: 11

Rep: Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928Reputation: 928
Quote:
btw.. thats a (one) not an (ell) in the first line
I don't think the -1 is actually needed ...


Cheers,
Tink
 
Old 09-09-2009, 06:48 PM   #4
smaddox
LQ Newbie
 
Registered: Sep 2009
Posts: 11

Original Poster
Rep: Reputation: 0
Thanks for putting me on the right direction! I learned a lot.

Your code didn't really work for a couple of reasons though.
1) The for loop uses spaces as field separators
2) the regex should not be in quotes unless you want it to be literal

I found the solution for the reverse problem (tested with echo in place of mv):

Code:
ls -1 | while read file; do 
  if [[ $file =~ (200[89])\ (Fall|Spring)(.*) ]]; then  
    mv "'$file' '${BASH_REMATCH[1]}`echo ${BASH_REMATCH[2]}|cut -c1`${BASH_REMATCH[3]}'";
  fi;
done
That was far more difficult than I expected, but I think next time I have something similar I might be able to do it fairly quickly. =)

Thanks again.
 
Old 09-10-2009, 02:15 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
Note that 'The for loop uses spaces as field separators' is typical for almost all *nix utils ie space is seen as an arg separator, that's why you don't usually see spaces in (pre-supplied) filenames.
Causes too much hassle.
 
Old 09-10-2009, 08:56 AM   #6
smaddox
LQ Newbie
 
Registered: Sep 2009
Posts: 11

Original Poster
Rep: Reputation: 0
What annoyed me was that neither escaping the spaces (ls -b), nor surrounding the filenames in quotes (ls -Q) worked. The for loop would still separate a the space. It seems to me that quotes should have worked since they aren't allowed in file names, and passed as arguments they would be there to group words.

Was there something I missed?
 
Old 09-11-2009, 01:57 AM   #7
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,358

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
I'd alter the IFS value to just a newline. By default its space or tab or newline.
You may need to restore it to the std values after the loop if you're going to do more processing in the script.
http://tldp.org/LDP/abs/html/internalvariables.html
 
Old 11-09-2009, 05:40 PM   #8
archtoad6
Senior Member
 
Registered: Oct 2004
Location: Houston, TX (usa)
Distribution: MEPIS, Debian, Knoppix,
Posts: 4,727
Blog Entries: 15

Rep: Reputation: 234Reputation: 234Reputation: 234
rename works on directories too. The only trick needed to keep it from changing the name of similarly named files seems to be using a trailing '/':
Code:
rename 's,(200.)F,$1 Fall,' 200*/
should do what you want. -- I tested on a recreation of your sample.
Lather, rinse, repeat for the other 3 seasons.

from the man page:
Quote:
SYNOPSIS
rename [ -v ] [ -n ] [ -f ] perlexpr [ files ]
Remember that the perlexpr is a perl expression, while the optional files entry uses bash globbing -- i.e. the wildcarding is totally different.

Final Note: Spaces in *nix file (& directory) names can cause problems -- it's a bad practice.
 
Old 11-09-2009, 06:27 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
Quote:
Originally Posted by kbp View Post
Something like:

Code:
for file in $(ls -1)
do
kbp
no need ls.....
Code:
for file in *
 
Old 11-09-2009, 06:34 PM   #10
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 244Reputation: 244Reputation: 244
if you have Python 2.4+ and don't mind, you can use use this script

usage:
Code:
$ ls -1
2008F - EE325K - Antennas and Wireless Propagation
2008F - EE348 - Lasers and Optical Engineering
2009F - EE396V - Physical Principles of Electronic Materials
2009S - EE383V - Electromagnetic Metamaterials
2009S - EE464 - Senior Design Project

$ filerenamer.py -t d -p "200([89])F" -e "200\1 - Fall" -l "200[89]F*"
=>>>>  [ /home/2008F - EE325K - Antennas and Wireless Propagation ]==>[ /home/2008 - Fall - EE325K - Antennas and Wireless Propagation ]
=>>>>  [ /home/2009F - EE396V - Physical Principles of Electronic Materials ]==>[ /home/2009 - Fall - EE396V - PhysicalPrinciples of Electronic Materials ]
=>>>>  [ /home/2008F - EE348 - Lasers and Optical Engineering ]==>[ /home/2008 - Fall - EE348 - Lasers and Optical Engineering ]
Do another time for spring
Code:
filerenamer.py -t d -p "200([89])S" -e "200\1 - Spring" -l "200[89]S*"
remove -l option to commit changes
 
  


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



Similar Threads
Thread Thread Starter Forum Replies Last Post
renaming directories from upper case to lower case, help!! linux_teller Linux - Newbie 3 03-07-2008 05:15 AM
regex with sed to process file, need help on regex dwynter Linux - Newbie 5 08-31-2007 05:10 AM
CHMOD directories.sub-directories.files zerojosh Linux - Software 2 11-19-2005 03:22 PM
Searching multiple directories and sub directories for a file jeep99899 Linux - Newbie 2 10-13-2005 12:23 PM
dumb newbie question about renaming directories Lleb_KCir Linux - General 2 10-26-2004 08:37 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 08:12 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