Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Linux - Replacing spaces in the file names

I have a number of files in a folder, and I want to replace every space character in all file names with underscores. How can I achieve this?

like image 393
Mithun Sreedharan Avatar asked Nov 27 '09 05:11

Mithun Sreedharan


People also ask

Are spaces allowed in Linux file names?

It's not very common in Linux to handle filename with spaces but sometimes files copied or mounted from windows would end up with spaces. While it is not recommended to have file names with spaces, let's discuss how to manage filename with spaces in a Linux system.

Why Should spaces be avoided in Linux filenames?

It could intimidate a new user or even a seasoned one if there are way too many of those slashes. It gets even messier when there are backslashes in the filename. Then you'll be seeing double backsplashes. This is the reason why you should try and avoid using spaces or other special characters in file names.


1 Answers

This should do it:

for file in *; do mv "$file" `echo $file | tr ' ' '_'` ; done 
like image 93
neesh Avatar answered Nov 16 '22 00:11

neesh