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

Go to top