bash if 文のヘルプ

bash if 文のヘルプ

現在2つのスクリプトを使用しています。年ごとにファイルをフィルタリングするスクリプト1つ。 2番目のスクリプトは年サブフォルダにあり、月(01-12)ごとにフィルタリングされます。

以下のものより良いものがありますか?

すべてのファイルは次の1つのディレクトリにあります。

ソース: ./tape_backup/sync1/* (140万ファイル) ターゲット: ./tape_backup/<1990 - 2019>/<01-12>/ (年/月ごとに構成)

ファイル名構文:A1000_T195_R256393_D120498094600

_D = 重要ではない

D = 開始日

1-2 = 月

3-4 =日(重要ではない)

5-6 = 3

7-* =タイムスタンプ(重要ではない)

だから私が全部やるんだ98

次のように読みます。

for f in ./sync1 do
(月と年を確認してから月と年のフォルダに移動/)
完了

Cygwinとサーバー2012 r2を使用しています。

#C:/cygwin/bin/bash
for filename in ./* ; do
 if [[ $filename == *D??90 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1990/

 elif [[ $filename == *D??91 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1991/

 elif [[ $filename == *D??92 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1992/

 elif [[ $filename == *D??93 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1993/

 elif [[ $filename == *D??94 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1994/

 elif [[ $filename == *D??95 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1995/

 elif [[ $filename == *D??96 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1996/

 elif [[ $filename == *D??97 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1997/

 elif [[ $filename == *D??98 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1998/

 elif [[ $filename == *D??99 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/1999/

 elif [[ $filename == *D??00 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2000/

 elif [[ $filename == *D??01 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2001/

 elif [[ $filename == *D??02 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2002/

 elif [[ $filename == *D??03 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2003/

 elif [[ $filename == *D??04 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2004/

 elif [[ $filename == *D??05 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2005/

 elif [[ $filename == *D??06 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2006/

 elif [[ $filename == *D??07 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2007/

 elif [[ $filename == *D??08 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2008/

 elif [[ $filename == *D??09 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2009/

 elif [[ $filename == *D??10 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2010/

 elif [[ $filename == *D??11 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2011/

 elif [[ $filename == *D??12 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2012/

 elif [[ $filename == *D??13 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2013/

 elif [[ $filename == *D??14 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2014/

 elif [[ $filename == *D??15 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2015/

 elif [[ $filename == *D??16 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2016/

 elif [[ $filename == *D??17 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2017/

 elif [[ $filename == *D??18 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2018/

 elif [[ $filename == *D??19 ]] ; then
      mv $filename /cygdrive/d/RAID5/RAID200/invoices/1/2019/
 fi
done

ベストアンサー1

異なるドライブ間でコンテンツを移動するために140万回の呼び出しを実行すると、速度がmv遅くなります。mv通話を減らしてください。

??パターンのパターンが強度と一致する必要があるとします。

for year in {1990..2019}; do
    find . -maxdepth 1 -type f -name "*D$year" \
        -exec mv -t "/cygdrive/d/RAID5/RAID200/invoices/1/$year/" {} +
done

これは関連するすべての年を繰り返します。毎年find、特定のパターンに一致するファイルをできるだけ一度に一括移動するコマンドを実行します。

このコードは、GNU mv(該当する-tオプションについて)とGNU find(またはfindそのオプションがあるすべて-maxdepth)を使用していると想定していますbash。ソースディレクトリにサブディレクトリがない場合は、-maxdepth 1コマンドから削除できます。

おすすめ記事