2016-08-02 61 views
0

我需要查找大於45天的文件,將它們壓縮並將它們移動到壓縮文件夾並刪除原始文件。單命令查找文件,壓縮並移動到不同的位置,刪除原始文件

find . -name '*.dat' -exec zip '{}.zip' '{}' ';' -exec mv '{}' ~/archive/ \; 

上面的命令似乎工作,但原始文件仍在文件夾中。

+0

你爲什麼不做一個很好的腳本? –

+0

我有一個腳本來清理不同用例的文件,其中大部分都是一行。所以我儘管我會嘗試使用find將所有內容放在一行中。 – coolnags

+0

您可以在腳本中創建一個功能。調用該函數也是1行。 –

回答

0

男人拉鍊​​

-m 
    --move 
      Move the specified files into the zip archive; actually, this 
      deletes the target directories/files after making the specified 
      zip archive. If a directory becomes empty after removal of the 
      files, the directory is also removed. No deletions are done 
      until zip has created the archive without error. This is useful 
      for conserving disk space, but is potentially dangerous so it is 
      recommended to use it in combination with -T to test the archive 
      before removing all input files. 
0

# find . -mtime +45 -name '*.dat' -exec zip '{}.zip' '{}' ';' -exec mv '{}' ../. ';' -exec rm '../{}' \;

上面的命令是好的,
順便說一句。爲什麼,然後移動到刪除?這似乎並不滿足。

+0

可能是我混淆了我的描述。當我試圖壓縮文件時,它保留原來的文件。我需要的只是找到超過45天的文件,將它們壓縮並將它們移動到歸檔目錄。 – coolnags

+0

我有命令「find。-name」* .gz *「-print | barges -I mv {}〜/ archive 該命令正​​常,但是會報錯如下 mv:'/ home//test.dat.gz'和'/ home/ /test.dat.gz'是同一個文件 但是文件正在移動到正確存檔 這個錯誤的原因是什麼 – coolnags

+0

運行路徑在哪裏?也許因爲'find'找到了一個'archive' –

相關問題