DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Trending

  • GitHub Copilot's New AI Coding Agent Saves Developers Time – And Requires Their Oversight
  • The Future of Java and AI: Coding in 2025
  • Scaling Microservices With Docker and Kubernetes on Production
  • Rust, WASM, and Edge: Next-Level Performance

How the cp Command Works on Linux

The CPcpcommand is used to copy files and documents on Linux and other Unix-like systems like MacOS. Let's look at how it works.

By 
Johnny Simpson user avatar
Johnny Simpson
DZone Core CORE ·
Apr. 07, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
3.5K Views

Join the DZone community and get the full member experience.

Join For Free

The cp command (not to be confused with cd), allows us to copy files or directories. As such it is very commonly used on Linux and Unix-like systems like MacOS.

The syntax for cp is shown below, where [OPTIONS] are optional settings we can change, SOURCE is one or more files/directories we want to copy, and LOCATION is where we want to copy them to:

cp [OPTIONS] SOURCE LOCATION


How to Copy Files With cp on Linux and macOS

At its most basic, we can use cp to copy a file or directory to a new location. For example, the following command will copy a file called my-file-1.txt to a directory called test:

cp my-file-1.txt ./test


In the above example, we don't give a file name, so the original file name is used. If we add a file name, we can copy the file with a new name. The following example will copy the file, and save it as new-file.txt in the test directory:

cp my-file-1.txt ./test/new-file.txt


If the file already exists, it will be overwritten. If you want to avoid that, just add the -n option, which will prevent any duplicate files from being overwritten:

cp -n my-file-1.txt ./test


If you'd instead like to confirm when a file is going to be overwritten, use the -i option. This will trigger a prompt asking if you want to overwrite it:

cp -i my-file-1.txt ./test


On Linux only, you can also use the -u option, which will only overwrite files if the file is older than the file you want to overwrite it with. This will not work on MacOS.

cp -u my-file-1.txt ./test


Finally, if you want a response whenever a cp command is complete, use -v to get a verbose message which will tell you exactly what's happened:

cp -v my-file-1.txt ./test
# my-file-1.txt -> ./test/my-file-1.txt


Maintaining File Permissions When Copying a File on Linux and Mac

If you want to maintain all the permissions that existed on the file you are copying when you copy it to its new directory, use the -p option. If you don't, the owner will be whoever is using the cp file:

cp -p my-file-1.txt ./test


How to Copy Directories With cp on Linux and Mac

So far we've looked at how to copy files. If we want to copy directories, we need to use the -R option, which stands for recursive. When we use this option, we copy the entire directory and all its children to a new location. For instance, to copy the test directory and call this copied directory newTest, you would write the following in the terminal:

cp -R ./test ./newTest


How to Copy Multiple Files and Directories With cp

To copy multiple items at once, list them all out, and have the last location as the place where you want to copy all that stuff to. For example:

cp my-file-1.txt my-file-2.txt my-file-3.txt ./newTest


And if you want to include folders when you copy multiple things, use the -R option:

cp -R my-file-1.txt my-file-2.txt ./test ./newTest

Published at DZone with permission of Johnny Simpson, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!