80

When I tar certain files in OS X:

tar cvf foo.tar foo

It produces an extra file ._foo in the tarball:

./._foo
foo

which only shows up if I extract it on a non-Mac operating system. But ._foo doesn't exist on my file system! What's going on? How can I get rid of it?

Robotnik
  • 2,645
Jesse Beder
  • 1,811

6 Answers6

86

OS X's tar uses the AppleDouble format to store extended attributes and ACLs.

$ touch file1 file2 file3
$ xattr -w key value file1
$ chmod +a 'admin allow delete' file2
$ ls -le@ *
-rw-r--r--@ 1 lauri  staff  0 May 25 07:09 file1
    key 5
-rw-r--r--+ 1 lauri  staff  0 May 25 07:09 file2
 0: group:admin allow delete
-rw-r--r--  1 lauri  staff  0 May 25 07:09 file3
$ tar -cf 1.tar *
$ tar -tf 1.tar
./._file1
file1
./._file2
file2
file3

OS X's tar also knows how to convert the ._ members back to native formats, but the ._ files are usually kept when archives are extracted on other platforms. You can tell tar to not include the metadata by setting COPYFILE_DISABLE to some value:

$ COPYFILE_DISABLE=1 tar -cf 2.tar file*    
$ tar -tf 2.tar
file1
file2
file3
  • The copyfile functions are described in man copyfile
  • ls -l@ shows the keys and sizes of extended attributes, ls -le prints ACLs
  • xattr -l lists the keys and values of extended attributes
  • xattr -c clears all extended attributes (-d can't be used alone)
  • chmod -N deletes ACLs
  • Zip files created on OS X use a __MACOSX folder to store similar metadata

Information stored as extended attributes:

  • Resource forks (resource forks have been extended attributes since 10.4)
    • Custom icons set in Finder and the images of Icon\r files
    • Metadata in PSD files
    • Objects stored in scpt files, AppleScript Editor window state, descriptions of scripts
  • Information about aliases (aliases stop working if extended attributes are removed)
  • Quarantine status or source URLs of files downloaded from the internet
  • Spotlight comments
  • Encoding of files saved with TextEdit
  • Caret position of files opened with TextMate
  • Skim notes
Lri
  • 42,502
  • 8
  • 126
  • 159
Jesse Beder
  • 1,811
17

As of bsdtar 3.0.3 - libarchive 3.0.3 (and perhaps earlier) there's a new option to the bsdtar command called --disable-copyfile to suppress the creation of ._ files.

# on Mac OS X
# /usr/bin/tar -> bsdtar
ls -l /usr/bin/tar    

# from man bsdtar
--disable-copyfile
        Mac OS X specific.  Disable the use of copyfile(3).
mdm
  • 171
2

The ._ files are resource forks as mentioned in other answers. However, there's a better way to get rid of them when using tar:

export COPYFILE_DISABLE=true
tar cvf foo.tar foo

There's also a dot_clean utility for cleaning up these files (I think it's usually used for external storage).

jtbandes
  • 8,960
0

Also answered on Apple stackexchange

The accepted answer didn't work on MacOS Monterey (V 12.4). The tar is bsdtar and the man page revealed that there's a separate option to be specified --no-mac-metadata

$ tar -czv --no-mac-metadata -f <file_name> <filelist>

Sequence of the options seem to matter as following command didn't give desired result:

$ tar --no-mac-metadata -czvf <file_name> <filelist>
Mandar
  • 1
0

Here's a python script for removing those files. Should work in any popular OS.

Not thoroughly tested, use at your own risk!

import os
import os.path

def dot_clean(folder):
    files = os.listdir(folder)
    for file in files:
        full_name = folder + "/" + file
        if os.path.isdir(full_name):
            dot_clean(full_name)
        elif file.startswith("._"):
            os.remove(full_name)

dot_clean('.')            
Aivar
  • 111
-3

The period character, ".", is used on the Mac platform as a hidden file indicator. On Windows, it is the "$" character. Anyways, the ._foo file probably holds some OS X specific information and I would recommend against deleting it. On other systems you will have to ignore it, or someone here might be able to provide you with a script that will hide files and folders that begin with a ".".

dbmikus
  • 95