Converting spaces in filenames to underscores



Linux supports long file names, in some (many?) ways better than windows. However, when I moved over to linux I had tons of files with spaces in the name. This isn’t really a problem usually, but it can be a bit annoying having to enclose the filename in quotes for everything… anyway. Most of these were mp3’s that I had ripped from my collection of cd’s to store on the server. The script I used to automatically play through the music archive had problems dealing with the spaces (and I didn’t want to figure out how to make it work…) so I found another solution….


I found a bash script that basically does the job. It’s very simple and I can’t take credit for writing it, but if you want to convert spaces in filenames to underscores then this is for you…..

#!/bin/bash
for i in $(ls -1 *)
do
rename \ _ *.$1
done

Basically, it takes the file extension of the files you want to rename as an argument, so to rename files with spaces that are mp3s…. call it like this

$convertspaces mp3

and everything in the current directory get’s “fixed”.

   Send article as PDF   

Similar Posts