remove spaces via bash

Remove spaces from file names via bash

if you need a simple way to remove spaces from file names and replace with an underscore or a hyphen or whatever else here is a bash one liner. You can also do this via python or perl or most likely via php (why? o.0) but since bash / zsh is here and readily available for me I choose to use bash / zsh.

Remove spaces and replace with underscore:

find /tmp/ -depth -name "* *" -execdir rename 's/ /_/g' "{}" \;

remove spaces and replace with hyphen :

find /tmp/ -depth -name "* *" -execdir rename 's/ /-/g' "{}" \;

Remove spaces completely:

find /tmp/ -depth -name "* *" -execdir rename 's/ //g' "{}" \;

Be careful with he above as it will remove all spaces from file and directory names.

hope this helps. if you have a better way of doing it comment and let me know 🙂

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *