Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

How to go through all the subfolders and rename all the folders named 'Old' to 'New'

share|improve this question

2 Answers 2

If you have zsh:

autoload zmv
zmv -QW '**/Old(/)' '**/New'
zmv -W '**/Old' '**/New'

Here autoload zmv loads zmv script that comes with zsh; zmv -W pattern replacement means «for each match remember value of all wildcards in pattern and replace corresponding wildcard characters in replacement with this value»; zmv -Q means «allow glob qualifiers in pattern»; '**/Old' means «match all files named «Old» in the tree under current directory» and (/) glob qualifier restricts «all files» to be only directories. Other useful zmv options:

  • -n: do not do anything, just print what will be done.
  • -i: show each line to be executed and ask whether to execute it
  • -f: force overwriting of destination files

If you prefer [ba]sh:

RnAll() { for f in "$1"/* ; do [ -d "$f" ] || continue ; ( RnAll "$f" "$2" "$3" ) ; [ "`basename $f`" \== "$2" ] && mv "$f" "`dirname $f`/$3" ; done }
RnAll . Old New

This is almost pure sh (or at least bash) solution, that does not require anything but shell and coreutils.

share|improve this answer

Something like this:

find . -type d -exec rename <rename options> \;

The rename options will vary depending on which rename you have. Red Hat rename uses rename Old New {}, whereas Debian/Ubuntu systems often use "Perl rename", which would use this syntax:

rename 's/^Old$/New/' {} 

That will only change exact matches for "Old"; use s/Old/New/g if you want to change all occurrances of "Old" to "New", including multiple occurrances within one foldername.

share|improve this answer
    
Debian/Ubuntu systems often use "Perl rename", which would use this syntax: rename 's/^Old$/New/' {} ... that will only change exact matches for "Old"; use s/Old/New/g if you want to change all occurrances of "Old" to "New", including multiple occurrances within one foldername. –  quack quixote Jun 11 '10 at 20:08

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.