Atlantic.Net Blog

How to Copy Files and Directories in Linux

Hitesh Jethva
by Atlantic.Net (85posts) under VPS Hosting
0 Comments

If you are a system administrator, copying files and directories is one of the most common tasks in your day-to-day work. The cp command is a basic Linux command used for copying files and directories from one location to another. By default, the cp command is available in almost all Unix- and Linux-like operating systems.

In this tutorial, we will show you how to use the cp command to copy files and directories in Linux.

Prerequisites

  • A fresh Ubuntu 20.04 VPS on the Atlantic.net Cloud Platform.
  • A root password configured on your server.

Create Atlantic.Net Cloud Server

First, log in to your Atlantic.Net Cloud Server.  Create a new server, choosing Ubuntu 20.04 as the operating system with at least 1GB RAM. Connect to your Cloud Server via SSH and log in using the credentials highlighted at the top of the page.

Once you are logged into your Ubuntu 20.04 server, run the following command to update your base system with the latest available packages.

apt-get update -y

The cp Command Options

The basic syntax of the cp command is shown below:

cp [OPTION] [SOURCE] [DESTINATION]

A brief explanation of each available option is shown below:

  • -a : Copy files and directories recursively and preserve links.
  • -i : You will be prompted before overwriting any existing file on the destination.
  • -r : Copy directory recursively.
  • -f : Remove an existing destination file if it can not be opened.
  • -u : Copy only when the SOURCE file is newer.

1. Copy a File to Directory

If you want to copy a single file named /etc/crontab to the /mnt directory, run the following command:

cp /etc/crontab /mnt

You can now verify it with the following command:

ls -l /mnt

You should see your file in the following output:

-rw-r--r-- 1 root root 1042 Jun 21 11:32 crontab

2. Copy Multiple Files to Directory

If you want to copy multiple files named /etc/hosts and /etc/hostname at the same time to the /mnt directory, run the following command:

cp /etc/hosts /etc/hostname /mnt

If you want to display verbose output during the copying process, run the following command:

cp -v /etc/hosts /etc/hostname /mnt

You should see the following output:

'/etc/hosts' -> '/mnt/hosts'
'/etc/hostname' -> '/mnt/hostname'

3. Copy a File Interactively

If you want to copy a file interactively, use the option -i with cp command. This option will prompt you before copying the file to the destination directory if the same file already exists.

cp -i /etc/crontab /mnt

You should see the following output:

cp: overwrite '/mnt/crontab'? yes

If you don’t want to overwrite an existing file, you can use the option -n.

cp -vn /etc/crontab /mnt

This will not prompt for the overwrite and also will not overwrite the existing file.

4. Copy a Directory Recursively

You can use option -r with cp command to copy files and directory recursively.

cp -r /etc/apache2 /mnt

You can also use -a option to archive the files and directory during the copy.

cp -ar /etc/apache2 /mnt

5. Preserve mode, ownership, and timestamps

If you want to preserve mode, ownership, and timestamps after copying files and directories, you can use -p option:

cp -arp /etc/apache2 /opt

6. Backup a Destination File

By default, the cp command will overwrite the file on the destination directory if the same file exists. In this case, you can use –backup option to back up any existing destination file during the copying process.

cp --backup=simple -v /etc/hosts /mnt

You should see the following output:

'/etc/hosts' -> '/mnt/hosts' (backup: '/mnt/hosts~')

7. Copy Multiple Files from One Directory to Another

You can also copy multiple files from one directory to another using the *. For example, if you want to copy all PHP files from the /var/www/wordpress directory to the /mnt directory, you can copy them with the following command:

cp -r /var/www/wordpress/*.php /mnt/

This command will find all files with .php extension in the /var/www/wordpress/ directory and copy them into the /mnt directory.

Conclusion

In this guide, you learned how to use the cp command to copy files and directories. Get started with the cp command today on a Linux VPS from Atlantic.Net!

Get A Free To Use Cloud VPS

Free Tier Includes:
G2.1GB Cloud VPS Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


Looking for a Hosting Solution?

We Provide Cloud, Dedicated, & Colocation.

  • Seven Global Data Center Locations.
  • Flexible Private, Public, & Hybrid Hosting.
  • 24x7x365 Security, Support, & Monitoring.
Contact Us Now! Med Tech Award FTC
SOC Audit HIPAA Audit HITECH Audit

Recent Posts

Get started with 12 months of free cloud VPS hosting

Free Tier includes:
G2.1GB Cloud VPS Server Free to Use for One Year
50 GB of Block Storage Free to Use for One Year
50 GB of Snapshots Free to Use for One Year


New York, NY

100 Delawanna Ave, Suite 1

Clifton, NJ 07014

United States

San Francisco, CA

2820 Northwestern Pkwy,

Santa Clara, CA 95051

United States

Dallas, TX

2323 Bryan Street,

Dallas, Texas 75201

United States

Ashburn, VA

1807 Michael Faraday Ct,

Reston, VA 20190

United States

Orlando, FL

440 W Kennedy Blvd, Suite 3

Orlando, FL 32810

United States

Toronto, Canada

20 Pullman Ct, Scarborough,

Ontario M1X 1E4

Canada

London, UK

14 Liverpool Road, Slough,

Berkshire SL1 4QZ

United Kingdom

Resources