Lets say I want to pick a random file when I do an "ls" command. I don't have set number of files in each directory.
ls | head -1
This gives me the first one in each directory, is there a way to do the same but pick a random one.
Lets say I want to pick a random file when I do an "ls" command. I don't have set number of files in each directory.
ls | head -1
This gives me the first one in each directory, is there a way to do the same but pick a random one.
A bit odd, but...
ls | sed $((RANDOM%$(ls | wc -w)+1))!d\;q
or
ls | head -$((RANDOM%$(ls | wc -w)+1)) | tail -1
First one didn't work
ls | sed $((RANDOM%$(ls | wc -w)+1))!d
-bash: !d: event not found
The second one works perfectly. Can you help me understand how it works? thanks.
Hey, you're right!
Works in KSH (on AIX) but not in Bash.
ls | sed -n "$((RANDOM%$(ls | wc -l)+1))p"
(changed wc to use -l)