I would like to rename all of my files of my USB device randomly. Indeed, in my car, my tuner has not the random function.
I think I can do it with PHP ... but I'm pretty sure it is possible to do something shorter in shell script, right?
An idea?
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems. It only takes a minute to sign up.
Sign up to join this communityYou can add a random number of equal width as prefix, that's what I'm doing to randomize my audio files:
for i in *; do mv "$i" "$(seq -w 0 999 | shuf -n 1)_$i"; done
a.mp3
, b.mp3
and c.mp3
, the order a,b,c is slightly more likely than a,c,b or b,a,c or b,c,a or c,a,b which are slightly more likely than c,b,a.
Aug 22, 2012 at 7:16
Assuming that you are running Linux, that you want to randomly order all files in the current directory and that no file name contains a newline:
printf '%s\n' * | shuf | nl -n rz -s - |
while IFS= read -r name; do
mv -- "${name#*-}" "$name"
done
The printf
command prints the file names, one per line. shuf
applies a random permutation to the input lines. nl
adds prefixes like 000001-
, 000002-
, etc. to each line. The while
loop iterates over all the lines. The mv
command renames foo.mp3
to 000001-foo.mp3
, etc.