remove spaces from filenames in linux

Let us assume that we have a situation where in your boss wants you to remove all spaces from his MP3/OGG/WMV/MPG filenames and replace them with underscore character. To accomplish this task of replacing space character with underscore character is real easy for a common linux task and can be easily done using linux renaming tool command mv.

Now, picture that renaming and removing spaces from 100,000 MP3 filenames or any other multimedia file names?

How to rename multiple files?
How to rename multiple thousand files?
How to convert all lowercase alpha characters to uppercase from thousand filenames?

Ding! Here’s how to accomplish these task.

Basically, replacing space character with underscore from the filename of a file is simple and would be done like so

# mv “file with space.mp3” file_with_space.mp3

Now, the next command assumes that you have thousands of hundred of thousands MP3 files with spaces between its filenames. And you want to remove space character and replace them with underscores. Just make sure you are currently inside the working folder of these MP3 files and issue

# for files in *.mp3; do mv “$files” `echo $files | tr ‘ ‘ ‘_’`; done

Wonderful linux.

Now, here’s another one shot command to convert all lowercase filenames into uppercase filenames using the above approach plus tr linux command. tr is a string manipulation linux command and discussed here.

How to convert lower case filenames to uppercase filenames of hundred thousand files in one shot?

# for files in *.mp3; do mv “$files” `echo $files | tr ‘[:lower:]’ ‘[:upper:]’`; done

You can replace *.mp3 with any other filename identifier or file glob.

Have a nice weekend with clicks to all! 🙂

1 thought on “remove spaces from filenames in linux”

Leave a Reply