Mass Renaming Of Folders

General Discussion of atomic repo and development projects.

Ask for help here with anything else not covered by other forums.
NightStorm
Forum User
Forum User
Posts: 60
Joined: Sun Dec 05, 2004 4:16 am

Mass Renaming Of Folders

Unread post 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?
breun
Long Time Forum Regular
Long Time Forum Regular
Posts: 2813
Joined: Sat Aug 20, 2005 9:30 am
Location: The Netherlands

Unread post 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.)
Lemonbit Internet Dedicated Server Management
NightStorm
Forum User
Forum User
Posts: 60
Joined: Sun Dec 05, 2004 4:16 am

Unread post by NightStorm »

That worked wonderfully. Thank you :)
Post Reply