Skip to content

Instantly share code, notes, and snippets.

@machuiwen
Created September 30, 2016 04:13
Show Gist options
  • Select an option

  • Save machuiwen/409bf77abfdd3c5b130fbea0100cf033 to your computer and use it in GitHub Desktop.

Select an option

Save machuiwen/409bf77abfdd3c5b130fbea0100cf033 to your computer and use it in GitHub Desktop.
Shuffle files in a folder, for example musics.
#!/bin/bash
shuffle () {
if [ -d $1 ]; then
local folder=${1%/}
read -p "Do you really want to shuffle folder: $folder? (yes/no) > "
if [ $REPLY == "no" ]; then
exit 1
elif [ $REPLY != "yes" ]; then
echo "Invalid entry." >&2
exit 1
fi
local files=$(ls $1)
while read -r fname; do
if [ -f "${folder}/${fname}" ] && [[ $fname == *.* ]]; then
while true; do
local new_name=$RANDOM.${fname##*.}
if [ -f "${folder}/${new_name}" ]; then
# duplicate file name
continue
fi
mv "${folder}/${fname}" "${folder}/${new_name}"
echo "${fname} -> ${new_name}"
break
done
fi
done <<<"$files"
else
echo "$(basename $0): $1: No such directory" >&2
exit 1
fi
}
shuffle "$@"
@wxdai
Copy link

wxdai commented Sep 30, 2016

cool!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment