Page 1 of 1

Mass Renaming Of Folders

Posted: Sat Oct 06, 2007 5:47 pm
by NightStorm
So, say I have a folder containing some 500 subfolders... all of these subfolders have a common string to their name... specifically, the names all start with a # sign.
Hypothetically, say I want to remove the # from the front of each and every one of these folder names.
Anyone know of a script that could perform this function? It can be bash, or php, or whatever... I honestly don't care, so long as it gets the # sign out of the front of the name.
Ideas?

Posted: Sat Oct 06, 2007 6:17 pm
by breun
There's probably a million ways to do this, but I think this will work:

Code: Select all

#!/bin/bash
#
# This script removes all hash signs (#) from the beginning of directories and files under the current working directory

for NAME in *; do
        FIXEDNAME=`echo $NAME | sed 's/^\#//'`
        if [ "$NAME" != "$FIXEDNAME" ]; then
                echo "Renaming $NAME to $FIXEDNAME"
                mv $NAME $FIXEDNAME
        fi
done
There are probably also specialized rename programs available. Use at your own risk. Run it in the parent directory of the dirs you want to fix.

(You could comment out the mv command first to see if this script will do exactly what you want it to do.)

Posted: Sat Oct 06, 2007 6:39 pm
by NightStorm
That worked wonderfully. Thank you :)