How to use cp command in Linux

The cp command is used to copy files and directories in Linux. Learn how to use the cp command with examples, including how to copy multiple files, copy directories recursively, and avoid overwriting existing files.

cp command Copying Files and Directories

  • cp – copy files and directories
  • Usage:
    • cp [options] file destination
  • More than one file may be copied at a time if the destination is a directory:
    • cp [options] file1 file2 destdir

Copying files with cp

cp must always be given least two arguments. When two arguments are given:

The first argument is interpreted as the source file. Either an absolute or relative pathname is acceptable.

The second argument is interpreted as the destination. Again, use either a relative or absolute pathname.
if it names an existing directory, a copy of the source file is placed in that directory with the same name as the source. Otherwise, the destination is interpreted as a file name, and a copy of the source file is created with that destination name.

When more than two arguments are given, all arguments but the last are interpreted as source files. The last argument is interpreted as a destination directory. Copies of the source files are placed, with their original file names, in the destination directory.

A few common options include:

-i (interactive): ask before overwriting a file
-r (recusive): recursively copy an entire directory tree
-p (preserve): preserve permissions, ownership, and time stamps
-a (archive) copies files and directories reclusively (like -r) while preserving permissions (like -p).

Example: –

In below example: – making copy of file3.txt with new name file4.txt in directory work.

[root@test /]# ls /as/
file1.txt file2.txt file3.txt
[root@test /]# cp /as/file3.txt /work/file4.txt
[root@test /]# ls /work/
file4.txt
How to use cp command in Linux

The Destination: –

  • If the destination is a directory, the copy is placed there
  • If the destination is a file, the copy overwrites the destination.
  • If the destination does not exist, the copy is renamed

The destination affects cp’s behavior

When copying a single file to a destination, cp first checks to see if a directory exists with the destination name. if it does, a copy of the source file is placed there with its original name. If not, the destination is assumed to be a new file name and copy of the source file is made with the destination name.

To illustrate, suppose your current directory is /work, you want to make a copy of file4 in a subdirectory of your work directory named backups.

[root@test work]# ls -l file4.txt
-rw-r--r-- 1 root root 0 Oct 8 12:58 file4.txt
[root@test work]# cp file4.txt backups/
[root@test work]# ls -l backups/
total 0
-rw-r--r-- 1 root root 0 Oct 8 13:02 file4.txt
How to use cp command in Linux

Note that you have created a file called /work/backup instead of /work/backups/file4.txt

To ensure that cp knows you intend to copy into a directory called backups, you can append a / to the destination: backup/ . This tells the command explicitly that you are referring to a directory, not a file. If the destination directory does not exist, the slash will cause the command to fail with an error message.

Leave a Comment