Skip to content

Instantly share code, notes, and snippets.

@sitaramc
Last active February 18, 2017 07:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sitaramc/b24c3387330694b88026c016fe0d1e7e to your computer and use it in GitHub Desktop.
Save sitaramc/b24c3387330694b88026c016fe0d1e7e to your computer and use it in GitHub Desktop.
randomise filenames / reverse the randomisation
#!/bin/bash
# Usage:
# $0 # renames all mp3 files in $PWD by prefixing a random sequence number and a hyphen
# $0 clean # renames all mp3 files in $PWD by removing any leading digits and a hyphen
ext=mp3
# making this a parameter is left as an exercise for the reader
if [ "$1" = "clean" ]
then
for i in *.$ext
do
[[ $i =~ ^[0-9]+-(.*) ]] || continue
mv ${BASH_REMATCH[0]} ${BASH_REMATCH[1]}
done
else
seq -w 1 `ls *.$ext | wc -l` | shuf | paste - <(ls *.$ext) | while read seq name
do
mv $name "$seq-$name"
done
fi
$ ls
capkin.mp3 catted.mp3 educ.mp3 fender.mp3 imaged.mp3 Marut.mp3 MIF.mp3 oleins.mp3 unwig.mp3 Yvonne.mp3
$ /tmp/randomise-files.sh
$ ls
01-imaged.mp3 03-catted.mp3 05-MIF.mp3 07-Yvonne.mp3 09-educ.mp3
02-Marut.mp3 04-oleins.mp3 06-fender.mp3 08-capkin.mp3 10-unwig.mp3
$ /tmp/randomise-files.sh clean
$ ls
capkin.mp3 catted.mp3 educ.mp3 fender.mp3 imaged.mp3 Marut.mp3 MIF.mp3 oleins.mp3 unwig.mp3 Yvonne.mp3
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment