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 unlike the
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
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/
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
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
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
To prompt for confirmation when copying a file pass the
-i flag. Normally when using the
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'?
To create a hard link instead of copying with the
cp command pass the
-l 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 links.
ls foo.txt cat foo.txt foo text cp -l foo.txt bar.txt echo 'bar text' > bar.txt cat foo.txt bar text
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
To show files that are being copied pass the
-v option to the
cp. This 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 contents.
cp -R -v foo bar 'foo' -> 'bar' 'foo/foo.txt' -> 'bar/foo.txt' 'foo/bar.txt' -> 'bar/bar.txt'
Have an update or suggestion for this article? You can edit it here and send me a pull request.
Using HashiCorp Vault with LDAP
How to use HashiCorp Vault to setup an LDAP backed secret store with read-only access for users in groups and read-write access for specific users
Linux and Unix xargs command tutorial with examples
Tutorial on using xargs, a UNIX and Linux command for building and executing command lines from standard input. Examples of cutting by character, byte position, cutting based on delimiter and how to modify the output delimiter.
Copy a file in Go
How to copy a file in Go. The ioutil package does not offer a shorthand way of copying a file. Instead the os package should be used.