Javatpoint Logo
Javatpoint Logo

cp Command in Linux/Unix | Linux Copy File

'cp' means copy. 'cp' command is used to copy a file or a directory.

To copy a file into the same directory syntax will be,

Linux File cp

In above snapshot, we have created a copy of 'docu' and named it as 'newdocu'. If in case, <new file name> (in our case it is 'newdocu') alreade exists, then it will simply over write the earlier file.


To copy a file in a different directory

We have to mention the path of the destination directory.

In the snapshot below, earlier there is no 'text' file. After giving the command, 'text' file has been copied to the destination directory that is 'Desktop'.

Linux File cp

cp Options

OptionFunction
cp -r To copy a directory along with its sub dirctories.
cp file1 file 2 directory name To copy multiple file or directories in a directory.
cp -backup <file name>To backup the existing file before over writing it.
cp -i Asks for confirmtion.
cp -l To create hard link file.
cp -p Preserves attribute of a file.
cp -u -v To make sure source file is newer then destination file.

Linux cp -r

Option 'r' with the copy command can be used to copy a directory including all its content from a source directory to the destination directory.

Syntax:

Example:

linux cp -r

In the above example, we have copied directory 'library' to the destination directory /home/sssit/Documents. Here, all the contents of 'library' directory including its contents have been copied to destination directory.

Linux Copy Multiple Files or Directories

Multiple files or directories can be copied to a destination directory at once. In this case, target must be a directory. To copy multiple files you can use wildcards (cp *.extension) having same pattern.

Syntax:

Example:

Linux copy multiple files

In above example, we have copied files (file1, file2, file3) having same extension '.txt' to Documents directory.

Linux cp --backup

If the file you want to copy already exists in the destination directory, you can backup your existing file with the use of this command.

Syntax:

Example:

Linux file cp backup

As you can see above, 'file2.txt' already exists in the destination directory. Hence, we have created a backup of this file and copied it in the same directory (having same name).

Now our destination directory that is 'Downloads' has two files with the same name (that is 'file2.txt').

Linux cp -i

The cp '-i' option allows you to confirm once before overwriting your file.

Syntax:

Example:

Linux cp -i

As you can see, it is asking for permission to over write the file 'file3.txt' as this file already exists in the destination directory. Now, you can press y to overwrite file and n not to overwrite file.

Linux cp -l

If you want to create a hard link of a file instead of copying that file, you can use option 'l'.

Note: In creating hard link of the file the inode number of the two files will remain same. While in case of copying, inode number changes.

Syntax:

Example:

Linux file cp -l

Note here that we have created a hard link of the file 'file1.txt' in 'usr'. Inode number of both the files are same.

Linux cp -p

The cp '-p' option is used to preserve the properties and attributes of a file. You can also preserve the selected properties which you want.

Syntax:

Example:

Linux cp -p

Now, you can match the two files (original one and the copied one) in the above picture, both have the same properties.

Linux cp -u -v

The cp -u -v command is used when you want to make sure that destination file is missing or doesn't exist.

Syntax:

Example:

Linux cp -u -v

In the above picture, you can see that there are two files 'docc' and 'file1.txt'. Now we want to copy these two files in the 'Download' directory. But we don't know that in the 'Downloads' directory file 'file1.txt' already exists.

Giving the command 'cp -u -v' will automatically take care of the already existing file (file1.txt) and will not over write it.

This command is useful in copying big files.

Next TopicLinux mv command





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA