How to Copy Files and Directories on Linux

Linux TLDR
Last Updated:
Reading time: 3 minutes

In UNIX/Linux systems, the cp command is the most commonly used command that allows you to copy files and directories from one location to another.

If you don’t know how to use this command, this article will show you how to use it in a variety of ways, from the most basic to the most advanced.

Tutorial Details

DescriptionCopy Files and Directories
Difficulty LevelLow
Root or Sudo PrivilegesNo
OS CompatibilityUbuntu, Manjaro, Fedora, etc.
Prerequisitescp
Internet RequiredNo

Syntax of the cp Command

The cp command takes three arguments: one is the option, and the other is the source and destination paths.

$ cp [OPTION] [SOURCE] [DESTINATION]

There are a few things you should know before using this command, followed by:

  • If the β€œ[DESTINATION]” is not specified with the β€œ[SOURCE]β€œ, then it will force you to specify it.
  • If β€œ[SOURCE]” and β€œ[DESTINATION]” are in the same location, any already existing file that matches the name of β€œ[DESTINATION]” will get replaced without any warning (or use the β€œ-i” flag to get the confirmation message before replacing).

The following are a few known options that you should know before using this command:

OptionsDescription
-vPrint what files are being copied.
-pPreserved the access mode and the timestamp of the source file.
-fForce the process by removing or replacing the existing files.
-iIt will ask you if you want to do the current action. Use this to make sure you don’t lose any files.
-bIf file β€œA” is copied as file β€œB” and file β€œB” already exists in the same location, then it will rename it to β€œB~β€œ.
-rRecursively copy all the files and subdirectories into a directory.
-nAvoid overwriting existing files.
-uOverwrite if the target file is old or doesn’t exist.

Copying a File Using the cp Command

If you intend to copy the file to the same location, then rename it while copying.

$ cp file.txt file1.txt

Output:

Copying the file in same location with different name

Or, you can copy the file to a different path by specifying the absolute or relative path.

$ cp file.txt ~/Documents/

Output:

Copying the file to different path

If the file already exists in the destination path, give it a different name.

$ cp file.txt ~/Documents/file2.txt

Output:

Copying the file to different path with different name

You can copy several files with their full paths to the same destination path by putting a space between each file.

$ cp file.txt file2.txt ~/Documents/

Output:

Copying multiple files to different path

If multiple files share a common syntax or order, like β€œfile1.txtβ€œ, β€œfile2.txtβ€œ,.., β€œfile.5β€œ, then instead of specifying their full paths, you can use the wildcard to specify all files that share the same filename.

$ cp file*.txt ~/Documents/

Output:

Copying multiple files to different location using regular expression

The following is a list of wildcard usage.

$ cp *.txt ~/Documents/                                                                         #Copy all files sharing the "txt" extension
$ cp *.jpg ~/Documents/                                                                        #Copy all files sharing the "jpg" extension

Copying an Entire Directory and its Subdirectories to a New Path

You can copy all of the files and subdirectories from the source directory to the destination directory by using the β€œ-r” flag.

$ cp -r mydir/ ~/Documents/

Output:

Recursively copying the files and subdirectories to new path

Displaying the File Names While Being Copied

The β€œ-v” flag will print each file’s and subdirectory’s name in the output while they are being copied to the destination directory.

$ cp -rv mydir/ ~/Documents/

Output:

Displaying the copied file names in the output

Prompting for Permission Before Copying the Files

If the source files already exist in the destination path, they get replaced. To prevent this from happening, you can use the β€œ-i” flag to prompt for β€œyes” or β€œno” before overwriting the files.

$ cp -ri mydir/ ~/Documents/

Output:

Prompt for permission while overwriting the files

Backup the Already Existing Files

If the file is already present in the destination path, the β€œ-b” flag will create a copy and add β€œ~” to the end of the filename.

$ cp -rb mydir/ ~/Documents/

Output:

Backup the already existing file

Only Copying the Unique Files

You can use the β€œ-n” flag instead of the first two commands. This will only copy new files and directories from the source to the destination directory, leaving the ones that are already there alone.

$ cp -rn mydir/ ~/Documents/

Output:

Copying only unique files to destination path

This way, your existing file will not get replaced or deleted while copying.

Final Tip!

When copying files or directories from one path to another, the cp command is best; however, some users prefer the rsync command; it is entirely up to you.

To learn more about the cp command, you can check its man page using the β€œman cp” command.

Join The Conversation

Users are always welcome to leave comments about the articles, whether they are questions, comments, constructive criticism, old information, or notices of typos. Please keep in mind that all comments are moderated according to our comment policy.