Gentoo Archives: gentoo-user

From: tastytea <gentoo@××××××××.de>
To: gentoo-user@l.g.o
Subject: Re: [gentoo-user] [OT] tar exclude syntax tip
Date: Wed, 05 May 2021 14:03:33
Message-Id: 20210505160303.58ba6a67@ventiloplattform.tastytea.de
In Reply to: [gentoo-user] [OT] tar exclude syntax tip by Walter Dnes
1 On 2021-05-05 09:33-0400 "Walter Dnes" <waltdnes@××××××××.org> wrote:
2
3 > tar version
4 >
5 > ####################################################
6 > tar (GNU tar) 1.34
7 > Copyright (C) 2021 Free Software Foundation, Inc.
8 > License GPLv3+: GNU GPL version 3 or later
9 > <https://gnu.org/licenses/gpl.html>. This is free software: you are
10 > free to change and redistribute it. There is NO WARRANTY, to the
11 > extent permitted by law.
12 >
13 > Written by John Gilmore and Jay Fenlason.
14 > ####################################################
15 >
16 > I'm passing on this solution to help others avoid my frustration and
17 > wasted time. If you've done "RTFM" on tar, you'll find out that "TFM"
18 > is broken or out-of-date or whatever, re: "--exclude=PATTERN". I'm
19 > fighting the urge to turn this into a rant. Here's my situation...
20 >
21 > I either log in as root or "su -" and then "cd /home". I want to
22 > tar up /home/waltdnes, and transfer it to another machine. While I'm
23 > at it, I want to exlude directory /home/waltdnes/.cache/ and all *.xz
24 > files in directory /home/waltdnes/pm/ The "--exclude=" never worked.
25 > After much hair pulling, I was ready to give up on the exclude, and
26 > simply transfer all the unnecessary garbage.
27 >
28 > Then "I asked Mr. Google". It seems that I wasn't the only person
29 > running into problems. After some searching, I finally found a syntax
30 > that works...
31 >
32 > ####################################################
33 > #!/bin/bash
34 > export GZIP=-9
35 > tar cvzf wd.tgz --exclude ".cache/*" --exclude "pm/*.xz" waltdnes
36 > ####################################################
37 >
38 > Notes...
39 >
40 > 1) This is obviously not in line with the man page. Specifically,
41 > "--exclude" is followed by one space, not an equals sign.
42 >
43 > 2) ***THERE MUST BE EXACTLY ONE SPACE BETWEEN EACH WORD***
44 >
45 > 3) All directories and/or files to exclude must be listed as relative
46 > paths to the directory being tarred, i.e. last parameter on the
47 > command line.
48 >
49 > 4) I don't know the maximum line-length, which would limit the number
50 > of --exclude entries. In those cases, I wonder if
51 > "--exclude-from=FILE" works as "--exclude-from FILE".
52 >
53
54 This works fine here with “tar (GNU tar) 1.34”:
55
56 $ mkdir -p a/b
57 $ touch a/file a/b/file
58 $ touch a/file.xz a/b/file.xz
59 $ tree a
60 a
61 ├── b
62 │   ├── file
63 │   └── file.xz
64 ├── file
65 └── file.xz
66
67 1 directory, 4 files
68 $ tar -cvzf test.tar.gz --exclude="a/file" --exclude="a/b/*.xz" a
69 a/
70 a/file.xz
71 a/b/
72 a/b/file
73 $ tar -tf test.tar.gz
74 a/
75 a/file.xz
76 a/b/
77 a/b/file
78
79 You can find out the maximum length of the command-line with
80 `getconf ARG_MAX`.
81 --
82 Get my PGP key with `gpg --locate-keys tastytea@××××××××.de` or at
83 <https://tastytea.de/tastytea.asc>.

Replies

Subject Author
[gentoo-user] Re: [OT] tar exclude syntax tip nunojsilva@×××××××.pt
Re: [gentoo-user] [OT] tar exclude syntax tip Walter Dnes <waltdnes@××××××××.org>