Linux and Unix cp command tutorial with examples
Tutorial on using cp, a UNIX and Linux command for copying files and directories. Examples of copying a file, copying multiple files, copying a directory, taking a backup when copying and preserving file attributes when copying.
What is the cp command? ¶
cp command is a command-line utility for copying files and directories. It
supports moving one or more files or folders with options for taking backups and
preserving attributes. Copies of files are independent of the original file
How to copy a file ¶
To copy a file with the
cp command pass the name of the file to be copied and
then the destination. In the following example the file
foo.txt is copied to a
new file called
bar.txt. The cp command will also create the new file as part
of the operation.
ls foo.txt cp foo.txt bar.txt ls foo.txt bar.txt
How to copy multiple files ¶
To copy multiple files using the
cp command pass the names of files followed
by the destination directory to the
tree -F . ├── bar.txt ├── baz.txt ├── foo/ └── foo.txt cp foo.txt bar.txt baz.txt foo/ tree . . ├── bar.txt ├── baz.txt ├── foo │ ├── bar.txt │ ├── baz.txt │ └── foo.txt └── foo.txt
The same operation can also be achieved through pattern matching
cp *.txt foo/
How to copy a directory ¶
By default the
cp command will not copy directories. Attempting to copy a
directory results in an error.
cp directory/ foo cp: omitting directory 'directory/'
To copy a directory pass the
-R flag. This will recursively copy a folder and
create a copy.
cp -R directory/ foo
How to copy multiple directories ¶
To copy multiple directories pass the path of the directories to be copied followed by the destination directory.
tree . . ├── bar │ └── bar.txt ├── baz │ └── baz.txt ├── foo │ └── foo.txt └── some-directory cp foo bar baz some-directory tree . . ├── bar │ └── bar.txt ├── baz │ └── baz.txt ├── foo │ └── foo.txt └── some-directory ├── bar │ └── bar.txt ├── baz │ └── baz.txt └── foo └── foo.txt
How to take a backup when copying a file ¶
If a copy operation will overwrite a file the
-b flag may be used to create a
back up of the file. This copies the file into place and writes a backup file.
ls foo.txt bar.txt cp -b foo.txt bar.txt ls foo.txt bar.txt bar.txt~
To specify the name of the backup file use the
ls foo.txt bar.txt cp -S .bak foo.txt bar.txt ls foo.txt bar.txt bar.txt.bak ls foo.txt bar.txt bar.txt.bak
How to prompt for confirmation when copying a file ¶
To prompt for confirmation when copying a file pass the
-i flag. Normally when
cp command a destination file will be overwritten if it exists when
copying. Using the
-i flag the command will prompt to overwrite the file.
ls foo.txt bar.txt cp -i foo.txt bar.txt cp: overwrite 'bar.txt'?
How to create a hard link instead of copying ¶
To create a hard link instead of copying with the
cp command pass the
option. Instead of copying the file a new file will be created that is a hard
link to the data on disk. Here’s a primer on hard and symbolic or soft
ls foo.txt cat foo.txt foo text cp -l foo.txt bar.txt echo 'bar text' > bar.txt cat foo.txt bar text
How to preserve file attributes ¶
To preserve file attributes (permissions, group and user owernship) pass the
--preserve option along with the attributes to be preserved. By default mode,
ownership and timestamps will be preserved.
ls -la -rw------- 1 george users 0 Oct 13 09:14 foo.txt cp --preserve foo.txt bar.txt -rw------- 1 george users 0 Oct 13 09:14 bar.txt -rw------- 1 george users 0 Oct 13 09:14 foo.txt
How to show files that are being copied ¶
To show files that are being copied pass the
-v option to the
prints the files and folders that are being copied to standard output. In the
following example the directory
foo is copied as
bar along with the
cp -R -v foo bar 'foo' -> 'bar' 'foo/foo.txt' -> 'bar/foo.txt' 'foo/bar.txt' -> 'bar/bar.txt'
Further reading ¶
Can you help make this article better? You can edit it here and send me a pull request.
Linux and Unix mv command tutorial with examples
Tutorial on using mv, a UNIX and Linux command to move or rename files. Examples of moving a file, moving multiple files, moving a directory, prompting before overwriting and taking a backup before moving.
Linux and Unix ln command tutorial with examples
Tutorial on using ln, a UNIX and Linux command to make links between files. Examples of creating a hard link, creating a symbolic link, and a broken symbolic link.
Linux and Unix rm command tutorial with examples
Tutorial on using rm, a UNIX and Linux command for removing files or directories. Examples of removing a file, removing multiple files, prompting for confirmation, removing recursively and forcing removal.