Link to home
Start Free TrialLog in
Avatar of Oramcle
Oramcle

asked on

Rename Multiple file in Unix

I have accidentally Rename more than 100 files in one Folder in Unix  like

File_Namee to   July_2016_FIle_name  Like all the file prefixed to same String "july_2016_"   How can I Reverse this action through Script?

Thank you Very Much.
Avatar of MURUGESAN N
MURUGESAN N
Flag of India image

@Oramcle
Related code:
Updated script including exceptions:
#!/bin/bash
if test 0 -eq $#
then
	echo "Usage:"
	echo "$0 RequiredDirectory"
	echo -e "RequiredDirectory pointing the directory where you need to rename all files\nFrom: July_2016_OriginalFileName\nTo:   OriginalFileName"
elif test ! -f /usr/bin/find
then
	echo "Update this script to use full path for find command"
elif test ! -f /bin/sed
then
	echo "Update this script to use full path for sed command"
elif test ! -f /bin/mv
then
	echo "Update this script to use full path for mv command"
else
	FIND="/usr/bin/find"
	SED="/bin/sed"
	MV="/bin/mv"
	if test "/" = "$1"
	then
		echo "Are you going to search all files in this system from following directory:"
		echo "$1"
		echo "Press enter two times to continue or Ctrl C to quit"
		read
		echo "Press enter second time or Ctrl C to quit"
		read
		echo "Wait till $FIND finish searching July_2016* files."
	fi
	cd "$1"
	FOUND_JULY_2016_FILES=0
	for files in ''`$FIND "$1" -type f -name "July_2016*" | $SED "s/ /REPLACE_SPACE_EXCEPTION/g;"`''
	do
		if test "" != "$files"
		then
			FOUND_JULY_2016_FILES=1
			CurrFile=''`echo -n "$files" | $SED "s/REPLACE_SPACE_EXCEPTION/ /g;"`''
			OriginalFileName=''`echo -n "$CurrFile" | $SED "s/July_2016_//;"`''
			echo "$MV -i \"$CurrFile\" \"$OriginalFileName\""
			$MV -i "$CurrFile" "$OriginalFileName"
		fi
	done
	if test 0 -eq $FOUND_JULY_2016_FILES
	then
		echo "Unable to find July_2016* files in $1 directory"
	fi
fi

Open in new window

Sample output:
$ ./29062928.sh /
Are you going to search all files in this system from following directory:
/
Press enter two times to continue or Ctrl C to quit

Open in new window


$ ./29062928.sh $PWD
/bin/mv -i "/home/Murugesandinesh/29062928/asd/July_2016_FIle_name" "/home/Murugesandinesh/29062928/asd/FIle_name"
/bin/mv -i "/home/Murugesandinesh/29062928/asd1/July_2016_FIle_name" "/home/Murugesandinesh/29062928/asd1/FIle_name"
/bin/mv -i "/home/Murugesandinesh/29062928/July_2016_FIle_name" "/home/Murugesandinesh/29062928/FIle_name"
/bin/mv -i "/home/Murugesandinesh/29062928/July_2016_FIle _name " "/home/Murugesandinesh/29062928/FIle _name "

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Hanno P.S.
Hanno P.S.
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Hi Oramcle,

Reason for using script:

1. Validate existence of related commands (/bin/mv /usr/bin/find....
2. While renaming using /bin/mv -i for handling exception: if destination file is present
3. Handle exception to /usr/bin/find at related directory instead of / => performance and related path
4. If already renamed, display related alert message "Unable to find July_2016.....
5. Same script can be used if facing similar issue (which can be used to enhance or modify based on current or new requirement)
6. Using /usr/bin/find command for searching all related files at required directory and it's sub directories
You can use following command to handle exceptions:
/usr/bin/find ..... 2>/dev/null
Using the "quick" approach you can learn a lot how Unix sytems work -- end even learn to use powerful renaming tools like "RegExRenamer" on Windows ;-)
Anyway, it always depends on the level of experience a user has ...
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Oramcle
Oramcle

ASKER

Thank you Experts.
Oramcle.