Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is Difference between cp and ditto command on OSX?

Tags:

macos

I want to know what is exact difference between cp and ditto command on OSX?

What are the main points that differentiate these two commands?

like image 360
hrishikesh chaudhari Avatar asked Dec 05 '16 12:12

hrishikesh chaudhari


People also ask

What is Ditto on Mac?

ditto : Copy or merge directories or archives If the destination directory does exist, ditto will merge the source directory with the destination, overwriting duplicate filenames. For instance, you could use ditto to merge the contents of two large directories of pictures into a single nested directory structure.

What is cp command in Mac?

Copy a file or folder locally In the Terminal app on your Mac, use the cp command to make a copy of a file. For example, to copy a folder named Expenses in your Documents folder to another volume named Data: % cp -R ~/Documents/Expenses /Volumes/Data/Expenses. The -R flag causes cp to copy the folder and its contents.

What does cp command means?

cp stands for copy. This command is used to copy files or group of files or directory. It creates an exact image of a file on a disk with different file name. cp command require at least two filenames in its arguments.

Are Linux terminal commands same as Mac?

In general, both will have the same core commands and features (especially those defined in the Posix standard), but a lot of extensions will be different. For example, linux systems generally have a useradd command to create new users, but OS X doesn't.


1 Answers

Actually the headlines in the man pages describe the difference:

  • cp - copy files and directories

  • ditto - copy directory hierarchies, create and extract archives

Further information from the man pages

  • cp
cp [OPTION]... [-T] SOURCE DEST

cp [OPTION]... SOURCE... DIRECTORY

cp [OPTION]... -t DIRECTORY SOURCE...

Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

  • ditto
ditto [-v] [-V] [-X] [<options>] src ... dst_directory.

ditto [-v] [-V] [<options>] src_file dst_file

ditto -c [-z | -j | -k] [-v] [-V] [-X] [<options>] src dst_archive

ditto -x [-z | -j | -k] [-v] [-V] [<options>] src_archive ...
       dst_directory

In its first form, ditto copies one or more source files or directories to a destination directory. If the destination directory does not exist it will be created before the first source is copied. If the destination directory already exists then the source directories are merged with the previous contents of the destination.

In its second form, ditto copies a file to the supplied dst_file path- name.

The next two forms reflect ditto's ability to create and extract ar- chives. These archives can be either CPIO format (preferred for unix content) or PKZip (for Windows compatibility). src_archive (and dst_archive) can be the single character '-', causing ditto to read (write) archive data from stdin (or to stdout, respectively).

ditto follows symbolic links provided as arguments but does not follow any links as it traverses the source or destination hierarchies. ditto overwrites existing files, symbolic links, and devices in the destination when these are copied from a source. The resulting files, links, and devices will have the same mode, access time, modification time, owner, and group as the source items from which they are copied. Pipes, sock- ets, and files with names beginning with .nfs or .afpDeleted will be ignored. ditto does not modify the mode, owner, group, extended attributes, or ACLs of existing directories in the destination. Files and symbolic links cannot overwrite directories or vice-versa.

ditto can be used to "thin" Universal Mach-O binaries during a copy. ditto can also copy files selectively based on the contents of a BOM ("Bill of Materials") file. ditto preserves file hard links (but not directory hard links) present in the source directories and preserves setuid and setgid modes when run as the superuser.

ditto will preserve resource forks and HFS meta-data information when copying unless instructed otherwise using --norsrc . Similarly, ditto will preserve extended attributes and Access Control Lists (ACLs) unless --noextattr or --noacl is passed. DITTONORSRC can be set in the environ- ment as an alias to --norsrc --noextattr --noacl on the command line.

like image 147
vadian Avatar answered Oct 19 '22 13:10

vadian