Find the answer to your Linux question:
Results 1 to 8 of 8
I have been looking for the Following. I need this for Quite a While already and I am sick of searching, so its ASking time =) How can I get ...
  1. #1
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422

    Random File from Folder and Sub-Folders.

    I have been looking for the Following. I need this for Quite a While already and I am sick of searching, so its ASking time =)

    How can I get a Random File+Path of a File with a Bash Script.
    For example.

    I for the following Folder.
    /home/robin/randomstuff/
    That has two folders.
    One folder is Called. AAA
    And the Other is called BBB.
    The Folder AAA has as the files:
    File1, File2 and File3.
    and Folder BBB has File4, File5 and File6

    So I want to point this script at /home/robin/randomstuff
    and I want the Script to pick a Random File from the randomstuff folder and the subfolders.
    So from the folders AAA and BBB.


    How can I do that in Bash??? Amd CAN I do this in Bash?

    Thanks,

    Cheers.
    Robin
    New Users, please read this..
    Google first, then ask..

  2. #2
    Just Joined!
    Join Date
    Apr 2008
    Posts
    35
    Hey There,

    That's a good question

    Here's a good explanation that I probaby shouldn't cut-n-paste into this forum:

    http://www.museum.state.il.us/ismdep...randomvar.html

    Best wishes,

    Mike

  3. #3
    Just Joined!
    Join Date
    Apr 2008
    Posts
    35
    Sorry - just in case:

    dir=/the/dir/you/want # or $1

    bound=`echo \`find $dir|wc -l\`` # to get the highest number in your random pool

    Cheers,
    Mike

  4. #4
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    Thanks, but that solved half on my Question =)
    It gave a a nice boost though. Since I didnt think of the Highest Random you gave me..

    Here is my code now:
    [script]
    #!/bin/bash


    #Add Check for One Argument Later..
    rnddir=$1;

    bound=`echo \`find $rnddir|wc -l\`` # to get the highest number in your random pool
    # Getting fancy...
    SEED=$(head -1 /dev/urandom | od -N 1 | awk '{ print $2 }')
    # Pseudo-random output fetched from /dev/urandom (system pseudo-random "device"),
    # then converted to line of printable (octal) numbers by "od",
    # finally "awk" retrieves just one number for SEED.
    RANDOM=$SEED%$bound+1
    echo $RANDOM #Now we get a Random Number.

    ##Here I should Convert that to a randomfile name + Path...


    exit 0
    [/script]

    It does do the search thingy. But the Random Calculation doesnt work. And I have NO idea how to convert that number to the Number I'll get to a Filename + Path..

    I dont get what is wrong in: [script]RANDOM=$SEED%$bound+1[/script] Since that is basicly how I do it in C++ as well. but then its. [script]randomNumber = rand() % bound + 1;[/script]

    Any tips/ Advice?
    New Users, please read this..
    Google first, then ask..

  5. #5
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117
    Hi.

    I used the bash $RANDOM:
    Code:
    #!/bin/bash3 -
    
    # @(#) s1       Demonstrate using $RANDOM to choose a file.
    # See http://www.tldp.org/LDP/abs/html/randomvar.html#EX21
    
    echo
    echo "(Versions displayed with local utility \"version\")"
    version >/dev/null 2>&1 && version =o $(_eat $0 $1) tree find sed
    echo
    
    # Create a structure
    rm -rf a b c
    mkdir a a/b a/c
    touch a/b/b{1..4} a/c/c{1..4}
    echo
    echo " Structure to be considered:"
    tree a
    
    find a -type f >t1
    RANGE=$( wc -l <t1 )
    
    number=$RANDOM
    let "number %= $RANGE"  # Scales $number down within $RANGE.
    let "number++"
    echo "Random number [1 and $RANGE] ---  $number"
    
    echo
    echo " Line $number from list of plain files:"
    sed -n "${number}p" t1
    
    exit 0
    To produce:
    Code:
    % ./s1
    
    (Versions displayed with local utility "version")
    Linux 2.6.11-x1
    GNU bash 3.00.16(1)-release
    tree v1.5.0 (c) 1996 - 2004 by Steve Baker ...
    GNU find version 4.1.20
    GNU sed version 4.1.2
    
    
     Structure to be considered:
    a
    |-- b
    |   |-- b1
    |   |-- b2
    |   |-- b3
    |   `-- b4
    `-- c
        |-- c1
        |-- c2
        |-- c3
        `-- c4
    
    2 directories, 8 files
    Random number [1 and 8] ---  5
    
     Line 5 from list of plain files:
    a/c/c1
    Best wishes ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  6. #6
    Linux Guru
    Join Date
    Nov 2007
    Location
    Córdoba (Spain)
    Posts
    1,513
    I use this to set a random wallpaper on fvwm:

    Code:
    #!/bin/bash
    # Reads a given directory and set a random
    # wallpaper using the images on that dir.
    
    if [[ -d "${WALLPAPERDIR}" ]]
    then
            files=$(ls "${WALLPAPERDIR}")
            file_matrix=($files)
            num_files=${#file_matrix[*]}
            FvwmCommand "ChangeWallpaper "${WALLPAPERDIR}/${file_matrix[$((RANDOM&#37;num_files))]}""
    fi
    
    exit 0
    If you change the ls by the proper find command, then it can do what you need with little modifications.

  7. #7
    Just Joined!
    Join Date
    Apr 2008
    Posts
    35
    Cool Looks like there plenty of options to choose from now

    Best wishes,

    Mike

  8. #8
    Linux Engineer RobinVossen's Avatar
    Join Date
    Aug 2007
    Location
    The Netherlands
    Posts
    1,422
    Thanks all..
    I got the following now:
    Code:
    #!/bin/bash
    
    if [[ -d "$1" ]]
    then
    	echo "Start Indexing Files"
            files=$(find "$1")
            echo "Done with Indexing Files"
            file_matrix=($files)
            num_files=${#file_matrix[*]}
            echo "$1/${file_matrix[$((RANDOM&#37;num_files))]}"
    fi
    exit 0
    I loved the way drl made up. But I didnt get his code and this works fine BUT...

    The are Folders with Longer Names for example:
    "All my Family Pictures"
    or in Linux Format
    All\ my\ family\ pictures

    and it only shows All\ for example...

    What is the way to fix this?


    Thanks.. =)

    edit:

    Its..
    Code:
    #!/bin/bash
    
    if [[ -d "$1" ]]; then
    	echo "Start Indexing Files"
            if [[ "$2" ]]; then
               files=$(find "-path $1 -name *.$2")
            else
               files=$(find "-path $1 -name *")
    	fi
            echo "Done with Indexing Files"
            file_matrix=($files)
            num_files=${#file_matrix[*]}
            echo "$1/${file_matrix[$((RANDOM%num_files))]}"
    fi
    
    exit 0
    Now
    New Users, please read this..
    Google first, then ask..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...