» » How to remove spaces in file names?

How to remove spaces in file names?

Section: Other | Actuality: Unspecified

How to remove spaces in file names?

Spaces in file names very often can create a lot of "hemorrhoids". Who works in the console, that will understand me. Recently there was a need to mass encode a group video files from one format to another. But the script didn't work because the file names were these unfortunate spaces.
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:
for i in *.avi
k=`echo ${i}|sed s/' '/'_'/g`
mv "${i}" ${k}
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:
for i in *.mp4 *.txt *.avi *.html *.odt
k=`echo ${i}|sed s/' '/'_'/g`
mv "${i}" ${k}
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!!!

Print version

Add comments

  • Local comments
  • Facebook comments
  • bowtiesmilelaughingblushsmileyrelaxedsmirk
    Your working environment Other polls