ditto

Copy directory hierarchies, create and extract archives. Ditto can preserve ownership / permissions, HFS resource forks and file / folder metadata. Ditto will automatically create the destination folder if it doesn’t yet exist, if the destination does exist and contains files, then ditto will merge them.

Syntax
     ditto [options] Source_file  Destination_file

     ditto [options] Source  Destination_folder

     ditto -x [options] Source_Archive  Destination_folder

     ditto -c [options] Source  Destination_archive

     ditto -h | --help

     Where Source = a file, folder or archive.

Options

     -c            Create an archive at the destination path.  The default format is CPIO, unless
                   -k is given.  CPIO archives should be stored in files with names ending in .cpio.
                   Compressed CPIO archives should be stored in files with names ending in .cpgz.

     -j            Create compressed CPIO archives, using bzip2(1) compression.
     -z            Create compressed CPIO archives, using gzip(1) compression.

     -k            Create or extract from a PKZip archive instead of the default CPIO.
                   PKZip archives should be stored in filenames ending in .zip.

     -v            Print a line of output to stderr for each source directory copied.

     -V            Print a line of output to stderr for every file, symbolic link, and device copied.

     -x            Extract the archives given as source arguments. The format is assumed to be CPIO,
                   unless -k is given.  Compressed CPIO is automatically handled.


     -X            When copying one or more source directories, do not descend into directories that
                   have a different device ID.

     -h            Print full usage.

     --keepParent  When creating an archive, embed the parent directory name source in Destination_archive.

     --arch arch   Thin Universal binaries to the specified architecture.
                   If multiple --arch options are specified then the resulting destination file
                   will contain each of the specified architectures (if they are present in the
                   source file).  arch should be specified as "i386", "x86_64", etc.

     --bom bom     Copy only files, links, devices, and directories that are present in the
                   specified BOM.

     --rsrc        Preserve resource forks and HFS meta-data.  ditto will store this data in
                   Carbon-compatible ._ AppleDouble files on filesystems that do not natively 
                   support resource forks.  As of Mac OS X 10.4, --rsrc is default behavior.

     --norsrc      Do not preserve resource forks and HFS meta-data.
                   If both --norsrc and --rsrc are passed, whichever is passed last will take
                   precedence.  Both options override DITTONORSRC.
                   Unless explicitly specified, --norsrc also implies --noextattr and --noacl
                   to match the behavior of Mac OS X 10.4.

     --extattr     Preserve extended attributes (requires --rsrc).
                   As of Mac OS X 10.5, --extattr is the default.

     --noextattr   Do not preserve extended attributes (requires --norsrc).

     --qtn         Preserve quarantine information.  As of Mac OS X 10.5, --qtn is the default.

     --noqtn       Do not preserve quarantine information.

     --acl         Preserve Access Control Lists (ACLs).  As of Mac OS X 10.5, --acl is the default.

     --noacl       Do not preserve ACLs.

     --nocache     Do not perform copies using the Mac OS X Unified Buffer Cache.
                   Files read and written will not be cached, although if the file is
                   already present in the cache, the cached information will be used.

     --hfsCompression
                   When copying files or extracting content from an archive, if the destination is
                   an HFS+ volume that supports compression, all the content will be compressed if
                   appropriate. This is only supported on Mac OS X 10.6 or later, and is only
                   intended to be used in installation and backup scenarios that involve system files.
                   Since files using HFS+ compression are not readable on versions of Mac OS X earlier
                   than 10.6, this flag should not be used when dealing with non-system files or other
                   user-generated content that will be used on a version of Mac OS X earlier than 10.6.

     --nohfsCompression
                   Do not compress files with HFS+ compression when copying or extracting content
                   from an archive unless the content is already compressed with HFS+ compression.
                   Supported on Mac OS X 10.6 or later.  --nohfsCompression is the default.

     --preserveHFSCompression
                   When copying files to an HFS+ volume that supports compression, ditto will preserve
                   the compression of any source files that were using HFS+ compression.
                   Supported on Mac OS X 10.6 or later.  --preserveHFSCompression is the default.

     --nopreserveHFSCompression
                   Do not preserve HFS+ compression when copying files that are already compressed
                   with HFS+ compression. Supported on Mac OS X 10.6 or later.

     --sequesterRsrc
                   When creating a PKZip archive, preserve resource forks and HFS meta-data in the
                   subdirectory __MACOSX.  PKZip extraction will automatically find these resources.

     --zlibCompressionLevel num
                   Sets the compression level to use when creating a PKZip archive. The compression
                   level can be set from 0 to 9, where 0 represents no compression, and 9 represents
                   optimal (slowest) compression.
                   By default, ditto will use the default compression level as defined by zlib.

     --password    When extracting a password-encrypted ZIP archive, you must specify --password to
                   allow ditto to prompt for a password to use to extract the contents of the file.
                   If this option is not provided, and a password-encrypted file is encountered,
                   ditto will emit an error message.

     --persistRootless
                   If a file being replaced has the SF_RESTRICTED flag or the com.apple.rootless extended
                   attribute set, retain it even if the src file may not have had the same flag or attribute.

     --nopersistRootless
                   Do not persist the SF_RESTRICTED flag or the com.apple.rootless extended attribute
                   for files being replaced.

     --nonAtomicCopies
                   Do not perform atomic copies when replacing existing files. By default ditto will
                   atomically swap new files into place when completing a copy.

     --segmentLargeFiles
                   When copying files to a CPIO archive, segment files larger than 8 gigabytes into
                   multiple entries.

     --keepBinaries
                   When copying files ditto will set aside the original Mach-O binary when it is being
                   replaced.  The file name will be changed to a random number preceeded by the prefix .BC.T_

     --keepBinariesList path
                   When ditto keeps binary files it will record the location of the kept file in the
                   file at the specified path.

     --keepBinariesPattern regex
                   Keep any regular file that matches the specified regular expression.
                   Note that this file must not be a Mach-O binary.

     --lang lang   When copying files with an index bom specified via -b option the user can specify
                   language variants to filter from the index bom.  By default ditto will create
                   a new index bom at /tmp/ditto.XXXXX representing the filtered contents.
                   You can direct the output bom via the -o flag.

     --outBom bom  Specify an explicit path for the output bom.
                   This bom will only be created if the -o flag or the -l flags are used.

     --option key=value
                   Specify an arbitrary key value pair to be passed to the copier.
                   The value can be a string, boolean, or integer.
                   Booleans can be specified as 'true', 'false', 'yes', or 'no'.

Unlike cp -R, if the destination folder already exists, the existing contents will be merged with the contents of the folder being copied.
Use --keepParent for non-archive copies to alleviate this issue.

Examples

Copy a folder, notice that quotes are needed because of the space in the folder name. This will merge the files into the destination:

ditto "~/Desktop/work files" "/Volumes/work backup"

To also preserve the Mac HFS resource forks, type and creator codes (only required prior to 10.4):

ditto -rsrc "~/Desktop/work files" "/Volumes/work backup"

Don’t copy metadata and HFS resource forks:

ditto -V --norsrc "~/Desktop/work files" "/Volumes/work backup"

Copy Scripts, skipping any resources or meta-data, to rhost:

ditto -c --norsrc Scripts -|ssh rhost ditto -x --norsrc - ./Scripts

List the files in the CPIO archive archive.cpio:

pax -f archive.cpio

List the files in the compressed CPIO archive archive.cpgz:

pax -zf archive.cpgz

Create a PKZip archive similarly to the Finder’s Compress functionality:

ditto -c -k --sequesterRsrc --keepParent src_directory archive.zip

List the files in the PKZip archive archive.zip:

unzip -l archive.zip

“The Borgesian meta-library contains a copy of every book ever written, but my dream-artifact is already, and always, every book ever written, on demand” ~ William Gibson

Related macOS commands

cp - Copy files.
dd - Data Duplicator - convert and copy a file.
install - Copy files and set attributes.
mv - Move Files.
pax - Read and write file archives and copy directory hierarchies
tar - store or extract files to an archive (allows symbolic links to be copied as links.)


 
Copyright © 1999-2024 SS64.com
Some rights reserved