Batch rename expression to remove X leading characters from files of a specific file type.

Darkness
  6 years ago
  5

This tutorial describes how to batch rename files of a specific file type by removing X number of leading characters from the beginning of the filename.

A. Background:

Sometimes I get a CD or DVD containing a lot of files of a particular type that are named to a particular convention, which I need to take a copy of. Once I've copied it locally, I want to rename the files to remove the convention. For example, say I borrow a music CD from a friend, which he has burnt himself. He has named the track listing according to his own convention thus:

  1. 001 - Track 1.mp3
  2. 002 - Track 2.mp3
  3. ...
  4. 100 - Track 100.mp3

I would like to rename these files so that the track identifier (the number part) is removed along with the spaces and the hyphen character such that I am left with just the track name. So, for example:

001 - Track 1.mp3

becomes

Track 1.mp3

Obviously I could rename these manually, which if I only had a few files to rename would be fine. However, in the example above I have 100 files to rename and doing this manually would be time consuming and tedious. So I will now show you a nice little renaming routine, which you can adapt, that will handle this for us.

B. The How To:

  1. Copy your files into one directory. The files, as I mentioned, should all be of one file type, for this to work.
  2. Open a Terminal window.
  3. At the prompt type:

cd <path_to_directory_containing_files> && for file in *<file_type> do mv "$file" "${file:<number_of_leading_characters_to_remove>}"; done

Where:

<path_to_directory_containing_files> is the path to the directory you copied you files to.

<file_type> is the file type of the files in the directory without the dot (so instead of *.mp3, simply enter *mp3).

<number_of_leading_characters_to_remove> is the number of characters you wish to remove from the front of the file name.

C. Example:

In the earlier example the filenames were prefixed with a three digit track identifier such as '001'. This was followed by a space, a hypen and space (' - ') and finally the track name ('Track 1.mp3', for example).

Let us assume, that for the 100 files in the example we want to remove the three digit identifier, the space, the hypen and the last space before the track name so that we are only left with the track name and file type, e.g:

001 - Track 1.mp3

becomes

Track 1.mp3

I therefore need to remove the first 6 characters from the beginning of each filename: 3 characters for the track identifier + 1 space + 1 hyphen + 1 more space.

Let us also assume that I have copied the 100 files into a folder on my Desktop called 'Music'.

So, in order to achieve the bulk rename I would type in the Terminal:

cd /home/me/Desktop/Music/ && for file in *mp3; do mv "$file" "${file:6}"; done

You should now have all 100 files renamed to your requirements.

D. Further Notes for Adapting This:

  1. file is simply a variable and can have any name you like. Some people use i but I chose file for the example as we were dealing with files.
  2. Naturally you can use any file type. I used mp3 files for the example as people are generally familiar with it. Remember, don't include the '.' when referencing your file type i.e. *<file_type> not *.<file_type>
  3. You can remove as many characters as you like. The number of characters to remove is not 0 based, it is 1 based. So, if you want to remove 6 characters like in our example you would not enter "${file:5}" (0, 1, 2, 3, 4, 5) but would instead enter "${file:6}" (1, 2, 3, 4, 5, 6) to achieve the correct result.
  4. for file in *mp3; do mv "$file" "${file:6}"; done is actually a small loop.
  5. I'm sure this can be modified in many other ways too, for example, to perhaps remove trailing characters. I've not needed to do that yet so haven't investigated that just now. If I need to do that, I will write another tutorial.

yes

I hope this has helped you.

smiley

Comments
quantumbox 5 years ago

Perfect, just what I was looking for! Easy peasy.


sujitnag 6 years ago

sorry to say unnecessary make it complicated.
easy topic butbad way to express.
"path_to_directory_containing_files"- choose easy and short name
like "source_dir_path".

not easy to read and follow.

demote