Shell command to tar directory excluding certain files/folders

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

    If you are trying to exclude Version Control System (VCS) files, tar already supports two interesting options about it! :)

    1. Option : --exclude-vcs

    This option excludes files and directories used by following version control systems: CVS, RCS, SCCS, SVN, Arch, Bazaar, Mercurial, and Darcs.

    As of version 1.32, the following files are excluded:

    • CVS/, and everything under it
    • RCS/, and everything under it
    • SCCS/, and everything under it
    • .git/, and everything under it
    • .gitignore
    • .gitmodules
    • .gitattributes
    • .cvsignore
    • .svn/, and everything under it
    • .arch-ids/, and everything under it
    • {arch}/, and everything under it
    • =RELEASE-ID
    • =meta-update
    • =update
    • .bzr
    • .bzrignore
    • .bzrtags
    • .hg
    • .hgignore
    • .hgrags
    • _darcs

      1. Option : --exclude-vcs-ignores

    When archiving directories that are under some version control system (VCS), it is often convenient to read exclusion patterns from this VCS' ignore files (e.g. .cvsignore, .gitignore, etc.) This option provide such possibility.

    Before archiving a directory, see if it contains any of the following files: cvsignore, .gitignore, .bzrignore, or .hgignore. If so, read ignore patterns from these files.

    The patterns are treated much as the corresponding VCS would treat them, i.e.:

    .cvsignore

    Contains shell-style globbing patterns that apply only to the directory where this file resides. No comments are allowed in the file. Empty lines are ignored.

    .gitignore

    Contains shell-style globbing patterns. Applies to the directory where .gitfile is located and all its subdirectories.

    Any line beginning with a # is a comment. Backslash escapes the comment character.

    .bzrignore

    Contains shell globbing-patterns and regular expressions (if prefixed with RE:(16). Patterns affect the directory and all its subdirectories.

    Any line beginning with a # is a comment.

    .hgignore

    Contains posix regular expressions(17). The line syntax: glob switches to shell globbing patterns. The line syntax: regexp switches back. Comments begin with a #. Patterns affect the directory and all its subdirectories.

    1. Example

    tar -czv --exclude-vcs --exclude-vcs-ignores -f path/to/my-tar-file.tar.gz path/to/my/project/

提交回复
热议问题