Home –  tr command
Tag Archives: tr command

How to delete spaces from file name in Linux?

If your file name has spaces in it and there are many such files that it is not possible for you to rename each file’s name manually then you may use the command given below. It will rename all the file names which have┬áspaces by replacing the spaces with an underscore ( _ ).

for FILE in *; do mv "$FILE" "$(echo "$FILE"|tr ' ' '_')"; done

or

for file in *; do mv "$file" `echo $file | sed -e 's/ */_/g' -e 's/_-_/-/g'`; done

I had two files named a b c.txt and x y z.txt in my directory. I used the command given above to delete spaces from file name. It changed the file names to a_b_c.txt and x_y_z.txt respectively. See the screenshot below: