Visit Our UNIX and Linux User Community


choosing a random folder in a folder


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting choosing a random folder in a folder
# 1  
Old 04-14-2012
choosing a random folder in a folder

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

Code:
#!/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.

Last edited by digitalviking; 04-14-2012 at 12:06 PM..
# 2  
Old 04-14-2012
Try
Code:
find FOLDER -maxdepth 1 type -d

instead of
Code:
ls -l FOLDER

This User Gave Thanks to yazu For This Post:
# 3  
Old 04-14-2012
I worked it out

Code:
#!/bin/bash

dir='/folder'
folder=`/bin/ls -d "$dir"/*/ | sort --random-sort | head -1`

echo "The randomly-selected file is: $folder";

output is a random folder in a folder

Boooo yacka shaaaaa

---------- Post updated at 03:49 PM ---------- Previous update was at 03:24 PM ----------

Quote:
Originally Posted by yazu
Try
Code:
find FOLDER -maxdepth 1 type -d

instead of
Code:
ls -l FOLDER

I like this a lot

find FOLDER -maxdepth 1 type -d

what do I put in the place of folder I can get it to work??

Also, say I want to search my documents folder but I only want the results to be folders or folders in folders. Is there away of doing that?

Cheers
DV
# 4  
Old 04-14-2012
Quote:
what do I put in the place of folder I can get it to work??
your folder Smilie
Quote:
Also, say I want to search my documents folder but I only want the results to be folders or folders in folders. Is there away of doing that?
"-type d" means "find" command will find only directories (folders).
# 5  
Old 04-14-2012
Error

Quote:
Originally Posted by yazu
your folder Smilie

"-type d" means "find" command will find only directories (folders).
find /root/folder/ -maxdepth 1 type -d

I get this error

find: paths must precede expression: type
Usage: find [-H] [-L] [-P] [-Olevel] [-D help|tree|search|stat|rates|opt|exec] [path...] [expression]
# 6  
Old 04-14-2012
Sorry. Add '-a' :

Code:
find /root/folder/ -maxdepth 1 -a type -d

# 7  
Old 04-14-2012
Worked it out its

Quote:
Originally Posted by yazu
Sorry. Add '-a' :

Code:
find /root/folder/ -maxdepth 1 -a type -d

find /root/folder/ -maxdepth 100 -type d

---------- Post updated at 04:41 PM ---------- Previous update was at 04:29 PM ----------

My code

Code:
path=`find /root/folder/ -maxdepth 100 -type d | sort --random-sort | head -1`

if this returns a directory with spaces in for instance:

/root/folder/this is a folder

is there a way to replace the value of path with the same value but with the spaces replaced with

/root/folder/this\ is\ a\ folder/

Any qlues?

Cheers
DV

Previous Thread | Next Thread

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Delete oldest folder based on folder named as date

Hi, I have a script doing backup to synology server, the script create new folder each day with the date as being folder name i.e. 2018-07-30. Just before creating the new folder I want the script to find the oldest folder from the list and delete it including its content. for example... (3 Replies)
Discussion started by: humble_learner
3 Replies

2. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

3. Shell Programming and Scripting

Shell scripting for moving folder specific files into target directory of that country folder.

I need help to write shell script to copy files from one server to another server. Source Directory UAE(inside i have another folder Misc with files inside UAE folder).I have to copy this to another server UAE folder( Files should be copied to UAE folder and Misc files should be copied in target... (3 Replies)
Discussion started by: naresh2389
3 Replies

4. Shell Programming and Scripting

copying random Jpg files to different folder

Hi, I have 200 pictures in a folder and I would like move 10 random pictures every week to given folder automatically. I have this server on 1and1.com. So I tried the following using Bash script for manual copy and paste for testing #!/bin/bash mapfile -t -n 3 files < <(find... (13 Replies)
Discussion started by: raamkum
13 Replies

5. Shell Programming and Scripting

File Management: How do I move all JPGS in a folder structure to a single folder?

This is the file structure: DESKTOP/Root of Photo Folders/Folder1qweqwasdfsd/*jpg DESKTOP/Root of Photo Folders/Folder2asdasdasd/*jpg DESKTOP/Root of Photo Folders/Folder3asdadfhgasdf/*jpg DESKTOP/Root of Photo Folders/Folder4qwetwdfsdfg/*jpg DESKTOP/Root of Photo... (4 Replies)
Discussion started by: guptaxpn
4 Replies

6. Windows & DOS: Issues & Discussions

How can I upload a zip folder on a unix path from my windows folder?

Hello, I am an amature at UNIX commands and functionality. Please could you all assist me by replying to my below mentioned querry : How can I upload a zip folder on a unix path from my windows folder? Thanks guys Cheers (2 Replies)
Discussion started by: ajit.yadav83
2 Replies

7. Shell Programming and Scripting

script for Finding files in a folder and copying to another folder

Hi all, I have a folder '/samplefolder' in which i have some files like data0.txt, data1.txt and data2.txt. I have to search the folder for existence of the file data0.txt first and if found have to copy it to some other file; next i have to search the folder for existence of file... (5 Replies)
Discussion started by: satish2712
5 Replies

8. UNIX for Advanced & Expert Users

Auto copy for files from folder to folder upon instant writing

Hello all, I'm trying to accomplish that if a file gets written to folder /path/to/a/ it gets automatically copied into /path/to/b/ the moment its get written. I thought of writing a shell script and cron it that every X amount of minutes it copies these files over but this will not help me... (2 Replies)
Discussion started by: Bashar
2 Replies

9. Shell Programming and Scripting

Parse the .txt file for folder name and FTP to the corrsponding folder.

Oracle procedure create files on UNIX folder on a regular basis. I need to FTP files onto windows server and place the files, based on their name, in the corresponding folders. File name is as follows: ccyymmddfoldernamefile.txt; Folder Name length could be of any size; however, the prefix and... (3 Replies)
Discussion started by: MeganP
3 Replies

Featured Tech Videos