Skip to content
Fix Code Error

Shell command to tar directory excluding certain files/folders

March 13, 2021 by Code Error
Posted By: Anonymous

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 directory that has a number of very large files I do not need to backup.

Not quite solutions:

The tar --exclude=PATTERN command matches the given pattern and excludes those files, but I need specific files & folders to be ignored (full file path), otherwise valid files might be excluded.

I could also use the find command to create a list of files and exclude the ones I don’t want to archive and pass the list to tar, but that only works with for a small amount of files. I have tens of thousands.

I’m beginning to think the only solution is to create a file with a list of files/folders to be excluded, then use rsync with --exclude-from=file to copy all the files to a tmp directory, and then use tar to archive that directory.

Can anybody think of a better/more efficient solution?

EDIT: Charles Ma‘s solution works well. The big gotcha is that the --exclude='./folder' MUST be at the beginning of the tar command. Full command (cd first, so backup is relative to that directory):

cd /folder_to_backup
tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

Solution

You can have multiple exclude options for tar so

$ tar --exclude='./folder' --exclude='./upload/folder2' -zcvf /backup/filename.tgz .

etc will work. Make sure to put --exclude before the source and destination items.

Answered By: Anonymous

Related Articles

  • Ubuntu apt-get unable to fetch packages
  • Can't install via pip because of egg_info error
  • AppCompat v7 r21 returning error in values.xml?
  • Design DFA accepting binary strings divisible by a…
  • easiest way to extract Oracle form xml format data
  • Issue with iron-ajax request
  • Docker compose fails to start a service with an…
  • Easy interview question got harder: given numbers…
  • gzip: stdin: not in gzip format tar: Child returned…
  • javascript .replace and .trim not working in vuejs
  • Circle line-segment collision detection algorithm?
  • How do i arrange images inside a div?
  • Fix top buttons on scroll of list below
  • Having trouble with my nav bar/header, It used to…
  • Git command to show which specific files are ignored…
  • How are zlib, gzip and zip related? What do they…
  • Examples of GoF Design Patterns in Java's core libraries
  • How to deal with persistent storage (e.g. databases)…
  • Fixing a systemd service 203/EXEC failure (no such…
  • Reversing a string in C
  • Tar a directory, but don't store full absolute paths…
  • How do I ignore files in Subversion?
  • How to parse an RSS feed using JavaScript?
  • What are the undocumented features and limitations…
  • C++ template,typename and operator
  • TypeError: Cannot read property 'webpackJsonp' of undefined
  • How to backup a local Git repository?
  • Maven Jacoco Configuration - Exclude…
  • Sort table rows In Bootstrap
  • What is "android:allowBackup"?
  • How do you clear the SQL Server transaction log?
  • Backup database in Laravel 8
  • How the int.TryParse actually works
  • I am getting the following error while creating the…
  • How do SO_REUSEADDR and SO_REUSEPORT differ?
  • data.table vs dplyr: can one do something well the…
  • How to extract filename.tar.gz file
  • How to use Regular Expressions (Regex) in Microsoft…
  • Flex: REJECT rejects one character at a time?
  • How to remove element from array in forEach loop?
  • How to use html template with vue.js
  • Onclick, the button moves down, why can that due to?
  • How to handle Vue 2 memory usage for large data (~50…
  • Spark EMR job jackson error -…
  • What is the origin of foo and bar?
  • How to permanently set $PATH on Linux/Unix?
  • Pandas Merging 101
  • Backup/Restore a dockerized PostgreSQL database
  • Git submodule head 'reference is not a tree' error
  • Text editor to open big (giant, huge, large) text files
  • Create a tar.xz in one command
  • Flutter: RenderBox was not laid out
  • How can I exclude all "permission denied" messages…
  • useEffect Error: Minified React error #321 (GTM…
  • Database development mistakes made by application developers
  • What is your most productive shortcut with Vim?
  • How to take MySQL database backup using MySQL Workbench?
  • Do a "git export" (like "svn export")?
  • The definitive guide to form-based website authentication
  • Why is it that "No HTTP resource was found that…
  • Editing ZIP archive in place in Golang
  • How does PHP 'foreach' actually work?
  • How should a model be structured in MVC?
  • Javascript: Push Filtered Array to Existing Array
  • How can I draw a curly brace under my matrix to…
  • Vue.js ignore html in textarea
  • Deploying Maven project throws…
  • Logging best practices
  • How to get $HOME directory of different user in bash script?
  • How can I find the product GUID of an installed MSI setup?
  • Smart way to truncate long strings
  • Cast/initialize submodels of a Backbone Model
  • Define make variable at rule execution time
  • Active tab issue on page load HTML
  • Why does C++ code for testing the Collatz conjecture…
  • Start redis-server with config file
  • Problems installing csvtk with Docker using…
  • how to integrate selenium driver for chrome and…
  • ExpressJS How to structure an application?
  • How to display gpg key details without importing it?
  • Ukkonen's suffix tree algorithm in plain English
  • How to install Intellij IDEA on Ubuntu?
  • download & install openshift cli command not working
  • Converting file into Base64String and back again
  • Memcached vs. Redis?
  • Excluding directory when creating a .tar.gz file
  • How can a maven build fail when certain dependencies…
  • How do I install Java on Mac OSX allowing version switching?
  • Convert array to nested JSON object - Angular Material tree
  • How to config backups of Azure Managed Disks from Terraform?
  • "Large data" workflows using pandas
  • How to check if a string contains only digits in Java
  • Callback functions in C++
  • PHP - Failed to open stream : No such file or directory
  • Install pip in docker
  • How can I initialize a MySQL database with schema in…
  • Making macOS Installer Packages which are Developer ID ready
  • How do I print out the contents of a vector?
  • Complex nesting of partials and templates
  • gpg: decryption failed: No secret key without first…

Disclaimer: This content is shared under creative common license cc-by-sa 3.0. It is generated from StackExchange Website Network.

Post navigation

Previous Post:

How can I represent an ‘Enum’ in Python?

Next Post:

Ways to iterate over a list in Java

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

.net ajax android angular arrays aurelia backbone.js bash c++ css dataframe ember-data ember.js excel git html ios java javascript jquery json laravel linux list mysql next.js node.js pandas php polymer polymer-1.0 python python-3.x r reactjs regex sql sql-server string svelte typescript vue-component vue.js vuejs2 vuetify.js

  • you shouldn’t need to use z-index
  • No column in target database, but getting “The schema update is terminating because data loss might occur”
  • Angular – expected call-signature: ‘changePassword’ to have a typedeftslint(typedef)
  • trying to implement NativeAdFactory imports deprecated method by default in flutter java project
  • What should I use to get an attribute out of my foreign table in Laravel?
© 2022 Fix Code Error