[GLLUG] Changing the case in file names

Scott Henry Harrison harris41 at msu.edu
Thu Jun 30 10:44:05 EDT 2005


> -----Original Message-----
> From: Seth Bembeneck [mailto:sbdataspiller at sbcglobal.net]
> Is there a quick way to rename several files in a directory and
> subdirectories so that the file names are in lower case, and also replacing
> all spaces with _'s?

Hampton, Rodney writes:
> ls|perl -e 'while(<STDIN>){chomp $_;$ofn=$_;$nfn=lc($ofn);$nfn=~s/
> /_/g;print "new_file_name is $nfn \n";$exec_string="mv $ofn $nfn";print
> "exec_string is $exec_string \n";system($exec_string);}'
 

To also get to subdirectories,
you can use Rodney's invocation, except
you would also want to substitute "ls |" with
"find . -type f |".  (And then, you may
want to adjust the command-line perl script to
only modify filenames, not directory
names (if, for example, the directory names contain spaces
and uppercase characters)). 


If the subdirectories are deep, check
out the -maxdepth option to find (man find). 


Finally... any time you are recursively traversing and
modifying your filesystem with a prototyped script,
a backup is highly recommended (and also
first sanity testing your script). 


Regards,
Scott 





More information about the linux-user mailing list