Showing entries 1 to 1
Displaying posts with tag: spaces to underscores (reset)
Quick tip: how do you rename all files so spaces are converted to underscores?

My friend today asked me how to convert all spaces in filenames under a specified directory to underscores. Also, at the same time lowercase all of the filenames. Here is a quick script to do what is needed. Let us start with creating some test data in a temp directory:

mkdir temp
cd temp
touch Foo FooO "Foo Bar" "FOO BAaR"
\ls | while read -r FILENAME
do
mv -v "$FILENAME" `echo $FILENAME| tr ' ' '_'| tr '[A-Z]' '[a-z]'`
done

Note:  I intentionally have slash in front of ls (\ls).  \ls means that we want to make sure there is no ls alias overwriting our command. This is needed if your system has alias setup to display ls in a different way instead of default listing.  mv -v shows us the filenames being renamed as your script goes through the whole dir.  Your output should be like:

`Foo' -> `foo'
`FOO BAaR' -> …

[Read more]
Showing entries 1 to 1