LinuxQuestions.org
Visit the LQ Articles and Editorials section
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 02-14-2005, 04:13 PM   #1
Apostasy
Member
 
Registered: Jun 2004
Location: Wisconsin
Distribution: Ubuntu
Posts: 31

Rep: Reputation: 15
Question directory renaming shell script?


In a pacticular directory of mine i have hundreds of subdirs most with ( and ) characters in the dir names... would it be possible to write a shell script to take those characters out? leaving the rest of the name intact?

example:
item-(year)-item
item-(year)-blah_blah
item_2-year-foobar

needs to be:
item-year-item
item-year-blah_blah
item_2-year-foobar

TIA

Last edited by Apostasy; 02-14-2005 at 04:16 PM.
 
Old 02-14-2005, 05:12 PM   #2
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
This should do it (there's more than one way to skin that rat):

Code:
for NAME in *; do
    NEWNAME=`echo $NAME | tr -d '(' | tr -d ')'`;
    mv $NAME $NEWNAME;
done
 
Old 02-14-2005, 06:38 PM   #3
Apostasy
Member
 
Registered: Jun 2004
Location: Wisconsin
Distribution: Ubuntu
Posts: 31

Original Poster
Rep: Reputation: 15
hey Thanks a lot! that saved me hours of manual labor lolol
 
Old 02-14-2005, 06:52 PM   #4
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
sigsegv,

Very nice! Do you have a good way to do sub directories also?
 
Old 02-14-2005, 08:21 PM   #5
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
Quote:
Originally posted by homey
sigsegv,

Very nice! Do you have a good way to do sub directories also?
Code:
for NAME in `find ./ -type d`; do
    NEWNAME=`echo $NAME | tr -d '(' | tr -d ')'`;
    mv $NAME $NEWNAME;
done
 
Old 02-14-2005, 08:38 PM   #6
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Quote:
for NAME in `find ./ -type d`; do
NEWNAME=`echo $NAME | tr -d '(' | tr -d ')'`;
mv $NAME $NEWNAME;
done
That fails to work for sub directories because the parent directory gets changed before the sub directory. I haven't figured this one out yet.

Here is another attempt which also fails to do sub directories....
Code:
for i in `ls -R` ; do
   j=`echo $i | tr -d '(' | tr -d ')'`
   mv $i $j
done
 
Old 02-14-2005, 10:15 PM   #7
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
Quote:
Originally posted by homey
That fails to work for sub directories because the parent directory gets changed before the sub directory. I haven't figured this one out yet.
Right you are ... That's what I get for posting code without testing it

Code:
$ cat dirfix.sh 
#!/bin/sh

for DIR in *; do
    if [ -d $DIR ]; then
        if ( echo $DIR | grep '(\|)' >> /dev/null ); then
            NEWNAME=`echo $DIR | tr -d '(' | tr -d ')'`
            mv $DIR $NEWNAME
            cd $NEWNAME
            $0
            cd -
        fi
    fi
done
The drawback to this being that you have to run the script with the full path to the script. If someone wants to work out a subroutine and the recursion, have right at it.

Last edited by sigsegv; 02-14-2005 at 10:23 PM.
 
Old 02-14-2005, 10:33 PM   #8
twantrd
Senior Member
 
Registered: Nov 2002
Location: CA
Distribution: redhat 7.3
Posts: 1,438

Rep: Reputation: 52
Very nice. Thanks! You are the man.

-twantrd
 
Old 02-14-2005, 11:40 PM   #9
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Thanks sigsegv,

That's a keeper.
 
Old 02-15-2005, 01:00 AM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
How about:
Code:
for NAME in `find ./ -type d | tac`; do
NEWNAME=`echo $NAME | tr -d '(' | tr -d ')'`;
mv $NAME $NEWNAME;
done
The 'tac' command will reverse all of the lines so the subdirs will come first.
 
Old 02-15-2005, 06:11 AM   #11
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Hi jschiwal,

Quote:
for NAME in `find ./ -type d | tac`; do
NEWNAME=`echo $NAME | tr -d '(' | tr -d ')'`;
mv $NAME $NEWNAME;
done
That doesn't seem to be doing the sub directories here.
 
Old 02-15-2005, 07:49 AM   #12
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
tac also isn't part of my BSD system, which means it doesn't exist as far as shell scripts are concerned.

If you can't count on something to be installed in the base system, why use it for shell scripts? At that point you're better off using python/perl.
 
Old 02-15-2005, 07:59 AM   #13
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
sigsegv,

I kinda wondered what is this all about....
Code:
if ( echo $DIR | grep '(\|)' >> /dev/null )
 
Old 02-15-2005, 12:53 PM   #14
sigsegv
Senior Member
 
Registered: Nov 2004
Location: Third rock from the Sun
Distribution: NetBSD-2, FreeBSD-5.4, OpenBSD-3.[67], RHEL[34], OSX 10.4.1
Posts: 1,197

Rep: Reputation: 46
That checks to see if the directory actually has a '(' or ')' in it's name. It's not strictly necessary, but it causes a bunch of errors to be printed if it's not there. If a directory is present and it doesn't have parentheses in it, the script will try to mv it from itself to itself, which usually results in "invalid operation" being printed on STDERR.

Make sense?
EDIT: Hrm ... After thinking about it, it also won't recurse into those directories to check for parentheses either. I'll leave that as a challenge for the others in the group.

Last edited by sigsegv; 02-15-2005 at 12:59 PM.
 
Old 02-15-2005, 02:03 PM   #15
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
Thanks sigsegv,

So how would your script look if I wanted to rename something more ordinary like this to remove the word test...

/testone/testtwo/testthree
/testfour/testfive/testsix
/testseven/testeight/testnine
/muddtest/housetest/farmtest

to look like this....

/one/two/three
/four/five/six
/seven/eight/nine
/mudd/house/farm

EDIT: I must have had a typo as it seems to work now. ....
Code:
#!/bin/sh

for DIR in *; do
    if [ -d $DIR ]; then
        if ( echo $DIR | grep 'test' >> /dev/null ); then
            NEWNAME=`echo $DIR | sed -e 's/test//g'`
            mv $DIR $NEWNAME
            cd $NEWNAME
            $0
            cd -
        fi
    fi
done

Last edited by homey; 02-15-2005 at 02:14 PM.
 
  


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
shell renaming script paskari Programming 3 09-24-2005 02:15 PM
Directory listing - Calling shell script from a CGI script seran Programming 6 08-11-2005 11:08 PM
file renaming with shell script or ? XJNick Linux - General 5 07-29-2005 02:43 PM
shell script: delete all directories named directory.# except directory.N brian0918 Programming 3 07-13-2005 06:54 PM
Need help with shell script - renaming multiple files NiallC Linux - Newbie 25 07-04-2004 10:45 AM


All times are GMT -5. The time now is 06:00 PM.

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