Reputation: 1
OK, I am stumpped. I need a shell Script that will randomly select a file with the extention of .sct. The 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 for the number of files to randomly select.
So I have a file structure like this:
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 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.
Scott
Upvotes: 0
Views: 296
Reputation: 38255
Here's how I would approach this:
#!/bin/bash
## use the first param as count (1 as default value)
count=${1:-1}
## list all .sct files, random sort and pick the first $count
ls $SRCDIR/*.sct | sort -R | head -n $count |
while read file; do
## for each file, figure out the prefix and move prefix*
prefix="${file%-*}-"
mv -v $prefix* $DESTDIR
done
EDIT: I initially missed the part where the number of files is a parameter, updated the script now. I skipeed the part where you set SRCDIR and DESTDIR just for clarity.
Upvotes: 0
Reputation: 22210
#! /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 #no more .sct file, exiting
sct=${files[$[($RANDOM % $ct)]]} #pick random file
# You might want to change this according to your file names
# Everything before the last dash `-' (included) will be taken
# as prefix
prefix=$(echo $sct | sed 's:\(.*-\).*:\1:')
mot_files=($prefix*.mot) #creates array of all matching .mot
mv $sct $dir #moves .sct to $dir
if [ ${#mot_files[@]} -gt 0 ]; then
mv ${mot_files[@]} $dir #moves each matching .mot to $dir
fi
done
} || echo "usage: $0 <dir> <num of files>"
Would do it.
/tmp/r > ls 123-12345-00.mot 123-12345-05.mot 123-12346-04.mot 123-12345-00.sct 123-12346-00.mot 123-12346-05.mot 123-12345-01.mot 123-12346-00.sct 123-12348-00.mot 123-12345-02.mot 123-12346-01.mot 123-12348-00.sct 123-12345-03.mot 123-12346-02.mot foo 123-12345-04.mot 123-12346-03.mot /tmp/r > mkdir bar /tmp/r > ./foo bar 2 /tmp/r > ls 123-12346-00.mot 123-12346-02.mot 123-12346-05.mot 123-12346-00.sct 123-12346-03.mot bar 123-12346-01.mot 123-12346-04.mot foo /tmp/r > ls bar 123-12345-00.mot 123-12345-02.mot 123-12345-05.mot 123-12345-00.sct 123-12345-03.mot 123-12348-00.mot 123-12345-01.mot 123-12345-04.mot 123-12348-00.sct
Upvotes: 1
Reputation: 3355
This script moves the 123-12345-*.sct
and 123-12345-*.mot
files to a directory named 123-12345
and so on.
Note: This does not randomly select a file, but all files within the directory. You can modify this to accept a command line argument for the number of random files. Then you'll need to modify this command ls [0-9]*.sct | grep -oe '[0-9]\{3\}\-[0-9]\{5\}'
to use your command line argument which is a count of files and return a random number of prefixes.
Copy the below to a file, say mv_sct_mot.sh
within the same directory as your sct and mot files.
#!/bin/bash
for prefix in `ls [0-9]*.sct | grep -oe '[0-9]\{3\}\-[0-9]\{5\}'`; do
mkdir -p ${prefix};
mv ${prefix}-*.{mot,sct} ${prefix};
done
To make the file executable modify it's permission like:
chmod +x mv_sct_mot.sh
Run it like:
./mv_sct_mot.sh
Upvotes: 1