Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I try using tar to backup my files. With --exclude=/proc/, the archive contains files under /proc, and --exclude=/proc does not. What causes the difference?

P.S. The shell I use is Bash.

share|improve this question
up vote 2 down vote accepted

There is really no major difference between using a trailing slash or not in general for a directory, except in symlink handling. See Open Group Base Specifications, 4.11 Pathname Resolution (also see this answer at Unix SE).

As GNU tar is concerned: a trailing slash is simply not handled in --exclude patterns, as you've noticed. The difference is simply caused by its specific source code, if one should point to something :-) . See this question at Server Fault SE (though it does not give much detail - it merely states the fact). See this mail thread for some related information. A clue might be found in this passage (though I'm not entirely sure what to make of it):

Otto Moerbeek wrote:

This is the piece of code that is relevant:

             * Some programs that create ustar archives append a '/'
             * to the pathname for directories. This clearly violates
             * ustar specs, but we will silently strip it off anyway. 
            if (arcn-> name[arcn-> nlen - 1] == '/')
                    arcn-> name[--arcn-> nlen] = '\0';

As you can see with hexdump -C of archives created by gtar, gtar is such a program. I do not feel like changing tar just to accomodate non-conforming programs.

share|improve this answer

Usually, it's down to the convention of the program. With (GNU) tar it's a little inconsistent because a trailing / does not change included directories. The behaviour is partly explained here "patterns and names are used as-is". Excludes can match any part of a name (unless you use --anchor).

Documentation isn't clear that there is a difference, or why.

Internally, the reason a trailing / doesn't match in an exclude is that tar uses opendir(3), readdir(3) and fnmatch(3) -- opendir() accepts trailing / on a directory, readdir() doesn't put a slash on directory names, and fnmatch() simply matches patterns without regard to existing files or canonicalization. Specifically, fnmatch("proc/","proc",0) returns 1 (no match)..

Compare rsync which has very distinct (and well documented) distinctions for trailing / on directory names.

A related issue is that sometimes you want to backup a directory because it's a mount point, though you don't want its contents. The intent being your backup can contain all the required mount points (/dev proc /sys ) so usually you would want to do:

tar --exclude=/proc/* --exclude=/sys/* --exclude=/dev/* [...]
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .