How to Copy Files and Directories in Linux

Copying files and directories into the Linux terminal is really efficient and simple once you know how to do it. It is useful when we need to manage our files and directories without the need for a GUI, for example in a BASH script. We can do this whether we are at our office or if we are accessing a remote machine via SSH.

In this guide, we will look at the PC command that copies files and directories. We’ll look at a few arguments we can add to get the most out of this command.

While you get used to these commands, it’s good to work with sample empty files and directories, and you should take extra care to make sure you follow the instructions carefully.

All commands in this tutorial will work on most Linux machines. We used an Ubuntu 20.04 install, but you can run this guide on a Raspberry pie. All procedures are performed through the terminal. You can open a terminal window on most Linux machines by pressing ctrl, alt and t.

How to Copy a Single File in Linux

(Image credit: Tom’s Hardware)

Let’s start very simply by copying a single file. The command syntax is structured as follows.

cp  

1. Create a new file called test1.txt.

touch test1.txt

2. List the contents of the directory to check if the file was created. You should now see the test1.txt file.

ls

3. Copy it test1.txt file and rename it to test2.txt. This copies the item to the same directory as the original but to a new file.

cp test1.txt test2.txt

4. List the contents of the directory to verify that the file was copied and renamed. You should see that the original file test1.txt is listed and the copied file test2.txt is listed.

ls

How to copy a directory in Linux

(Image credit: Tom’s Hardware)

Let’s walk through a similar example to show that PC can be used to copy a directory using the -r argument. Although we used an empty directory as an example, any directory contents would also be copied to the new directory using this argument.

1. Create an empty directory in your home directory.

mkdir test_directory

2. Copy and rename the new directory.

cp -r test_directory test_directory_2

3. List the contents of the directory to verify that the copied directory is created. You should see the test_directory and test_directory_2 directories listed.

ls

How to Copy Files to Another Location in Linux

(Image credit: Tom’s Hardware)

For the next example, let’s look at copying a file to a different location.

1. Create an empty directory in your home directory.

mkdir test_directory

2. Create a test file in your home directory.

touch test1.txt

3. Copy the test file from the home directory to the test directory. This copies test1.txt from the home directory to test_directory.

$ cp test1.txt test_directory

4. Verify that the file copied correctly by changing the directory to test_directory and listing the contents.

cd test_directory
ls

How to copy files to and from directories while renaming them

(Image credit: Tom’s Hardware)

If you know the location of a file and the target location you want to copy the file to, you don’t need to jump through directories directly and you can run the PC command from home directory. Here is an example of how it could be done.

1. Create 2 empty directories. From the base directory, run the following command to create two new directories.

mkdir folder1 folder2

2. Move inside folder1 and create a test file called test1.txt.

cd folder1
touch test1.txt

3. back to residence phone book.

cd

4. Copy and rename the test file from the homepage phone book. We can provide the PC command with the name and location of the target file to copy and where to copy the file, renaming it to test2.txt along the way.

cp folder1/test1.txt folder2/test2.txt

5. Check that the file was copied correctly changing directory to folder2 and listing the contents. Use CD to change directory and ls to verify the copied file correctly. you should see test2.txt listed inside folder2.

cd folder2
ls

How to copy a file to a parent directory

(Image credit: Tom’s Hardware)

As you move through directories, you may occasionally need to copy a file to the parent directory of a current location. This can be achieved without typing long directory locations in a command using .. to target the parent directory. Here’s how.

1. Create a test directory using mkdir.

mkdir test_directory

2. Move to test_directory.

cd test_directory

3. Create test file inside test_directory.

touch test1.txt

4. Copy test1.txt in the parent directory. Here we just add .. to the PC to create a copy of test1.txt in the parent directory of the current location.

cp test1.txt ..

5. Move to the parent directory and check if the file was copied. You should see that test1.txt is now listed there.

cd
ls

As you can see the cp command is very simple to use yet very capable and with a bit of practice it is often much faster to be able to surgically copy and rename files using cp than using GUI tools help.

Comments are closed.