How to Copy Files and Folders in Linux

Learn how to duplicate files and directories like a pro using Linux cp command.

The cp command in Linux stands for “copy.” It is a command-line utility used to copy files and directories from one location to another within the file system. By using the cp command, users can create duplicates of files or directories, preserving the original content.

source is the file or directory you want to copy, and destination is the location where you want to place the copy. Various options can be added to modify the behavior of the copy, such as preserving file attributes or providing verbose output. It’s a fundamental and widely-used command in Linux for managing files and directories.

In this post, we will look at some common ways the cp command is used to copy files and folders in Linux.

Syntax:

cp [options] source destination
1. Duplicating a file
cp foo.txt bar.txt

This command will copy the contents of the file foo.txt into a file named bar.txt.

Example:

Let’s say you have a file named foo.txt with the following content:

Hello, World!

If you run the command cp foo.txt bar.txt, it will create a new file named bar.txt with the exact same content as foo.txt:

Hello, World!

If bar.txt already exists, its content will be overwritten with the content of foo.txt. If bar.txt does not exist, it will be created.

2. Duplicating a directory (and its contents)
cp -R foo-folder bar-folder

The -R option stands for “recursive,” and it’s used to copy directories and their contents, including subdirectories.

Here’s what the command cp -R foo-folder bar-folder does:

  • cp: Invokes the copy command.
  • -R: Tells the command to operate recursively, copying all directories and subdirectories.
  • foo-folder: The source directory that you want to copy.
  • bar-folder: The destination directory where you want to copy the source directory.

Example

Let’s say you have a directory called foo-folder with the following structure:

foo-folder/
├── file1.txt
└── subfolder
    └── file2.txt

And you want to copy this entire directory into another directory called bar-folder.

You would run the command:

cp -R foo-folder bar-folder

After running this command, the bar-folder directory will have the same structure as foo-folder:

bar-folder/
└── foo-folder
    ├── file1.txt
    └── subfolder
        └── file2.txt

If bar-folder does not exist, it will be created. If it does exist, the foo-folder will be copied into it, preserving the structure of foo-folder.

Note: If you want to copy the contents of foo-folder directly into bar-folder without creating a foo-folder inside bar-folder, you would need to make sure bar-folder exists and then run:

cp -R foo-folder/* bar-folder/
3. Show the copying progress
cp -v foo.txt bar.txt

The -v option stands for “verbose,” and when used with the cp command, it provides detailed information about the operations being performed.

Example:

Suppose you have a file named foo.txt in your current directory, and you want to create a copy of this file in the same directory with a new name bar.txt. You can use the following command:

cp -v foo.txt bar.txt

If the operation is successful, the command will output a message like this:

'foo.txt' -> 'bar.txt'

This message confirms that the file foo.txt has been copied to bar.txt.

4. Confirmation to overwrite a file
cp -i foo.txt bar.txt

The -i stands for "interactive". When you use this option, the system will prompt you before overwriting any files. This is useful if you want to avoid accidentally overwriting existing files.

Example:

Let’s say you have a file named foo.txt in your current directory and you want to create a copy of it named bar.txt in the same directory. However, you’re not sure if a file named bar.txt already exists, and you don’t want to overwrite it without being warned.

You would use the command:

cp -i foo.txt bar.txt

If bar.txt already exists, the system will prompt you with a message like:

cp: overwrite 'bar.txt'?

You can then choose to overwrite it by typing y (yes) or avoid overwriting by typing n (no).

If bar.txt doesn’t exist, the command will simply create a copy of foo.txt named bar.txt without any prompt.

5. Copying multiple files to a directory
cp foo.txt bar.txt baz

This command will duplicate a copy of foo.txt and bar.txt in, into the baz directory. The baz directory must first exist in order for command to work.

More Linux commands:
Directory Operations rmdir · cd · pwd · exa · ls
File Operations cat · cp · dd · less · touch · ln · rename · more · head
File System Operations chown · mkfs · locate
Networking ping · curl · wget · iptables · mtr
Search and Text Processing find · grep · sed · whatis · ripgrep · fd · tldr
System Information and Management env · history · top · who · htop · glances · lsof
User and Session Management screen · su · sudo · open
WebsiteFacebookTwitterInstagramPinterestLinkedInGoogle+YoutubeRedditDribbbleBehanceGithubCodePenWhatsappEmail