Created
September 30, 2016 04:13
-
-
Save machuiwen/409bf77abfdd3c5b130fbea0100cf033 to your computer and use it in GitHub Desktop.
Shuffle files in a folder, for example musics.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/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 "$@" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
cool!