Shell command to tar directory excluding certain files/folders

后端 未结 28 2200
春和景丽
春和景丽 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:17

    I agree the --exclude flag is the right approach.

    $ tar --exclude='./folder_or_file' --exclude='file_pattern' --exclude='fileA'
    

    A word of warning for a side effect that I did not find immediately obvious: The exclusion of 'fileA' in this example will search for 'fileA' RECURSIVELY!

    Example:A directory with a single subdirectory containing a file of the same name (data.txt)

    data.txt
    config.txt
    --+dirA
      |  data.txt
      |  config.docx
    
    • If using --exclude='data.txt' the archive will not contain EITHER data.txt file. This can cause unexpected results if archiving third party libraries, such as a node_modules directory.

    • To avoid this issue make sure to give the entire path, like --exclude='./dirA/data.txt'

提交回复
热议问题