2011-08-04 13 views
0

OK、私はstumしました。私はランダムに.sctの拡張子を持つファイルを選択するシェルスクリプトが必要です。ファイル名の一部を使用すると、関連する6つの.motファイルが選択されます。その後、それらをすべて別のフォルダに移動します。私はまたランダムに選択するファイルの数のユーザー入力が必要です。extentionでランダムファイルを選択し、接続されたファイルを選択してください

 
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 

などなど:

だから私はこのようなファイル構造を持っています。ランダムにファイル.sctを選択し、それとその関連ファイルを別のディレクトリに移動する必要があります。うまくいけば私はこれを十分に説明した。

ありがとうございました。私はVBでこれを行うことができますが、このUNIXの事は私stumppedしています。今、私たちは何千ものファイルをマニュアルで扱います。

はスコット

+0

おそらくアイデアはここで見つけることができます:http://stackoverflow.com/questions/414164/how-can-i-select-random-files-from-a-directory-in-bash – Nobody

答えて

1

このスクリプトは123-12345などという名前のディレクトリに123-12345-*.sct123-12345-*.motファイルを移動します。

注:ファイルをランダムに選択するのではなく、ディレクトリ内のすべてのファイルをランダムに選択します。これを変更して、ランダムファイル数に対するコマンドライン引数を受け入れることができます。次に、このコマンドls [0-9]*.sct | grep -oe '[0-9]\{3\}\-[0-9]\{5\}'を変更して、ファイルの数であるコマンドライン引数を使用して、プレフィックスの乱数を返します。

あなたのsctとmotファイルと同じディレクトリにあるmv_sct_mot.shというファイルに以下をコピーしてください。

#!/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 

ファイルを実行可能で、それはのような権限です変更するには:

chmod +x mv_sct_mot.sh 

それが好きなファイル名を指定して実行:

./mv_sct_mot.sh 
+0

あなたはなぜモーターを選んでいますか?後でそれらを捨てるためのファイル?そしてまた:ランダムな部分はどこに来ますか? – Nobody

+0

ランダム部分の明確化のために編集されています。モットファイルは破棄されません。それらはターゲットディレクトリに移動されます。 –

+1

私は次のことを意味しています:lsは.sctファイルだけを選択すれば十分な.sctと.motファイルをすべて選択します。ダブルの.sctファイル(123-12345-00.sctと123-12345-01.sctのような二重の意味)がない場合、この方法でソートして一意にする必要はありません。 – Nobody

1
#! /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>" 

がそれを行うだろう。

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


 
/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 
+0

+1:恐ろしい!スコットは最高のシャンパンのボトルを頼んでいます!皆さんお元気で。 – shellter

0

が、私はこれに近づくだろうかだ私は、最初はファイルの数は、現在のスクリプトを更新し、パラメータであり、一部を逃しました。わかりやすくするためにSRCDIRとDESTDIRを設定した部分はスキップします。

関連する問題