{
    "mode": "info",
    "parameter": "cp",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/info/cp/json",
    "generated": "2026-11-11T09:41:52Z",
    "sections": {
        "File: coreutils.info,  Node: cp invocation,  Next: dd invocation,  Up: Basic operations": {
            "content": "",
            "subsections": [
                {
                    "name": "11.1 'cp': Copy files and directories",
                    "content": "'cp' copies files (or, optionally, directories).  The copy is completely\nindependent of the original.  You can either copy one file to another,\nor copy arbitrarily many files to a destination directory.  Synopses:\n\ncp [OPTION]... [-T] SOURCE DEST\ncp [OPTION]... SOURCE... DIRECTORY\ncp [OPTION]... -t DIRECTORY SOURCE...\n\n* If two file names are given, 'cp' copies the first file to the\nsecond.\n\n* If the '--target-directory' ('-t') option is given, or failing that\nif the last file is a directory and the '--no-target-directory'\n('-T') option is not given, 'cp' copies each SOURCE file to the\nspecified directory, using the SOURCEs' names.\n\nGenerally, files are written just as they are read.  For exceptions,\nsee the '--sparse' option below.\n\nBy default, 'cp' does not copy directories.  However, the '-R', '-a',\nand '-r' options cause 'cp' to copy recursively by descending into\nsource directories and copying files to corresponding destination\ndirectories.\n\nWhen copying from a symbolic link, 'cp' normally follows the link\nonly when not copying recursively or when '--link' ('-l') is used.  This\ndefault can be overridden with the '--archive' ('-a'), '-d',\n'--dereference' ('-L'), '--no-dereference' ('-P'), and '-H' options.  If\nmore than one of these options is specified, the last one silently\noverrides the others.\n\nWhen copying to a symbolic link, 'cp' follows the link only when it\nrefers to an existing regular file.  However, when copying to a dangling\nsymbolic link, 'cp' refuses by default, and fails with a diagnostic,\nsince the operation is inherently dangerous.  This behavior is contrary\nto historical practice and to POSIX.  Set 'POSIXLYCORRECT' to make 'cp'\nattempt to create the target of a dangling destination symlink, in spite\nof the possible risk.  Also, when an option like '--backup' or '--link'\nacts to rename or remove the destination before copying, 'cp' renames or\nremoves the symbolic link rather than the file it points to.\n\nBy default, 'cp' copies the contents of special files only when not\ncopying recursively.  This default can be overridden with the\n'--copy-contents' option.\n\n'cp' generally refuses to copy a file onto itself, with the following\nexception: if '--force --backup' is specified with SOURCE and DEST\nidentical, and referring to a regular file, 'cp' will make a backup\nfile, either regular or numbered, as specified in the usual ways (*note"
                }
            ]
        },
        "Backup options::).  This is useful when you simply want to make a backup": {
            "content": "of an existing file before changing it.\n\nThe program accepts the following options.  Also see *note Common\noptions::.\n\n'-a'\n'--archive'\nPreserve as much as possible of the structure and attributes of the\noriginal files in the copy (but do not attempt to preserve internal\ndirectory structure; i.e., 'ls -U' may list the entries in a copied\ndirectory in a different order).  Try to preserve SELinux security\ncontext and extended attributes (xattr), but ignore any failure to\ndo that and print no corresponding diagnostic.  Equivalent to '-dR\n--preserve=all' with the reduced diagnostics.\n\n'--attributes-only'\nCopy only the specified attributes of the source file to the\ndestination.  If the destination already exists, do not alter its\ncontents.  See the '--preserve' option for controlling which\nattributes to copy.\n\n'-b'\n'--backup[=METHOD]'\n*Note Backup options::.  Make a backup of each file that would\notherwise be overwritten or removed.  As a special case, 'cp' makes\na backup of SOURCE when the force and backup options are given and\nSOURCE and DEST are the same name for an existing, regular file.\nOne useful application of this combination of options is this tiny\nBourne shell script:\n\n#!/bin/sh\n# Usage: backup FILE...\n# Create a GNU-style backup of each listed FILE.\nfail=0\nfor i; do\ncp --backup --force --preserve=all -- \"$i\" \"$i\" || fail=1\ndone\nexit $fail\n\n'--copy-contents'\nIf copying recursively, copy the contents of any special files\n(e.g., FIFOs and device files) as if they were regular files.  This\nmeans trying to read the data in each source file and writing it to\nthe destination.  It is usually a mistake to use this option, as it\nnormally has undesirable effects on special files like FIFOs and\nthe ones typically found in the '/dev' directory.  In most cases,\n'cp -R --copy-contents' will hang indefinitely trying to read from\nFIFOs and special files like '/dev/console', and it will fill up\nyour destination file system if you use it to copy '/dev/zero'.\nThis option has no effect unless copying recursively, and it does\nnot affect the copying of symbolic links.\n\n'-d'\nCopy symbolic links as symbolic links rather than copying the files\nthat they point to, and preserve hard links between source files in\nthe copies.  Equivalent to '--no-dereference --preserve=links'.\n\n'--debug'\nPrint extra information to stdout, explaining how files are copied.\nThis option implies the '--verbose' option.\n\n'-f'\n'--force'\nWhen copying without this option and an existing destination file\ncannot be opened for writing, the copy fails.  However, with\n'--force', when a destination file cannot be opened, 'cp' then\ntries to recreate the file by first removing it.  Note '--force'\nalone will not remove dangling symlinks.  When this option is\ncombined with '--link' ('-l') or '--symbolic-link' ('-s'), the\ndestination link is replaced, and unless '--backup' ('-b') is also\ngiven there is no brief moment when the destination does not exist.\nAlso see the description of '--remove-destination'.\n\nThis option is independent of the '--interactive' or '-i' option:\nneither cancels the effect of the other.\n\nThis option is ignored when the '--no-clobber' or '-n' option is\nalso used.\n\n'-H'\nIf a command line argument specifies a symbolic link, then copy the\nfile it points to rather than the symbolic link itself.  However,\ncopy (preserving its nature) any symbolic link that is encountered\nvia recursive traversal.\n\n'-i'\n'--interactive'\nWhen copying a file other than a directory, prompt whether to\noverwrite an existing destination file, and fail if the response is\nnot affirmative.  The '-i' option overrides a previous '-n' option.\n\n'-l'\n'--link'\nMake hard links instead of copies of non-directories.\n\n'-L'\n'--dereference'\nFollow symbolic links when copying from them.  With this option,\n'cp' cannot create a symbolic link.  For example, a symlink (to\nregular file) in the source tree will be copied to a regular file\nin the destination tree.\n\n'-n'\n'--no-clobber'\nDo not overwrite an existing file; silently fail instead.  This\noption overrides a previous '-i' option.  This option is mutually\nexclusive with '-b' or '--backup' option.\n\n'-P'\n'--no-dereference'\nCopy symbolic links as symbolic links rather than copying the files\nthat they point to.  This option affects only symbolic links in the\nsource; symbolic links in the destination are always followed if\npossible.\n\n'-p'\n'--preserve[=ATTRIBUTELIST]'\nPreserve the specified attributes of the original files.  If\nspecified, the ATTRIBUTELIST must be a comma-separated list of one\nor more of the following strings:\n\n'mode'\nPreserve attributes relevant to access permissions, including\nfile mode bits and (if possible) access control lists (ACLs).\nACL preservation is system-dependent, and ACLs are not\nnecessarily translated when the source and destination are on\nfile systems with different ACL formats (e.g., NFSv4 versus\nPOSIX formats).\n\n'ownership'\nPreserve the owner and group.  On most modern systems, only\nusers with appropriate privileges may change the owner of a\nfile, and ordinary users may preserve the group ownership of a\nfile only if they happen to be a member of the desired group.\n'timestamps'\nPreserve the times of last access and last modification, when\npossible.  On older systems, it is not possible to preserve\nthese attributes when the affected file is a symbolic link.\nHowever, many systems now provide the 'utimensat' function,\nwhich makes it possible even for symbolic links.\n'links'\nPreserve in the destination files any links between\ncorresponding source files.  Note that with '-L' or '-H', this\noption can convert symbolic links to hard links.  For example,\n$ mkdir c; : > a; ln -s a b; cp -aH a b c; ls -i1 c\n74161745 a\n74161745 b\nNote the inputs: 'b' is a symlink to regular file 'a', yet the\nfiles in destination directory, 'c/', are hard-linked.  Since\n'-a' implies '--no-dereference' it would copy the symlink, but\nthe later '-H' tells 'cp' to dereference the command line\narguments where it then sees two files with the same inode\nnumber.  Then the '--preserve=links' option also implied by\n'-a' will preserve the perceived hard link.\n\nHere is a similar example that exercises 'cp''s '-L' option:\n$ mkdir b c; (cd b; : > a; ln -s a b); cp -aL b c; ls -i1 c/b\n74163295 a\n74163295 b\n\n'context'\nPreserve SELinux security context of the file, or fail with\nfull diagnostics.\n'xattr'\nPreserve extended attributes of the file, or fail with full\ndiagnostics.  If 'cp' is built without xattr support, ignore\nthis option.  If SELinux context, ACLs or Capabilities are\nimplemented using xattrs, they are preserved implicitly by\nthis option as well, i.e., even without specifying\n'--preserve=mode' or '--preserve=context'.\n'all'\nPreserve all file attributes.  Equivalent to specifying all of\nthe above, but with the difference that failure to preserve\nSELinux security context or extended attributes does not\nchange 'cp''s exit status.  In contrast to '-a', all but\n'Operation not supported' warnings are output.\n\nUsing '--preserve' with no ATTRIBUTELIST is equivalent to\n'--preserve=mode,ownership,timestamps'.\n\nIn the absence of this option, the permissions of existing\ndestination files are unchanged.  Each new file is created with the\nmode of the corresponding source file minus the set-user-ID,\nset-group-ID, and sticky bits as the create mode; the operating\nsystem then applies either the umask or a default ACL, possibly\nresulting in a more restrictive file mode.  *Note File\npermissions::.\n\n'--no-preserve=ATTRIBUTELIST'\nDo not preserve the specified attributes.  The ATTRIBUTELIST has\nthe same form as for '--preserve'.\n\n'--parents'\nForm the name of each destination file by appending to the target\ndirectory a slash and the specified name of the source file.  The\nlast argument given to 'cp' must be the name of an existing\ndirectory.  For example, the command:\n\ncp --parents a/b/c existingdir\n\ncopies the file 'a/b/c' to 'existingdir/a/b/c', creating any\nmissing intermediate directories.\n\n'-R'\n'-r'\n'--recursive'\nCopy directories recursively.  By default, do not follow symbolic\nlinks in the source unless used together with the '--link' ('-l')\noption; see the '--archive' ('-a'), '-d', '--dereference' ('-L'),\n'--no-dereference' ('-P'), and '-H' options.  Special files are\ncopied by creating a destination file of the same type as the\nsource; see the '--copy-contents' option.  It is not portable to\nuse '-r' to copy symbolic links or special files.  On some non-GNU\nsystems, '-r' implies the equivalent of '-L' and '--copy-contents'\nfor historical reasons.  Also, it is not portable to use '-R' to\ncopy symbolic links unless you also specify '-P', as POSIX allows\nimplementations that dereference symbolic links by default.\n\n'--reflink[=WHEN]'\nPerform a lightweight, copy-on-write (COW) copy, if supported by\nthe file system.  Once it has succeeded, beware that the source and\ndestination files share the same data blocks as long as they remain\nunmodified.  Thus, if an I/O error affects data blocks of one of\nthe files, the other suffers the same fate.\n\nThe WHEN value can be one of the following:\n\n'always'\nIf the copy-on-write operation is not supported then report\nthe failure for each file and exit with a failure status.\nPlain '--reflink' is equivalent to '--reflink=always'.\n\n'auto'\nIf the copy-on-write operation is not supported then fall back\nto the standard copy behavior.  This is the default if no\n'--reflink' option is given.\n\n'never'\nDisable copy-on-write operation and use the standard copy\nbehavior.\n\nThis option is overridden by the '--link', '--symbolic-link' and\n'--attributes-only' options, thus allowing it to be used to\nconfigure the default data copying behavior for 'cp'.\n\n'--remove-destination'\nRemove each existing destination file before attempting to open it\n(contrast with '-f' above).\n\n'--sparse=WHEN'\nA \"sparse file\" contains \"holes\" - a sequence of zero bytes that\ndoes not occupy any file system blocks; the 'read' system call\nreads these as zeros.  This can both save considerable space and\nincrease speed, since many binary files contain lots of consecutive\nzero bytes.  By default, 'cp' detects holes in input source files\nvia a crude heuristic and makes the corresponding output file\nsparse as well.  Only regular files may be sparse.\n\nThe WHEN value can be one of the following:\n\n'auto'\nThe default behavior: if the input file is sparse, attempt to\nmake the output file sparse, too.  However, if an output file\nexists but refers to a non-regular file, then do not attempt\nto make it sparse.\n\n'always'\nFor each sufficiently long sequence of zero bytes in the input\nfile, attempt to create a corresponding hole in the output\nfile, even if the input file does not appear to be sparse.\nThis is useful when the input file resides on a file system\nthat does not support sparse files (for example, 'efs' file\nsystems in SGI IRIX 5.3 and earlier), but the output file is\non a type of file system that does support them.  Holes may be\ncreated only in regular files, so if the destination file is\nof some other type, 'cp' does not even try to make it sparse.\n\n'never'\nNever make the output file sparse.  This is useful in creating\na file for use with the 'mkswap' command, since such a file\nmust not have any holes.\n\nFor example, with the following alias, 'cp' will use the minimum\namount of space supported by the file system.  (Older versions of\n'cp' can also benefit from '--reflink=auto' here.)\n\nalias cp='cp --sparse=always'\n\n'--strip-trailing-slashes'\nRemove any trailing slashes from each SOURCE argument.  *Note\nTrailing slashes::.\n\n'-s'\n'--symbolic-link'\nMake symbolic links instead of copies of non-directories.  All\nsource file names must be absolute (starting with '/') unless the\ndestination files are in the current directory.  This option merely\nresults in an error message on systems that do not support symbolic\nlinks.\n\n'-S SUFFIX'\n'--suffix=SUFFIX'\nAppend SUFFIX to each backup file made with '-b'.  *Note Backup\noptions::.\n\n'-t DIRECTORY'\n'--target-directory=DIRECTORY'\nSpecify the destination DIRECTORY.  *Note Target directory::.\n\n'-T'\n'--no-target-directory'\nDo not treat the last operand specially when it is a directory or a\nsymbolic link to a directory.  *Note Target directory::.\n\n'-u'\n'--update[=WHICH]'\nDo not copy a non-directory that has an existing destination with\nthe same or newer modification timestamp; instead, silently skip\nthe file without failing.  If timestamps are being preserved, the\ncomparison is to the source timestamp truncated to the resolutions\nof the destination file system and of the system calls used to\nupdate timestamps; this avoids duplicate work if several 'cp -pu'\ncommands are executed with the same source and destination.  This\noption is ignored if the '-n' or '--no-clobber' option is also\nspecified.  Also, if '--preserve=links' is also specified (like\nwith 'cp -au' for example), that will take precedence;\nconsequently, depending on the order that files are processed from\nthe source, newer files in the destination may be replaced, to\nmirror hard links in the source.\n\nWHICH gives more control over which existing files in the\ndestination are replaced, and its value can be one of the\nfollowing:\n\n'all'\nThis is the default operation when an '--update' option is not\nspecified, and results in all existing files in the\ndestination being replaced.\n\n'none'\nThis is similar to the '--no-clobber' option, in that no files\nin the destination are replaced, but also skipping a file does\nnot induce a failure.\n\n'older'\nThis is the default operation when '--update' is specified,\nand results in files being replaced if they're older than the\ncorresponding source file.\n\n'-v'\n'--verbose'\nPrint the name of each file before copying it.\n\n'-x'\n'--one-file-system'\nSkip subdirectories that are on different file systems from the one\nthat the copy started on.  However, mount point directories are\ncopied.\n\n'-Z'\n'--context[=CONTEXT]'\nWithout a specified CONTEXT, adjust the SELinux security context\naccording to the system default type for destination files,\nsimilarly to the 'restorecon' command.  The long form of this\noption with a specific context specified, will set the context for\nnewly created files only.  With a specified context, if both\nSELinux and SMACK are disabled, a warning is issued.  This option\nis mutually exclusive with the '--preserve=context' option, and\noverrides the '--preserve=all' and '-a' options.\n\nAn exit status of zero indicates success, and a nonzero value\nindicates failure.\n",
            "subsections": []
        }
    },
    "flags": [],
    "examples": [],
    "see_also": []
}