close
» » How to remove spaces in file names?

How to remove spaces in file names?

Other

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.
CrossOver Linux
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!!!
 
 

Print version

Add comments

  • Local comments
  • Facebook comments
  • bowtiesmilelaughingblushsmileyrelaxedsmirk
    heart_eyeskissing_heartkissing_closed_eyesflushedrelievedsatisfiedgrin
    winkstuck_out_tongue_winking_eyestuck_out_tongue_closed_eyesgrinningkissingstuck_out_tonguesleeping
    worriedfrowninganguishedopen_mouthgrimacingconfusedhushed
    expressionlessunamusedsweat_smilesweatdisappointed_relievedwearypensive
    disappointedconfoundedfearfulcold_sweatperseverecrysob
    joyastonishedscreamtired_faceangryragetriumph
    sleepyyummasksunglassesdizzy_faceimpsmiling_imp
    neutral_faceno_mouthinnocent
CrossOver Linux
Your attitude to the website Other polls