|
11 years ago 5 |
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:
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.
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.
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.
I hope this has helped you.
Perfect, just what I was looking for! Easy peasy.
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