怎樣cp文件夾時忽略指定的文件夾和文件


   在備份ltedecoder程序時,須要把此文件夾拷由到bak文件夾下。但decoder文件夾下有個大文件,不須要備份,還有日志問題,也不須要備份,怎樣實現呢??

方法:  

   cd /source-dir
        find . -name .snapshot-prune-o-print0 | cpio -pmd0 /dest-dir
解釋: This command copies the contents of /source-dir to /dest-dir, but omits files and directories  named  .snapshot (and  anything in them).  The construct-prune -o -print0  is quite common. The idea here is  that  the  expression  before  -prune  matches  things  which  are  to be pruned.  However, the -prune action itself returns true, so the following -o  ensures that the right hand side is evaluated only for those directories which didn't get pruned (the  contents  of the pruned directories are not even visited, so their contents are irrelevant).  The expression on the right  hand side of the -o is in parentheses only for clarity.  It emphasises that the -print0 action takes place only for  things  that  didn't have -prune applied to them.  Because the default `and' condition between tests binds more tightly than -o, this is the default anyway, but the parentheses help to show what is going on.

此段的意思是:當找到name為.snapshot時,prune就會返回真。-o是或的意思,“或”的使用技巧是當左邊的表達式為“真”時。則不會運算右邊的表達式。當左邊的表達式為假。則運算右邊的表達式,即,當左邊name不為.snapshot。則進行右邊的表達式,右邊的表達式意思是print文件名稱(包含路徑的);當找到name為.snapshot時,則不打印文件名稱。這樣就實現了把非.snampshot為命名的文件打印出來並交給cpio進行處理。cpio則把這些文件拷則的dest-dir中,實現忽略.snampshot文件的拷備(注:文件指目錄或file名)。

         拓展:

-prune -o ... -print0

“... -print0”

這里能夠使用find的查找文件的參數。如-name,-size等。

find . -name Persistence -prune -o  ! -name '*log*'  ! -size +100M -print0   | cpio -pmd0 /root/do_bak/ltedecoder

上面則不僅忽略Persistence文件夾,還會忽略包涵log的文件或文件夾,還會忽略大於100M的文件。

注意: 你一定注意到了find 后面是“.”,代表當前文件夾,即此命令必須是pwd是你要拷則的文件夾里面,如你把sour-dir的文件進行拷則,則須要先cd $sour-dir。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM