2017-04-20 299 views
0

新增和刪除的文件映射文件的目錄中,我有幾個文件爲:比較目錄中的文件與從相同的目錄

a.txt 
b.txt 
c.txt 
z.txt 

,並在不同的目錄中我有一個名爲MAP_FILE與像數據的文件名:

a.txt 
b.txt 
c.txt 
d.txt 
e.txt 
f.txt 

我想比較(只有文件名不包含文件名的內容)文件在目錄中退出時使用map_file中的文件名。

  1. 如果在目錄中找到新文件(不退出map_file),則將文件複製並重命名爲其他目錄。
  2. 如果文件不存在於該目錄(按照MAP_FILE),則還複製和重命名一些目錄。

下面的示例代碼,我只獲得了第一個條件不能同時使用。我使用的

示例代碼

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 

所以最終的輸出應該是這樣的:

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 

請看看我的答案和迴應,如果能進一步優化。

相關問題