I need my script to choose a random folder within a folder and set that folders name as a variable.
I can list all folders within a folder using
#!/bin/bash
dir='/folder'
file=`/bin/ls -1 -l "$dir" | egrep '^d' | sort --random-sort | head -1`
path=`readlink --canonicalize "$dir/$file"` # Converts to full path
echo "The randomly-selected file is: $path"
echo "$file"
echo "$path"
Say my folder structure is
folder
|____folder01
|____folder02
|____folder03
By using the code above I can list the folders as so.
The randomly-selected file is: /folder/drwxr-xr-x 2 user user 4096 2012-04-14 14:53 folder01
drwxr-xr-x 2 user user 4096 2012-04-14 14:53 folder01
/folder/drwxr-xr-x 2 user user 4096 2012-04-14 14:53 folder01
I just want it to display 'folder01' and not 'drwxr-xr-x 2 user user 4096 2012-04-14 14:53 folder01'
Any help would be greatly appreciated.
Cheers
DV
---------- Post updated at 03:10 PM ---------- Previous update was at 02:25 PM ----------
I think the issue is with this command
ls -1 /folder/
I just need this command to just list directories and not files.