20

I'm wanting to create a tar archive of a specific directory (with its subdirectories of course). But when I do it, using the tar command, I get a list of files that were included, for example:

a calendar_final/._style.css

a calendar_final/style.css

As you can see, there are two versions of the same file. This goes for every file, and there are many. How do I exclude the temporary files, with the ._ prefix, from the tar archive?

2
  • This might help: unix.stackexchange.com/questions/2213/…
    – tshepang
    Commented Mar 19, 2011 at 19:03
  • 1
    These aren't temporary files, but represent OS X file system metadata (if I'm not mistaken, they include Finder labels, type and creator codes, extended attributes, etc.). These files don't exist prior to creating the tar on OS X. Possible answers depend on whether you're on OS X/HFS+ or another system. Commented Mar 19, 2011 at 20:07

7 Answers 7

38

You posted in a comment that you are working on a Mac OS X system. This is an important clue to the purpose of these ._* files.

These ._* archive entries are chunks of AppleDouble data that contain the extra information associated with the corresponding file (the one without the ._ prefix). They are generated by the Mac OS X–specific copyfile(3) family of functions. The AppleDouble blobs store access control data (ACLs) and extended attributes (commonly, Finder flags and “resource forks”, but xattrs can be used to store any kind of data).

The system-supplied Mac OS X archive tools (bsdtar (also symlinked as tar), gnutar, and pax) will generate a ._* archive member for any file that has any extended information associated with it; in “unarchive” mode, they will also decode those archive members and apply the resulting extended information to the associated file. This creates a “full fidelity” archive for use on Mac OS X systems by preserving and later extracting all the information that the HFS+ filesystem can store.

The corresponding archive tools on other systems do not know to give special handling to these ._* files, so they are unpacked as normal files. Since such files are fairly useless on other systems, they are often seen as “junk files”. Correspondingly, if a non–Mac OS X system generates an archive that includes normal files that start with ._, the Mac OS X unarchiving tools will try to decode those files as extended information.

There is, however an undocumented(?) way to make the system-supplied Mac OS X archivers behave like they do on other Unixy systems: the COPYFILE_DISABLE environment variable. Setting this variable (to any value, even the empty string), will prevent the archivers from generating ._* archive members to represent any extended information associated with the archived files. Its presence will also prevent the archivers from trying to interpret such archive members as extended information.

COPYFILE_DISABLE=1 tar czf new.tar.gz …
COPYFILE_DISABLE=1 tar xzf unixy.tar.gz …

You might set this variable in your shell’s initialization file if you want to work this way more often than not.

# disable special creation/extraction of ._* files by tar, etc. on Mac OS X
COPYFILE_DISABLE=1; export COPYFILE_DISABLE

Then, when you need to re-enable the feature (to preserve/restore the extended information), you can “unset” the variable for individual commands:

(unset COPYFILE_DISABLE; tar czf new-osx.tar.gz …)

The archivers on Mac OS X 10.4 also do something similar, though they use a different environment variable: COPY_EXTENDED_ATTRIBUTES_DISABLE

1
  • 1
    anyone seen this not work? I've tried this solution on Yosemite and when I unzip on an AIX box the PaxHeader directories are still generated Commented Mar 1, 2017 at 23:21
5

This should work:

tar zcf calendar.tgz "a calendar_final" --exclude '.*'
6
  • I thought it worked but actually when I unpacked the archive, I found that they had been included :/
    – user1995
    Commented Mar 19, 2011 at 18:56
  • Weird, which version of tar do you use? tar --version gives GNU 1.23 here. Commented Mar 19, 2011 at 19:01
  • 2
    Those files are file system metadata on OS X and don't exist prior to running tar, so probably cannot be excluded this way. They are included to allow restoring OS X file metadata when unpacking archived on another Mac. Commented Mar 19, 2011 at 20:04
  • Works for me. I recommend using tar vzcf ... to see if any wrong files were included.
    – stribika
    Commented Mar 19, 2011 at 20:31
  • @stribika He's most likely on OS X. See my comment on the question. The files cannot be excluded as they don't exist outside the tar archive. Commented Mar 19, 2011 at 20:54
2

Frederik Deweerdt has given a solution that works on GNU tar (used on Linux, Cygwin, FreeBSD, OSX, possibly others), but not on other systems such as NetBSD, OpenBSD or Solaris.

POSIX doesn't specify the tar command (because it varies too wildly between unix variants) and introduces the pax command instead. The option -w means to produce an archive (-r extracts), and -x selects the archive format. The option -s '!BRE!!' excludes all files whose path matches the basic regular expression BRE.

pax -w -x ustar -s '!^.*/\..*$!!' calendar_final >calendar_final.tar
1
  • Interesting... I didn't know about pax.
    – gabe.
    Commented Mar 19, 2011 at 20:25
2

As of bsdtar 3.0.3 - libarchive 3.0.3 (and perhaps earlier) there's a new (Mac OS X specific) option to the bsdtar command called --disable-copyfile to suppress the creation of ._ files. Older versions of tar don't have the --disable-copyfile option, but setting the environment variable COPYFILE_DISABLE=1 should work.

1
  • With bsdtar 2.8.3 - libarchive 2.8.3 of 10.7.5 the --disable-copyfile is not documented but available nonetheless. Commented Apr 30, 2013 at 21:38
1

The modern version of this would be to create the tarball using the --no-xattrs flag for MacOS's BSD tar.

tar --no-xattrs -c -f <tarfilename> [file list/glob/whatever]
1
  • The other post that uses --no-xattrs also specifies --no-mac-metadata which only affects extracting files on MacOS, not creating them.
    – Colby GK
    Commented Sep 1, 2023 at 14:42
0

Follow these steps:

  1. Find all ._ files and list them using -ls so that you can see all ._ files and finally delete them.

    find /path/to/directory -type f -iname "._*.*" -ls -delete
    
  2. Create archive .tar using.

    tar -cvf name.tar /path/to/directory
    
0

Nothing worked, in my case these hidden files is not present in file system (they metadata), so, I can't delete or ignore them. Solution I found is: --no-mac-metadata

To create archive in MacOS

# --no-xattrs - to fix warning when extracting on non-MacOS:
#   tar: Ignoring unknown extended header keyword 'LIBARCHIVE.xattr.com.apple.provenance'
#
# --no-mac-metadata - to not add Apple specific file attributes.
#   produces `._{filename}` files while extracting on non MacOS
tar -c \
    --no-xattrs \
    --no-mac-metadata \
     -f ${TARBALL} *

Note:

As I use in automated script, just in case I try to delete all useless files before creating .tar, this can be replaced with tar inclusion/exclusion rules, I just find it easy to read this way.

# remove annoying Apple files (._*) and remove python caches
find . \
    -type f -name '._*' -delete -or \
    -type f -name '*.py[co]' -delete -or \
    -type d -name __pycache__ -delete

You must log in to answer this question.