Random File Selection and Moving


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Random File Selection and Moving
# 1  
Old 08-04-2011
Random File Selection and Moving

OK, I am stumpped. I have this shell Script that I want to randomly select a file with the extention of .sct. Then using a portion of its file name select the six related .mot files. Then move them all to another folder. I also need a user input form for the number of .SCT files to randomly select and move.
So I have a file structure like this:
Code:
123-12345-00.sct
123-12345-00.mot
123-12345-01.mot
123-12345-02.mot
123-12345-03.mot
123-12345-04.mot
123-12345-05.mot
123-12346-00.sct
123-12346-00.mot
123-12346-01.mot
123-12346-02.mot
123-12346-03.mot
123-12346-04.mot
123-12346-05.mot

And so on.

Need to randomly select the file .sct and move it and its related .mot files to another directory. Hopefully I have explained this good enough.
Thanks for the help. I could do this in VB but this UNIX thing has me stumpped. Right now we do it manualy through thousands of files.

So here is the script I want to use. It isn't working I can't get past the
Code:
 if [ ! -d "$dir" ]; then echo "$0: $dir: no such directory"; exit; fi;


Code:
  #! /usr/bin/env bash
    dir="$1"
    count="$2"
    [ "$dir" ] && [ $count -gt 0 ] && {
           if [ ! -d "$dir" ]; then echo "$0: $dir: no such directory"; exit; fi;
           RANDOM=$$$(date +%s)                    #init random seed
           for (( c=0; c < $count; c++ )); do
                files=(*.sct)                       #creates array of sct files
                  ct=${#files[@]}                     #computes array length
                if [ $ct -eq 0 ]; then break; fi
                sct=${files[$[($RANDOM % $ct)]]}    #pick random file
                prefix=$(echo $sct | sed 's:\(.*-\).*:\1:')
                mot_files=($(ls $prefix*.mot 2> /dev/null))
                mv $sct $dir
            if [ ${#mot_files[@]} -gt 0 ]; then 
                mv ${mot_files[@]} $dir  #
            fi
            done
        } || echo "usage: $0 <dir> <num of files>"

Thanks for any help you can provide.

Scott

Last edited by pludi; 08-04-2011 at 10:35 AM..
# 2  
Old 08-04-2011
At first glance this scripts looks like working one. Try to simplify it (check for args at first and do the work at second) and debug it with bashdb. I'm sure if you can write this one by yourself you can easily debug it and find a mistake.
# 3  
Old 08-04-2011
I appoligize. I reread my post and I want to make it clear that I didn't write this code. I got it from another site. I have been trying to make it work all day and I can't. I am very new to Shell Scripting and I am trying to use this to automate a very time consuming process. Sorry for the confusion. It has been a long day out here.

Thanks for any help you can provide. It appears that the code will not create the directory.

Scott
# 4  
Old 08-04-2011
Insert set -x as the second line in your script, and please post the output. Maybe there's an error somewhere in there after all.
# 5  
Old 08-04-2011
So the UNIX machine isn’t connected to an internet connection so I had to write it down and type it in to the windows machine. Thanks for the help.
Here is the output:

Code:
++ dir=$’\r’
++ count=$’\r’
++ ‘[‘ $ ‘\r’ ‘]’
++ ‘[‘ –gto ‘]’
./random_select.sh: line 4: [:-gt:unary operator expected
++ ‘[‘ ‘!’ –d$ ‘\r’ ‘]’
: no such directory ‘lect.sh
:no such directory
++ exit


Last edited by pludi; 08-04-2011 at 04:49 PM..
# 6  
Old 08-04-2011
Are you using PuTTY to connect to that machine? If so, anything you highlight using the mouse is automatically copied to the clipboard.

Am I right in assuming that you called this script without parameters? And that you wrote it using Notepad? Because I'm seeing an awful lot of carriage returns (\r) here. On the command prompt, run
Code:
tr -d '\r' random_select.sh

to clear them out. How are you transferring the file? If by FTP, change the transfer type to ASCII to have the system do that automatically.
# 7  
Old 08-05-2011
Pludi,

Thanks for the reply. I tried to send you a PM explaining the situation. Unfortunately I could not.

So:
Are you using PuTTY to connect to that machine?
No, i can not connect to that machine from my unclassified computer. I am doing the pen and paper drill.

Am I right in assuming that you called this script without parameters?
yes, just ./

And that you wrote it using Notepad?
Yes, i wrote it in notepad on the unclass machine and brought it over to the other machine.

How are you transferring the file?
They are for this conversation just in the file location on the UNIX machine.

Thank you for your continued support. When i get back in I will try the code you mentioned. Thanks

Scott
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. UNIX for Advanced & Expert Users

Support for Unicode in GTK2 and GTK3 file selection box?

I'm on Tiny Core Linux Pure64 10.1. My locale is en_US.UTF-8 and I generally have no trouble with Unicode characters with one exception: When I try to use Unicode characters in GTK applications' file selection box, I get "Invalid file name": http://files.dantas.airpost.net/public/save_file.jpg ... (11 Replies)
Discussion started by: DevuanFan
11 Replies

2. Shell Programming and Scripting

Conditional File Selection From a Directory

I have a directory with files that was saved all along the day at different times. Every day I have to run a program that will select 50 files for each hour which has the maximum score. Sample file name: a_b_c_d_e_f where a,b,c,d,e,f are double values Score calculation: (a + b + c + d + e... (4 Replies)
Discussion started by: shekhar2010us
4 Replies

3. Shell Programming and Scripting

Need to generate a file with random data. /dev/[u]random doesn't exist.

Need to use dd to generate a large file from a sample file of random data. This is because I don't have /dev/urandom. I create a named pipe then: dd if=mynamed.fifo do=myfile.fifo bs=1024 count=1024 but when I cat a file to the fifo that's 1024 random bytes: cat randomfile.txt >... (7 Replies)
Discussion started by: Devyn
7 Replies

4. UNIX for Dummies Questions & Answers

Random selection of subset of sample from file

Hello Could you please help me to find a code that can randomly select 1224 lines from a file of 12240 and make tn output with 1224 line each. my input is txt file with 12240 lines like : 13474 999003507 0 0 2 -9 13475 999003508 0 0 2 -9 13476 999003509 0 0 1 -9 13477 999003510 0 0 1 -9 ... (7 Replies)
Discussion started by: biopsy
7 Replies

5. Ubuntu

expect script for random password and random commands

Hi I am new to expect. Please if any one can help on my issue its really appreciable. here is my issue: I want expect script for random passwords and random commands generation. please can anyone help me? Many Thanks in advance (0 Replies)
Discussion started by: vanid
0 Replies

6. Shell Programming and Scripting

Pick random file from ls command.

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. (3 Replies)
Discussion started by: elbombillo
3 Replies

7. Shell Programming and Scripting

how to change the current file processing to some other random file in awk ?

Hello, say suppose i am processing an file emp.dat the field of which are deptno empno empname etc now say suppose i want to change the file to emp.lst then how can i do it? Here i what i attempted but in vain BEGIN{ system("sort emp.dat > emp.lst") FILENAME="emp.lst" } { print... (2 Replies)
Discussion started by: salman4u
2 Replies

8. Shell Programming and Scripting

Random lines selection form a file.

>cat data.dat 0001 Robbert 0002 Nick 0003 Mark ....... 1000 Jarek (3 Replies)
Discussion started by: McLan
3 Replies

9. Programming

[C++] File I/O (Reading from a Random-Access File)

INFO: The program should enter a circle radius and Id for that circle to a file, then it should search for that id and print the radius for that circle. PROBLEM: This program compiles but it's not searching properly. Circle.h #ifndef CIRCLE_H #define CIRCLE_H #include <iostream>... (0 Replies)
Discussion started by: VersEtreOuNe
0 Replies

10. Shell Programming and Scripting

Getting a random file

Hello, I am very new to shell scripting. This problem seems quite easy so it should be quite easy (I hope ^^) I want to get a random file from a directory. this file will be in one subdirectory, and it will contain spaces. code I have got so far: N=find ./*/*.jpg | wc -l ((N=RANDOM%N)) ... (6 Replies)
Discussion started by: davidY
6 Replies
Login or Register to Ask a Question