Rename some of the files in a directory tree

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

I would like to rename on file ending with *.*_old to *.*

It work on the current directory well using:
for a in *_old; do mv "$a" "$(echo "$a" | sed 's/_old//')";

How do I expand it so that it will rename all the file in the subdirectories?

Question by:addady
    LVL 16

    Accepted Solution


    I am not 100% sure what you want to rename it to bu you could try:

    for dir in `find /home/damian/test -type d -name \*_old`; do mv $dir `echo $dir | sed -e 's/_old//g'`; done

    Of course you will want to replace /home/damian/test with the path you need.

    Author Comment


    Thank you it works. you hace small syntax errors. Here is the rigth command:

    for dir in `find /home/damian/test  -iname \*_old`; do mv $dir `echo $dir | sed -e 's/_old//'`; done

    Its not work.  For example:

    #find /tmp -iname \*.html

    #for dir in `find /tmp -type d -iname \*.html`; do mv $dir `echo $dir | sed -e 's/.html/.html-old/g'`; done

    Author Comment

    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.

    Author Comment

    Let me be more spcific:

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

    The directory name contain space

    #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 16

    Expert Comment


    First at the command line type


    Notice after the quotes I hit enter then on that new I added the closing quote now 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

    Should work fine.

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    6 Surprising Benefits of Threat Intelligence

    All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

    Suggested Solutions

    This is the error message I got (CODE) Error caused by incompatible libmp3lame 3.98-2 with ffmpeg I've googled this error message and found out sometimes it attaches this note "can be treated with downgrade libmp3lame to version 3.97 or 3.98" …
    Over the last ten+ years I have seen Linux configuration tools come and go. In the early days there was the tried-and-true, all-powerful linuxconf that many thought would remain the one and only Linux configuration tool until the end of times. Well,…
    Connecting to an Amazon Linux EC2 Instance from Windows Using PuTTY.
    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.

    779 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