MintGuide.org / How to remove spaces in file names?

How to remove spaces in file names?

Instead of spaces appeared underscores need to go to the desired directory and execute the command or script. To do this, go to the directory:
cd /path to directory/
And now let's execute the following script, which will replace the spaces with underscore characters, or any other desired character:
 
#!/bin/sh
for i in *.avi
do
k=`echo ${i}|sed s/' '/'_'/g`
mv "${i}" ${k}
done
Instead of *.avi put your extension files in which you need to make name changes. Or, you can simply transfer the desired file extensions, then the script will have the following form:
 
#!/bin/sh
for i in *.mp4 *.txt *.avi *.html *.odt
do
k=`echo ${i}|sed s/' '/'_'/g`
mv "${i}" ${k}
done
But Linux wouldn't be Linux if there wasn't masses of ways to solve the problem. So to combat the spaces you can run other commands using other utilities or programs. You can use the rename utility:
rename 's/ /_/g' *
But if after work on those files you want back again with spaces in the names, then run the command:
rename 's/_/ /gi' *.mp4 *.txt *.avi *.html *.odt
This command in batch mode replace the underscore for a space.
Good Luck!!!
 
 
3-12-2015, 10:31
Go Back