Copy Files and Folders on Linux

cp Command

The cp command is one of the basic Linux commands for copying files and directories from one location to another.

When copying files from source to destination, the source file name does not change, but we can change the target file name if we need to.

cp [Option] [Source] [Destination]

Copy a File from One Location to Another in Linux Using the cp Command

So we have copied the source file “/home/user1/demo.txt” to the target “/home/user2” directory.

Copy a File from One Location to Another With a Different Name on Linux Using the cp Command

Now, file is copied from the source “/home/source/demo.txt” to the target “/home/destination/demo1.txt” directory with a different name.

Copy Multiple Files from One Location to Another in Linux Using the cp Command

We copied three files named “cp-demo.txt, cp-demo-1.txt and cp-demo-9.txt”.

No option is required to perform this action, and all the files must be entered with the space.

Copy a Directory Recursively from One Location to Another in Linux Using the cp Command

If you want to copy a directory recursively from one location to another using the cp command, use the -r/R option with the cp command.

It copies the folder including the subdirectories and their files to the target directory.

The folder name remains same.

Copy Multiple Folders on Linux Using the cp Command

It’s like the one above and it allows you to copy multiple folders at once.

Copy All Files from One Location to Another in Linux Using “wildcard (*)”

If you have a list of files and you want to copy them all to another location, use the following cp command format.

This excludes the directory by default, and the “-r” option must be included in this command to copy them.

Copy All Files and Folders from One Location to Another in Linux Using “wildcard (*)”

To do so, use the following cp command format. This is similar to the one above, but it will copy files and folders to the target directory at the same time.

Copy All Files, Including Hidden Files (“.” Dot Files) in Linux Using the cp Command

This command is the same as above, but you need to add “dot (.)” in addition to copy all the files, including hidden files.

Thanks for Reading!!!!!!!!