Signup/Sign In
PUBLISHED ON: JULY 9, 2021

How to Copy a File or Directory in Linux (cp command)

In Linux, if we want to copy a file or a directory, we use the cp command.

The cp (copy) command is used to copy files or groups of files or directories in Linux/Unix operating systems. In Linux, the install command is also available to copy files and set attributes to any file or directory in Linux operating system.

The cp command works in the following modes:

  1. If you provide two arguments for pathnames of files, this command will copy the contents of the first file to the second file, creating the second file, and if the file doesn't exist then the cp command creates it.

  2. If you want to copy multiple files or directories then the source path will have all the names of files to be copied and the destination path must be a directory.

  3. If the source is a directory and the destination is also a directory then the cp command copies all contents of the first directory into the second directory.

The general syntax for the cp command is as follows:

cp [ option] source destination

Linux cp Command Options

Brief description of options available with the cp command.

Option

Description

-b

It is used to create a backup of every destination file.

-f

It is used for force copy by removing the destination file if needed.

-i

It is used to get user interaction.

-I, -s

It is used to create a hard/symbolic link rather than a physical copy.

-L

It is used to follow symbolic links.

-n

It is used to set no file overwrites.

-R

It is used to recursive copy (including hidden files).

-u

It is used to update. It copies only if the source newer than the destination or the destination missing.

-v

Verbose (output each step as it happens)

Example: Copy files in Linux

Here, we used cp command to copy a file to a folder and then verify it by getting in by using the cd command. The cd command is used to change the directory in Linux. See the commands.

simple use of cp command

Copy and Backup of file in Linux

-b (backup) - This option with cp command creates a backup file or directories inside the destination directory. We can use this command like:

cp -b hello.txt myfolder

It will copy the file hello.txt is the destination folder along with a backup file that includes a tiled symbol with the file name.

hello.txt ~ is the backup file of the previous hello. txt file.

Forcefuly Copy of file in Linux

-f (force) - This option is used to copy files or directories forcefully. If any files or directories are protected by writing permission then by using the -f option with the cp command, we can copy successfully from source to destination.

-f (force) option copy file forcefully

Copy Multiple files in Linux

Linux allows copying multiple files or directories to any destination.

In this command, all files in the current directory that end with a .txt extension are copied to another directory.

use of cp command with same type of files

Conclusion

In this tutorial, we covered how to copy files, groups of files, or directories using the cp command with the various options available in the cp command with suitable examples.



About the author:
Pradeep has expertise in Linux, Go, Nginx, Apache, CyberSecurity, AppSec and various other technical areas. He has contributed to numerous publications and websites, providing his readers with insightful and informative content.