Shell command to tar directory excluding certain files/folders

后端 未结 28 2269
春和景丽
春和景丽 2020-11-27 08:31

Is there a simple shell command/script that supports excluding certain files/folders from being archived?

I have a directory that need to be archived with a sub dire

28条回答
  •  鱼传尺愫
    2020-11-27 09:38

    To avoid possible 'xargs: Argument list too long' errors due to the use of find ... | xargs ... when processing tens of thousands of files, you can pipe the output of find directly to tar using find ... -print0 | tar --null ....

    # archive a given directory, but exclude various files & directories 
    # specified by their full file paths
    find "$(pwd -P)" -type d \( -path '/path/to/dir1' -or -path '/path/to/dir2' \) -prune \
       -or -not \( -path '/path/to/file1' -or -path '/path/to/file2' \) -print0 | 
       gnutar --null --no-recursion -czf archive.tar.gz --files-from -
       #bsdtar --null -n -czf archive.tar.gz -T -
    

提交回复
热议问题