LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - General (https://www.linuxquestions.org/questions/linux-general-1/)
-   -   Convert filenames with spaces? (https://www.linuxquestions.org/questions/linux-general-1/convert-filenames-with-spaces-389587/)

Macky 12-05-2005 05:15 PM

Convert filenames with spaces?
 
Hello, I have a lot of data I want to transfer from my windows partion to my linux partition. It is a lot of mp3's, ebooks and so on. But these files contains spaces in their names, and linux doesn't seem to support that so well. It seems a space is converted to "\ " in the console. It looks bad, and I can't use the tabcompletion.

I have read it is recommended to convert spaces to underscores, and upper cases letters to lower case. If I have a directory full of mp3's, how can I do this conversion with a simple command?

Thanks,

/Mack

jschiwal 12-05-2005 05:48 PM

You might want to use filename substitution in Bash.

Code:

cd <source directory>
for song in *.mp3; do
mv "${song}" "<destination directory>/${song// /_}"
done

If a filename contains spaces, you need double quotes around the bash variable. "song" in this example.
The "\ " in the names that you mentioned allow you to have a space treated literally with a command. The backslash is not a part of the name. The "//" two forward slashes in the destination part will replace all of the spaces to underscores. A single slash would have just replaced the first space.


All times are GMT -5. The time now is 02:13 PM.