Excluding files from a tar archive
I was trying to make a backup, but exclude my working directory, which just has a lot of temp files in it.
. . . easier said than done . . .
The concept is pretty easy, simply use the --exclude="file2exclude" switch option in the command, and the file(s)/directory(ies) listed will be excluded.
It didn't work.
Doing some digging into it, I found various posts about where to place the --exclude switch.
The tl;dr version is to put the --exclude between the archive location, and what is being added to the archive.
-
I'm running Ubuntu 20.04, which comes with
tar
version 1.3.0:tar --version tar (GNU tar) 1.30 Copyright (C) 2017 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>. This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by John Gilmore and Jay Fenlason.
-
In this example, I have a file structure that looks like this:
tree . ├── contact.html ├── db.bkup.tar.gz ├── index.html ├── internal │ ├── import.db │ └── scratchfile.txt ├── passwordfile.txt └── public 2 directories, 6 files
I want to exclude the internal directory, and also the passwordfile.txt file from the archive.
-
Some posts said to put the --exclude switch at the end of the tar command, but that didn't work:
tar cfvz ../backup.tar.gz * --exclude="internal" --exclude="passwordfile.txt" contact.html db.bkup.tar.gz index.html internal/ internal/import.db internal/scratchfile.txt passwordfile.txt public/ tar: The following options were used after any non-optional arguments in archive create or update mode. These options are positional and affect only arguments that follow them. Please, rearrange them properly. tar: --exclude ‘internal’ has no effect tar: --exclude ‘passwordfile.txt’ has no effect tar: Exiting with failure status due to previous errors
-
Others said to put it before the other
tar
switches, but that only puked:tar --exclude="internal" --exclude="passwordfile" cfvz ../backup.tar.gz * tar: You must specify one of the '-Acdtrux', '--delete' or '--test-label' options Try 'tar --help' or 'tar --usage' for more information.
-
Still others said to put it after the other switches, but before the archive name:
tar cfvz --exclude="internal" --exclude="passwordfile" ../backup.tar.gz. * tar: Removing leading `../' from member names tar: ../backup.tar.gz.: Cannot stat: No such file or directory contact.html db.bkup.tar.gz index.html internal/ internal/import.db internal/scratchfile.txt passwordfile.txt public/ tar: Exiting with failure status due to previous errors
This was the worst so far, as it didn't work, and now I've got a --exclude=internal file in the directory!
ls contact.html db.bkup.tar.gz '--exclude=internal' index.html internal passwordfile.txt public
To remove a file that starts with a dash
-
, you need to userm --
orrm ./
before the filename.rm -- --exclude=internal
-
After much trial and error, I figured out that the --exclude needs to go in between the archive name and what is being added to the archive:
tar cfvz ../backup.tar.gz --exclude="internal" --exclude="passwordfile.txt" * contact.html db.bkup.tar.gz index.html public/
References
GNU tar: an archiver tool - 6.4 Excluding Some Files https://www.gnu.org/software/tar/manual/html_node/exclude.html