Ugacomp

How to copy files and directories in Linux

Where necessary, you may need to have access to a VPS server so you can follow how to implement the steps in this article.  You can get a cheaper VPS Server from Contabo with 4vCPU cores, 8GM RAM, and 32TB Bandwidth for less than $5.50 per month. Get this deal here now

Table of Contents

Cloud VPS S

$5.50 Monthly
  • 4 vCPU Cores | 8GB RAM

CLOUD VPS M

$15.50 Monthly
  • 6 vCPU Cores | 16GB RAM

CLOUD VPS L

$17.50 Monthly
  • 8 vCPU Cores | 24GB RAM

Copying files and directories is a fundamental task in Linux, and understanding the command-line options can greatly enhance your efficiency. In this article, we’ll explore various commands and examples to copy files and directories in Linux.

Copying Files with cp Command

The cp command is a versatile tool for copying files. Here’s a basic syntax:

cp [options] source destination

Copy from one location to another

To copy a file from one location to another, you can use the following command:

cp file.txt /path/to/destination/

Copy multiple files to a directory

The cp command allows you to copy multiple files to a directory:

cp file1.txt file2.txt /path/to/destination/

Copying Directories

When dealing with directories, the -r (or --recursive) option is essential to copy the contents recursively.

cp -r /path/to/source/ /path/to/destination/

The term “recursive mode” refers to the ability of a command or operation to work on a directory and its contents, including subdirectories, in a nested or hierarchical manner. In the context of commands like cp (copy) in Linux, the recursive mode is crucial when you want to copy not only the specified directory but also all its subdirectories and their contents.

Preserving Timestamps

The -p (or --preserve) option ensures that original timestamps are maintained:

cp -p file.txt /path/to/destination/

Preserving Ownership and Permissions

To copy files and directories while preserving ownership and permissions, use the -a (or --archive) option:

cp -a /path/to/source/ /path/to/destination/

Verbose Mode

If you want more details about the copy operation, use the -v (or --verbose) option:

cp -v file.txt /path/to/destination/

Prompt Before Overwriting

To prevent accidental overwriting of files, the -i (or --interactive) option prompts for confirmation:

cp -i file.txt /path/to/destination/

Conclusion

Mastering the cp command in Linux is crucial for efficient file and directory management. Whether you are copying individual files, entire directories, or preserving metadata, understanding the various options ensures you can tailor the command to your specific needs.

Hire us to handle what you want

Hire us through our Fiverr Profile and leave all the complicated & technical stuff to us. Here are some of the things we can do for you:

  • Website migration, troubleshooting, and maintenance.
  • Server & application deployment, scaling, troubleshooting, and maintenance
  • Deployment of Kubernetes, Docker, Cloudron, Ant Media, Apache, Nginx,  OpenVPN, cPanel, WHMCS, WordPress, and more
  • Everything you need on AWS, IBM Cloud, GCP, Azure, Oracle Cloud, Alibaba Cloud, Linode, Contabo, DigitalOcean, Ionos, Vultr, GoDaddy, HostGator, Namecheap, DreamHost, and more.
 

We will design, configure, deploy, or troubleshoot anything you want. Starting from $10, we will get your job done in the shortest time possible. Your payment is safe with Fiverr as we will only be paid once your project is completed.