Having a space in the file name is never a good idea. If you are in need to remove space from all file names within your current directory you can use a following command to do so:

 ls | grep " " | while read -r f; do mv -i "$f" `echo $f | tr -d ' '`; done

In case that you wish to substitute space within a file name to underscore ( or any other character ) use a following command to do so:

ls | grep " " | while read -r f; do mv "$f" `echo $f | tr ' ' '_'`; done

How it works? ls and grep will feed while loop with all files within a current working directory which contain a space in their file name. In the body of the while loop we will next execute mv command a translate it file destination with tr command. Make sure to keep -i option enabled when using mv command to avoid accidentally overwrite files.



Free Linux eBooks

Do you have the right skills?

Our IT Skills Watch page reflects an up to date IT skills demand leaning towards the Linux and Unix environment. We have considered a number of skills and operating systems.

See the result...

Linux Online Training

Learn to run Linux servers and prepare for LPI certification with Linux Academy. 104 available video lessons with PDF course notes with your own server!

Go to top