Skip to content

Instantly share code, notes, and snippets.

@nepsilon
Last active November 15, 2024 23:14
Show Gist options
  • Save nepsilon/6896f1e0a075db59fb95 to your computer and use it in GitHub Desktop.
Save nepsilon/6896f1e0a075db59fb95 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