Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Syntax of cp Command
3.
How to Copy Files in Linux with the cp Command
3.1.
Copying Between Two Files in Linux
3.2.
Copy Files to a Directory in Linux
3.3.
How to Copy Directories in Linux
4.
Options Available in cp Command in Linux
4.1.
-i (or --interactive)
4.2.
-r (or --recursive)
4.3.
-v (or --verbose)
4.4.
-u (or --update)
4.5.
--preserve
5.
How to Copy Files Using Options Available in cp Command in Linux
5.1.
Interactive Copying with -i
5.2.
Verbose Copying with -v
5.3.
Preserving File Attributes with -a
5.4.
Avoiding Overwrite with --no-clobber
6.
Frequently Asked Questions
6.1.
Can I recover a file overwritten by the cp command?
6.2.
Is it possible to copy multiple files at once with cp?
6.3.
How do I exclude certain files when copying with cp?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

Copy Command in Linux

Author Riya Singh
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

The cp command is a tool used in Linux to copy files and directories from one location to another. It's a straightforward yet essential command for file management, allowing users to duplicate files, create backups, or move data seamlessly within the file system. 

Copy Command in Linux

In this article, we will look into how to use the cp command effectively, covering everything from basic file copying to advanced directory management and the various options that can customize the command's behavior to suit different needs. 

Syntax of cp Command

The cp command in Linux is straightforward, yet powerful. Its basic syntax is as follows:

cp [options] source destination


Here, source refers to the file or directory you want to copy, and destination is where you want to place the copy. This simplicity is what makes the cp command a fundamental part of file management in Linux.

For example, to copy a file named example.txt from your current directory to another directory named Documents, you would use:

cp example.txt ~/Documents/


This command tells the system to take example.txt and place a copy in the Documents directory. It's as simple as telling a friend to hand you a book from one shelf and place it on another. No complications, just a straightforward task executed efficiently.

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

How to Copy Files in Linux with the cp Command

Copying files in Linux using the cp command is like moving a document from one folder to another on your computer. It's a basic task but essential for organizing your files or creating backups. Let's break down the steps for some common copying tasks you might need.

Copying Between Two Files in Linux

When you want to copy the contents of one file to another, the process is straightforward. If the target file doesn't exist, cp will create it for you. If it does exist, cp will overwrite it, so be careful!

For example, to copy the contents of file1.txt to file2.txt, you use:

cp file1.txt file2.txt


This command says, "Take everything in file1.txt and duplicate it into file2.txt." Remember, if file2.txt already has content, this action will replace that content with what's in file1.txt.

Copy Files to a Directory in Linux

Sometimes, you might want to copy one or more files into a different directory. The cp command handles this efficiently.

For instance, to copy file1.txt into the Documents directory, the command is:

cp file1.txt ~/Documents/


This tells the system, "Copy file1.txt and put this copy in the Documents directory." It's that simple.

How to Copy Directories in Linux

Copying directories, which might contain multiple files and subdirectories, requires an extra option -r (or --recursive) to ensure everything inside the directory is copied as well.

To copy an entire directory named Data to another directory called Backup, you would use:

cp -r Data/ Backup/


This command means, "Copy the whole Data directory, with all its files and subfolders, into the Backup directory."

Options Available in cp Command in Linux

The cp command comes with a set of options that can modify its behavior to better fit your needs. These options allow you to control how the command works, making it more flexible and powerful. Let's look at some of the most useful options:

-i (or --interactive)

This option makes cp ask you before it overwrites any file. It's like a safety net, ensuring you don't accidentally replace important files.

Example

cp -i file1.txt file2.txt


If file2.txt exists, the command will ask if you want to overwrite it, giving you a chance to avoid accidental data loss.

-r (or --recursive)

This is crucial for copying directories because it tells cp to copy not just the directory itself but everything inside it, including other directories and their contents.

Example

cp -r folder1 folder2


This command copies everything in folder1 into folder2, maintaining the structure and contents.

-v (or --verbose)

With this option, cp will list what it's doing. It's like having cp narrate its actions, showing which files are being copied.

Example

cp -v file1.txt ~/Documents/


This will copy file1.txt to the Documents directory and show a message confirming the action.

-u (or --update)

This option tells cp to only copy a file if the source is newer than the destination or if the destination file doesn't exist. It's a way to update files without unnecessary copying.

Example

cp -u old_report.txt new_report.txt


If new_report.txt is older than old_report.txt, or if new_report.txt doesn't exist, cp will copy the file. Otherwise, it won't.

--preserve

This option allows you to keep specific attributes of the files, such as their mode, ownership, and timestamps. It's like telling cp to maintain the file's identity during the copy process.

Example

cp --preserve=all file1.txt file2.txt


This command ensures that file2.txt will have the same mode, ownership, and timestamps as file1.txt after copying.

These options enhance the cp command, allowing for more controlled and tailored file copying processes. Depending on what you need to achieve, combining these options can make your file management tasks more efficient and safer.

How to Copy Files Using Options Available in cp Command in Linux

Now that we're familiar with some of the options the cp command offers, let's work on few examples using them. These examples will show you how to use the options to handle specific copying tasks more effectively.

Interactive Copying with -i

If you're worried about accidentally overwriting files, the -i option is your safeguard. When you use it, cp will ask for your confirmation before replacing any file.

cp -i source.txt destination.txt

If destination.txt already exists, the system will ask if you want to overwrite it. You have to type y (yes) or n (no) to proceed or cancel the action.

Verbose Copying with -v

When you want to see what cp is doing, use the -v option. It's helpful when copying multiple files, and you want to track the progress.

cp -v file1.txt file2.txt destination_directory/

This command will show a message for each file being copied, so you know exactly what's happening.

Preserving File Attributes with -a

When copying files or directories, especially for backups, it's often important to keep their original attributes (like permissions and timestamps). The -a option ensures that everything is preserved.

cp -a /source_directory /backup_directory

This will copy the entire source_directory into backup_directory, keeping all original file attributes intact.

Avoiding Overwrite with --no-clobber

To prevent cp from overwriting existing files in the destination, you can use the --no-clobber option. This is particularly useful when you want to ensure that existing data is not lost.


cp --no-clobber source_file.txt destination_directory/

If a file named source_file.txt already exists in destination_directory, cp won't overwrite it.

Frequently Asked Questions

Can I recover a file overwritten by the cp command?

Once a file is overwritten with cp, recovery is not straightforward. It's essential to have backups to restore any lost data.

Is it possible to copy multiple files at once with cp?

Yes, you can copy multiple files to a destination directory in one command by listing all source files followed by the destination.

How do I exclude certain files when copying with cp?

The cp command doesn't support direct exclusion. For advanced copying with exclusions, consider using the rsync command instead.

Conclusion

In this article, we've learned the basics and some advanced uses of the cp command in Linux. Starting with what the cp command is and how it's used for copying files and directories, we've covered a range of topics to give you a better understanding. We discussed the syntax of the cp command, how to copy files between two locations, and how to duplicate entire directories. We also explored various options that cp provides, such as interactive mode, verbose mode, preserving file attributes, and preventing file overwrites.

You can refer to our guided paths on the Coding Ninjas. You can check our course to learn more about DSADBMSCompetitive ProgrammingPythonJavaJavaScript, etc. Also, check out some of the Guided Paths on topics such as Data Structure and AlgorithmsCompetitive ProgrammingOperating SystemsComputer Networks, DBMSSystem Design, etc., as well as some Contests, Test Series, and Interview Experiences curated by top Industry Experts.

Previous article
ssh Command in Linux
Next article
Hostname Command
Live masterclass