Bash - How to Access Random Files in a Directory

Trifork
  10 years ago
  3

Though this may not be the regular flow of the Mint community tutorials (we have Stack Overflow for that) but it is still a practical use. Perhaps you want to play music files in a directory at random (that's what I use it for) or you're just cruel and want to delete someone's files at random. Good luck, fellow newbs.

First, you need a directory, we'll say this one's path is "~/foo". Say everything in the foo directory was audio files and you wanted to play a random song. You, as an experienced scripter ( =] ), would type into your favorite text editor (feel free to copy and paste):

(THIS IS A SAFE VERSION)

--------------------------------------------------------

listnum=$(ls -l | wc -l)
filenum=$((listnum-1))

# filenum contains number of files in the directory
i=$((($RANDOM%filenum)+1))

#RANDOM should not be defined. It is a part of bash.
a=0

# "i" is randomized. We use the loop to see when a = i (a is incremented) then we choose the # file the loop is on

for file in ~/foo/*
do
        a=$(($a + 1))
        if [[ ("$a" ==  "$i") ]]; then
                filename=$file
        fi
done

# Command goes here ("filename" is the randomly chosen file)

# (THIS IS AN EXAMPLE)

play "$filename"

-----------------------------------------------------------

Or something better (probably something better). This would play a random file in the foo directory.

That's pretty much it. Copy and paste (or research and learn and become ULTIMATE).

After the for loop, you can do whatever you want. The variable "filename" contains the randomly chosen file. Please fix any errors that may occur. Good luck scripting.

Written by a newb (n00b?).

(In the original version of the tutorial, there were problems when using filenames with whitespace. If you execute the command like "play '$filename'", with quotes outside of the variable, it should be able to use that file, sorry for any confusion. It is guaranteed to work now)

Comments
Trifork 10 years ago

Thank you. It's my first tutorial. I guess I know to check for more now... =)


Hammer459 10 years ago

That is all good, I understand the whitespace issue as I have been there. A long time ago, you here limited to 8, 12 or 16 charachters in the filename too, depending on the unix flavour. And white space DOES make life suck in shell :-)
I have changed my vote on the tutorial to "Promote" now.


Trifork 10 years ago

You can now copy-and-paste the code and it will work. It's already tested and perfected.


Trifork 10 years ago

Sorry for any confusion. I'll fix anything else that comes along...


Trifork 10 years ago

Good point. If you put quotes outside of the variable names (I changed that in the tutorial), it should take the variable name literally, and it should work. Please comment if anything else is wrong (this tutorial is doing pretty badly so far...).


Hammer459 10 years ago

In shell it is considered bad practice but in todays world it is common practice to have white space.
That means that in todays real world, where most non-geeks like us live, it is bad practice to present a tutorial that breaks upon white space.


Trifork 10 years ago

Original Poster
If whitespace causes problems, put quotations around the variable filename's declaration (i.e. filename="$file"). It should work then. Either way, it's considered bad practice to put whitespace in file names. Underscores(_) are better. It definitely works in that case.


Hammer459 10 years ago

Cool idea... Only problem... It does not work! File names containing special characters (such as white-space) i.e. "ACDC - Thunderstruck.mp3" is the first problem.