-->

Create tar Archive excluding certain Files or Directories

Author : Piyush Gupta

To avoid operating on files whose names match a particular pattern, use the `--exclude' or `--exclude-from' options.
--exclude=pattern
Causes tar to ignore files that match the pattern.

The `--exclude=pattern' option prevents any file or member whose name matches the shell wildcard (pattern) from being operated on. For example, to create an archive with all the contents of the directory `src' except for files whose names end in `.o', use the command 
`tar -cf src.tar --exclude='*.o' src'.
Example:

  • Below command will archive all files and directories under public_html directory except wp-content/cache directory and create a archive file named backup.tar.gz.

tar czf backup.tar.gz --exclude "wp-content/cache" public_html
  • Exclude a single directory inside other directory. Create archive of all files under public_html directory ignoring all files and folders including text backup in there named
    tar czf backup.tar.gz --exclude "wp-content/uploads" public_html
    
  • You can define multiple exclude in single command. For example create and archive of all files under public_html directory ignoring .svn or .git files and directories.
    tar czf backup.tar.gz --exclude ".svn" --exclude ".git" public_html
    
  • To exclude a specific file define full path of the file. For example exclude “logs/access.log” inside public_html directory.
    tar czf backup.tar.gz --exclude "logs/access.log" public_html
    
  • Create archive of all files under public_html directory ignoring all files and directories starting with dot ( . ) (hidden files).
    tar czf backup.tar.gz --exclude "*.log" public_html


No comments:

Post a Comment