Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save jtuz/f3d50fe7ce57d949e436380368b9c00a to your computer and use it in GitHub Desktop.
Save jtuz/f3d50fe7ce57d949e436380368b9c00a to your computer and use it in GitHub Desktop.
How to remove spaces in filenames and lowercasing — First published in fullweb.io issue #20

How to remove spaces in filename and lowercasing

A short script this week to replace spaces in filenames in a given folder:

# Looping over files, forging new filename, and then renaming it.
# Spaces will be replaced by underscores (_).
for oldname in * 
do 
  newname=`echo $oldname | sed -e 's/ /_/g'` 
  mv "$oldname" "$newname" 
done
# Spaces are now underscores

This one remove the spaces, only the sed expression is different:

for oldname in * 
do 
  newname=`echo $oldname | sed -e 's/ //g'` 
  mv "$oldname" "$newname" 
done

And this one lowercase all letters in addition to removing spaces:

for oldname in * 
do 
  newname=`echo $oldname | sed -e 's/ //g' | tr '[:upper:]' '[:lower:]'` 
  mv "$oldname" "$newname" 
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment