Rename some of the files in a directory tree v2

Posted on 2007-08-01
Last Modified: 2010-04-20
Regarding my question:

I'm still have problem with space in file name.  the mv command is break :(

Change it to:

find /home/damian/test  -iname \*_old`; do mv "$dir" `echo "$dir" | sed -e 's/_old//'`; done
find /home/damian/test  -iname \*_old`; do mv \"$dir\" `echo \"$dir\" | sed -e 's/_old//'`; done
didn't solve it.
Question by:addady
    LVL 43

    Accepted Solution

    either set

    note the newline, You are setting IFS to newline character


    find /whatever | while read dir; do
     dirn=$( echo "$dir" | sed -e 's/_old//' )
     mv "$dir" "$dirn"

    rename "_old" "" *_old

    Author Comment

    Let me be more specific:

    #find /tmp/ -iname \*.genc__12-24__26-07-2007                
    /tmp/051008 - Elt/P1010039.JPG.genc__12-24__26-07-2007
    /tmp/051008 - Elt/P1010037.JPG.genc__12-24__26-07-2007
    /tmp/051008 - Elt/P1010036.JPG.genc__12-24__26-07-2007

    The directory name contain space

    The command:
    #for FILE in "`find /tmp/ -iname \*.genc__12-24__26-07-2007`"; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done

    fail because of the space in the directory name.

    Please provide example.

    LVL 43

    Expert Comment

    I already answered You Q.

    Example 1
    for FILE in `find /tmp/ -iname \*.genc__12-24__26-07-2007`; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done

    Example 2
    find /tmp/ -iname \*.genc__12-24__26-07-2007 | while read FILE; do
     NEW=$( echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/' )
     mv "$FILE" "$NEW";

    Example 3
    rename .genc__12-24__26-07-2007 "" /tmp/051008\ -\ Elt/*.genc__12-24__26-07-2007
    LVL 34

    Expert Comment

    by:Duncan Roe
    At work I found a sed command right after the find did it for me:

    for FILE in "`find /tmp/ -iname \*.genc__12-24__26-07-2007|sed - e 's/ /\\ /g'`"; do NEW=`echo $FILE | sed -e 's/.genc__12-24__26-07-2007/.genc/'`;mv "$FILE" "$NEW"; done

    works for any number of spaces, including consecutive

    Author Comment


    Hello ravenpl: and

    Example 1 work (Accept as solution)

    Example 2 did some of the job. it wirk well on dir like:
    "/tmp/051008 - Eilat"
    but fail on dir like:
    "/tmp/Tomer - Yom Huledet  5/"
    The error:
    mv: cannot move `/tmp/Tomer - Yom Huledet  5/Thumbs.db.genc__12-24__26-07-2007' to `/tmp/Tomer - Yom Huledet 5/Thumbs.db.genc': No such file or directory

    Example 3
    Why the subdir name is part of the command? it suppose to work on all /tmp

    If fail on:
    sed: -e expression #1, char 1: unknown command: `-'
    mv: cannot stat `': No such file or directory

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    Suggested Solutions

    I am a long time windows user and for me it is normal to have spaces in directory and file names. Changing to Linux I found myself frustrated when I moved my windows data over to my new Linux computer. The problem occurs when at the command line.…
    Little introduction about CP: CP is a command on linux that use to copy files and folder from one location to another location. Example usage of CP as follow: cp /myfoder /pathto/destination/folder/ cp abc.tar.gz /pathto/destination/folder/ab…
    Learn how to get help with Linux/Unix bash shell commands. Use help to read help documents for built in bash shell commands.: Use man to interface with the online reference manuals for shell commands.: Use man to search man pages for unknown command…
    Learn how to find files with the shell using the find and locate commands. Use locate to find a needle in a haystack.: With locate, check if the file still exists.: Use find to get the actual location of the file.:

    759 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

    10 Experts available now in Live!

    Get 1:1 Help Now