Breaking News
Home / Sys Admin / Unix / How to rename multiple files, convert to lower case and remove spaces?

How to rename multiple files, convert to lower case and remove spaces?

Covert files names to lower case

Let’s say you have bunch of Audio files that you have ripped from the CD or bought online or recorded own you own. Normally these file names are in proper case with spaces separating the words e.g. Jamba Music.mp3 or Till You Live.mp3 etc.

 

<!>

This is better for readability but not optimized for posting on website. So converting these filenames to lowercase with hypen seprated manually is not easy since there can be hundreds or more of such files. The following Perl script can do the job faster and easier. This script can be run from Unix/Linux or Cygwin (Windows) terminal in the directory where the files are stored. So go the directory where your audio files are stored and then follow these steps.

Copy this script into any file and name it perl_rename.sh

#!/usr/bin/perl
#
# Usage: rename perlexpr [files]

($regexp = shift @ARGV) || die “Usage:  rename perlexpr [filenames]n”;

if (!@ARGV) {
   @ARGV = <STDIN>;
   chomp(@ARGV);
}

foreach $_ (@ARGV) {
   $old_name = $_;
   eval $regexp;
   die $@ if $@;
   rename($old_name, $_) unless $old_name eq $_;
}

exit(0)

 

Copy the following in another file , say named fix_case.sh

./perl_rename.sh ‘y/A-Z/a-z/’ *

 

And finally copy the following in another file , say named fix_name.sh

./perl_rename.sh  ‘s/ /-/g’ *

 

Now make sure you can execute all these files by changing the permissions as:

chmod +x *.sh

 

Ex. the your directory consists of the following files whose names need to be updated.

Jamba Music.mp3

Till You Live.mp3

Bon Jon Rock.mp3

Now run these 2 Scripts one after the other. The order in you run does not matter, the end result is the same.

./fix_name.sh

./fix_case.sh

 

Check your directory and you should see.

jamba-music.mp3

till-you-live.mp3

bon-jon-rock.mp3

All these files are converted to lower case, with space replaced with hyphens. Upload these files to your web server and you are done.


[pb_builder]

Check Also

How to list disks information in Linux?

In order to check the disk in the system we can use the List Hardware …

Leave a Reply