Skip to main content

Bash Snippets


Lowercase all filenames

As this post, all filenames can be made to be lowercase by running the following within the specified directory:

for f in *; do mv "$f" "$f.tmp"; mv "$f.tmp" "`echo $f | tr "[:upper:]" "[:lower:]"`"; done

Rename files in a random numeric fashion

As this post notes, the $RANDOM environment variable can be used -- it generates random values between 0 and 32767.

A simple for loop in bash works fine (the examples below target .jpg file types, but these examples can be modified as appropriate):

for i in *.jpg; do mv -i "$i" ${RANDOM}.jpg; done

If you have a ton of files (e.g., more than 1000), then the following might be a better strategy:

for i in *.jpg; do mv -i "$i" ${RANDOM}${RANDOM}.jpg; done

Note that you could also do something like the following to preserve old information in the filename, if desired (the only difference is changing ${RANDOM}${RANDOM} to ${RANDOM}-$i, where $i preserves the original filename):

for i in *.jpg; do mv -i "$i" ${RANDOM}-$i.jpg; done