Bashスクリプトでフォルダをサブフォルダに分割する方法

Bashスクリプトでフォルダをサブフォルダに分割する方法

毎秒*.DATファイルを常に受け​​取るフォルダがあります。このフォルダを2つのフォルダに分割したいです。ここで、親フォルダ(2つに分割したいフォルダ)フォルダは、これら2つのサブフォルダから受信したすべての*を循環型の.DATファイルに移動します。 Bashスクリプトでこれを行う方法はありますか?

ベストアンサー1

あなたのスクリプトがあります:

#!/bin/bash
# enter the source dir
cd /tmp/a

# set initial subdir
subdir="b"

# run forever
while true
do
        # get first available *.DAT file
        newfile=`ls -1 *.DAT 2>/dev/null | head -n1`
        if [ "$newfile" != "" ]
        then
                # if the .DAT file exists, move it
                mv ./$newfile /tmp/$subdir/

                # replace subdir for next loop iteration
                if [ "$subdir" == "b" ]
                then
                        subdir="c"
                else
                        subdir="b"
                fi
        else
                # nothing found, wait 1 second
                sleep 1
        fi
done

テストには平面構造を使用します。

/tmp/a # source dir
/tmp/b # destdir 1
/tmp/c # destdir 2

状況に合わせて修正する必要がありますが、正常に動作します。

おすすめ記事