Little batch script to rename directories

Posted on 2004-11-14
Last Modified: 2012-08-14
Hi folks,

I'd like to rename some special folders on my harddisk. All the folders are looking this way:

[PPP] - ( this text is variable ) - [PPP]

Now I want to exchange all the [PPP] with [TTT] for example, the rest should be left untouched. With my very basics in batch programming I made the following script:

for FILE in "`find / -type d -name "?PPP]*" -print`"
        OLDNAME=`echo -n "$FILE"`
        NEWNAME=`echo -n "$FILE" | sed "s/PPP/TTT/g"`
        mv $OLDNAME $NEWNAME

Unfortunately, this seems not to work. When I do an "echo $OLDNAME" then I get a string with all the oldnamed directories without \n (newline).

Thanks for every helpful input in this :-)
Question by:Smudo
    LVL 33

    Expert Comment

    try this

    for FILE in `find / -type d -name "?PPP]*" -print`
    LVL 1

    Author Comment

    The header must be between quotes "". If it's not, it seems that the spaces in the dir-names are interpreted individually, means that mv gets parameters like




    When I use the quotes, I get the following output, when I add an echo $OLDNAME and echo $NEWNAME on the next line:

    ./test/[PPP] - ( blu blu ) - [PPP]
    ./test/[PPP] - ( blo blo ) - [PPP]
    ./[PPP] - ( blah blah ) - [PPP]
    ./test/[TTT] - ( blu blu ) - [TTT]
    ./test/[TTT] - ( blo blo ) - [TTT]
    ./[TTT] - ( blah blah ) - [TTT]
    mv: when moving multiple files, last argument must be a directory
    Try `mv --help' for more information.

    It's strange that all the [PPP] folders are listed on top. Shouldn't they be in order

    ./test/[PPP] - ( blu blu ) - [PPP]
    ./test/[TTT] - ( blu blu ) - [TTT]
    etc. ?

    LVL 48

    Accepted Solution

    Your problem is with quotes, or more specifically the lack of them.  If you have spaces in a string, you must quote then in order for it to be seen as a single string.  Also, using a for loop means that elements are split on whitespace (by default), so each part of the directory is split up.  You can use a for loop if you change the IFS value, but I think using a while loop is easier:

    find / -type d -name "*\[PPP]*" -print` | while read dir
       newdir=`echo "$dir" | sed 's/PPP/TTT/g'`
       mv "$dir" "$newdir"
    LVL 7

    Assisted Solution

    one-liner with perl's help:

    find /  -name "\[PPP\]*\[PPP\]"|perl -ne'chomp;$o=$_;s/PPP/TTT/g;print "mv \"$o\" \"$_\"\n";'|sh
    LVL 1

    Author Comment

    Great job! Both worked, Tintin was a bit faster :-)

    I just hate to work with spaces in filenames/directories.

    Featured Post

    Highfive Gives IT Their Time Back

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    Join & Write a Comment

    Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
    The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
    To add imagery to an HTML email signature, you have two options available to you. You can either add a logo/image by embedding it directly into the signature or hosting it externally and linking to it. The vast majority of email clients display l…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

    754 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    21 Experts available now in Live!

    Get 1:1 Help Now