Welcome to my site. Please CLICK HERE to give your opinions regarding this new look of "PCTipsbyAnu". Thanks for visiting.

Tuesday, December 21, 2010

Browse » Home » , , , , » How to make a script for renaming files names?

How to make a script for renaming files names?

Here i post a script which gives one of the best list of answers to how to rapidly rename all files in a directory tree to lower case.

Pure bash form

A concise version using "rename" command.
find my_root_dir -depth -exec rename 's/(.*)\/([^\/]*)/$1\/\L$2/' {} \;
This avoids problems with directories being renamed before files andtrying to move files into non-existing directories (e.g. "A/A" into"a/a").
Or, a more verbose version without using "rename".
for SRC in `find my_root_dir -depth`
DST=`dirname "${SRC}"`/`basename "${SRC}" | tr '[A-Z]' '[a-z]'`
if [ "${SRC}" != "${DST}" ]
[ ! -e "${DST}" ] && mv -T "${SRC}" "${DST}" || echo "${SRC} was not renamed"

Perl Script

For us perl lovers: Using Larry Wall's filename fixer
$op = shift or die $help;
chomp(@ARGV = <STDIN>) unless @ARGV;
for (@ARGV) {
$was = $_;
eval $op;
die $@ if $@;
rename($was,$_) unless $was eq $_;
it's as simple as
find | fix 'tr/A-Z/a-z/'
(where fix is of course the script above)
Read the article for other options, and visit the web site for other problems with solutions.
Other Solutions:

ls | ./fix.pl 'tr/\x80-\xFF//d' # remove control characters
ls | ./fix.pl 'tr/+&#()/_/d' # get rid of some really not nice filename characters
ls | ./fix.pl 's/_+/_/g' # replace duplicate underscores with one single underscore

You can leave a response, or trackback from your own site.

About 'Anu': My name is 'Anu' also Known as 'ANU 007 TIGER' .I'm administrator of 'PC Tips by Anu' blog .This blog was opened for sharing contents about hacking n cracking.


Post a Comment

Back to Top