2017-04-20 16 views
0

から新規追加され、削除されたファイルのためのマッピングファイルとディレクトリのファイルを比較はディレクトリ私のようにいくつかのファイルを持っているのと同じディレクトリ

私が比較したい
a.txt 
b.txt 
c.txt 
d.txt 
e.txt 
f.txt 

(ファイル名のみの内容ではなくファイル名)MAP_FILEにあるファイル名とディレクトリに終了しファイル。

  1. 新しいファイルがディレクトリにある場合(map_fileで終了しない場合)、ファイルを別のディレクトリにコピーして名前を変更します。
  2. ファイルがディレクトリに存在しない場合(map_fileごとに)、コピーしていくつかのディレクトリに名前を変更します。

以下のサンプルコードでは、私は最初の条件を取得することはありません。

Add-z.txt     Del-d.txt 
          Del-e.txt 
          Del-f.txt 

答えて

0
cd path/to/directory 
map_file=path/to/map.txt 
for file in `ls -lrt *.txt | awk '{print $9}'` 
do 
if [[ ! $(grep "$file" $map_file) ]]; 
then 
    echo "$file is not present in map_file" 
    #cp ${file} ../Add-${file} 
else 
    while IFS= read line 
    do 
     if [ ! -f "$line" ]; 
     then 
      #touch ../Del-${line} 
     fi 
    done <"$map_file" 
fi 
done 

私の答えを見て、さらに最適化することができれば返信してください:私が使用している

のサンプルコードでは、最終的な出力が見えるはずです

for file in *.txt 
do 
if [[ ! $(grep "$file" /path/to/map_file) ]]; then 
    echo "$file is not in map_file -- copy/rename somewhere else." 
    cp ${file} ../Add-${file} 
fi 
done 

です。

関連する問題