Bash script: Randomize filenames in a directory
Continuing on the theme of file management from my last post: This script takes a folder of files and randomizes all filenames, whilst keeping the filename extension. This is useful if you're sorting by name, and want to have the files presented in a random order. Some possible use cases are a folder of pictures you intend to post to a blog, do further processing on, and the order and names of the files aren't important.
Usage: cd
to the directory that contains the files you wish to randomize the filenames of, then run the script.
randomize_filenames.sh
:
#!/bin/bash
# Randomize file names in current working directory, keeping the filename extension
# Modified from: https://unix.stackexchange.com/a/6553
# Ignores dotfiles and subdirectories
find . -type f -not -path '*/.*' |
while read -r name; do
ext=${name##*/}
case $ext in
*.*) ext=.${ext##*.};;
*) ext=;;
esac
newName=`mktemp --dry-run XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX`
mv "$name" "$newName$ext"
done