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.
Build your own Vim statusline
Statuslines in Vim are not hard to create. Making your own means one less dependency in your life.
Custom Vim Bindings in tmux 2.4
tmux 2.4 made a significant change to key bindings. Here is how to support custom keybindings for versions before and after tmux 2.4
Using template files in Vim
Vim templates or skeletons, allow you to specify a template to be used for new files with a certain extension.