Rename Files in Bulk from the Command Line

Posted on July 6th, 2008 in Tech Tips by gmendoza

Renaming a large number of files can seem like a daunting task, but no worries, your trusty Linux CLI is at your service. For this example, we will rename a number of MP3’s located in multiple subdirectories with a couple very easy commands; “find” and “rename”.

By listing the following directory, you’ll see that the MP3’s have been named with “(LP Version)”, and of course I don’t like this naming convention.

$ cd ~/Music/Metallica/Metallica/
$ ls -1
01 - Enter Sandman (LP Version).mp3
02 - Sad But True (LP Version).mp3
03 - Holier Than Thou (LP Version).mp3
04 - The Unforgiven (LP Version).mp3
05 - Wherever I May Roam (LP Version).mp3
06 - Don't Tread On Me (LP Version).mp3
07 - Through The Never (LP Version).mp3
08 - Nothing Else Matters (LP Version).mp3
09 - Of Wolf And Man (LP Version).mp3
10 - The God That Failed (LP Version).mp3
11 - My Friend Of Misery (LP Version).mp3
12 - The Struggle Within (LP Version).mp3

We’ll use the “rename” command to search for and delete the string ” (LP Version)” in any of the mp3 file names.

Syntax:

$ rename (search command) (files)
$ rename 's/search_for_string/replace_string_with_this/' files

To delete the matching string, simply leave the replace area empty like so:

$ rename 's/search_for_string//' files

Our Example:

$ rename 's/ \(LP Version\)//' *.mp3

Notice, the left and right parentheses need to be preceded with a backslash “\” character, although the spaces do not. The backslash is a metacharacter used to give you control over what your are matching against. For more info, here’s a link to a decent tutorial on the matter.

You can see the results of the command below.

$ ls -1
01 - Enter Sandman.mp3
02 - Sad But True.mp3
03 - Holier Than Thou.mp3
04 - The Unforgiven.mp3
05 - Wherever I May Roam.mp3
06 - Don't Tread On Me.mp3
07 - Through The Never.mp3
08 - Nothing Else Matters.mp3
09 - Of Wolf And Man.mp3
10 - The God That Failed.mp3
11 - My Friend Of Misery.mp3
12 - The Struggle Within.mp3

Now, to rename a large number of files spanning multiple directories, simply combine “rename” with the power of the “find” command.

Syntax:

find . -type f -name *.mp3 -exec rename 's/ \(LP Version\)//' '{}' \;

In this example, we searched starting from the current directory for only files with .mp3 in their file names. We use the find command’s -exec option to execute the rename command against the result set. See the find(1) manpage for more info.

Other useful examples:

Replace all spaces with underscores
rename 's/ /\_/g' *.mp3

Replace all uppercase with lowercase characters
rename 'y/[A-Z]/[a-z]/' *.mp3

Easy stuff, and you don’t even need any fancy applications to do the job!

Vim in Color

Posted on September 30th, 2007 in Tech Tips by gmendoza

Turing on colored syntax highlighting in Vim can make it easier when looking through complicated text files, scripts, and source code. To quickly turn on or off the feature, use the “syntax” command within Vim:

:syntax on
:syntax off

The results are great. Here’s a screenshot of Vim in color.

Vim in Color

To make the feature permanent, edit the file /etc/vim/vimrc, or as your Vim package maintainer may suggest, /etc/vim/vimrc.local. One can also make the change simply for their user only by editing ~/.vimrc. In all cases, simply add the following line at the end of the file:

:syntax on

Users of Ubuntu by default have vim-tiny, which as it’s name suggests, is a smaller version of the Vim editor. This version does not support syntax highlighting and a number of other features. Of course, remedying this is very easy by installing the full featured vim:

# sudo apt-get install vim

Happy editing.

Grep in Color

Posted on September 2nd, 2007 in Tech Tips by gmendoza

If you would like to make it easier to visually spot what you are looking for when using “grep”, try out the “–color” flag. This option highlights any matches in the output of your search, giving you an upper hand when trying to visually scan through complicated or cumbersome sequences.

For example, if you were looking for any IP address in your /etc/hosts.deny file that has the number “209″ in it, issue the following command:

$ grep --color 209 /etc/hosts.deny

Here is the comparison of the same output, with and without the color option.

Grep with no color Grep with color

As you can see, one might find it very easy to miss the fact that there are some IP addresses that have multiple octets with the value of 209. The color flag really comes in handy.

Split and Reassemble Files

Posted on June 3rd, 2007 in Tech Tips by gmendoza

If you ever need to work with a large file and wish you could split it into smaller pieces, you’ll be pleased to know that it’s extremely easy to do in Linux. You can use the “split” utility that comes standard with most *nix variations. Lets take a look at a couple easy examples.

To create a test file to work with, the following will create one that’s exactly 100 megabytes. Note, I am using ‘dd’ with /dev/urandom to demonstrate that the results of the split and reassembly are completely accurate. This will be accomplished via md5 hash comparisons at the end of this process.

Useful APT Aliases

Posted on June 1st, 2007 in Tech Tips by gmendoza

If you’re an avid user of Ubuntu or other Debian based Linux distributions, then you’re probably very familiar with using APT and it’s related command line utilities. You might however find it useful to create some command line aliases that shorten the time it takes to type out these repetitive tasks.

For example,

“sudo apt-get update” can be shortened to “agu”.
“sudo apt-get install” can be shortened to “agi”.
“sudo apt-get dist-upgrade” can be shorted to “agd”.