How To Copy Files and Directories in Linux

Copy Files and Directories in Linux
(Image credit: Tom's Hardware)

Copying files and directories in the Linux terminal is really efficient and straightforward once you know how to do it. It’s 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 desk or if we are accessing a machine remotely via SSH.

In this how-to we’ll look at the cp command which copies files and directories. We’ll look at some arguments that we can add to get the most out of this command.

While you become accustomed to these commands it’s good to work with example empty files and directories and you should take extra care to ensure you are carefully following the instructions.

All the commands in this how-to will work on most Linux machines. We’ve used a Ubuntu 20.04 install but you could run this how-to on a Raspberry Pi. All of the how-to is performed via 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 really simply by copying a single file. The command syntax is structured as follows.

cp <location and name of item to copy> <location and name for copied item>

1. Create a new file called test1.txt.

touch test1.txt

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

ls

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

cp test1.txt test2.txt

4. List the contents of the directory to check the file has been 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 run through a similar example to show that cp can be used to copy a directory by using the -r argument. Though we have 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 directory contents to check the copied directory is created. You should see the directories test_directory and test_directory_2 listed.

ls

How to Copy Files to a Different 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 into the test directory. This copies test1.txt from the home directory into test_directory.

$ cp test1.txt test_directory

4. Check the file has been copied correctly by changing 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 move into the directories directly and you can run the cp command from the home directory. Here’s an example of how it could be done.

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

mkdir folder1 folder2

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

cd folder1
touch test1.txt

3. Return to the home directory.

cd

4. Copy and rename the test file from the home directory. We can supply the cp 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 has been copied correctly by changing directory to folder2 and listing the contents. Use cd to change directory and ls to check the file copied 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 need to occasionally copy a file to the parent directory of a current location. This can be achieved without having to type long directory locations into a command by using .. to target the parent directory. Here’s how.

1. Create a test directory using mkdir.

mkdir test_directory

2. Move into test_directory.

cd test_directory

3. Create a test file inside test_directory.

touch test1.txt

4. Copy test1.txt to the parent directory. Here we simply add .. to the cp command to create a copy of test1.txt into the parent directory of the current location.

cp test1.txt ..

5. Move to the parent directory and check if the file has been copied. You should see that test1.txt is now listed at this location.

cd
ls

As you can see the cp command is very simple to use but is very capable and, with a little practice, it’s often much quicker to be able to surgically copy and rename files using cp than using graphical interface tools.

Jo Hinchliffe

Jo Hinchliffe is a UK-based freelance writer for Tom's Hardware US. His writing is focused on tutorials for the Linux command line.  

  • Colif
    strange news, looks like a tutorial to me
    Reply
  • Grobe
    Colif said:
    strange news, looks like a tutorial to me
    Agree, it seems to covers the most basic commands for file and folder copying/moving.

    But one have to start somewhere. There is much more to learn.
    Reply
  • Sergei Tachenov
    rsync -aq does it all, copying everything, preserving symlinks, timestamps and permissions, works across LAN through SSH. You don't really need any other command if you have rsync installed.
    Reply
  • Grobe
    Sergei Tachenov said:
    You don't really need any other command if you have rsync installed.
    So far, I haven't' came across a Linux distro that does not have rsync included from start
    Reply
  • Putaku_Sama
    "How 2 copy files and directories in Linux"
    Me: Ctrl+C, Ctrl+V
    In terminal
    Me: oh srry
    Reply
  • Grobe
    Putaku_Sama said:
    "How 2 copy files and directories in Linux"
    Me: Ctrl+C, Ctrl+V
    Bonus feature: You can paste that text into any text editor directly. No cannot do in Windows.

    Bonus feature #2: Select any text, then put the text cursor somewhere, then click middle mouse button.
    Reply