How to Copy a File or Directory in Linux

As a Linux user, one of your most common tasks is copying files and folders to different locations on your local workstation or server. Drag-and-drop from the GUI and the cp command from the terminal are the most common methods for copying files in Linux.

Note that we’re using Peppermint OS 11 in this tutorial – it’s a Debian-based distro released in February 02, 2022, although all Linux commands used in this article are distro-independent, meaning they will work on any Linux distro.

Copying Files & Folders using the GUI

Long-time Windows users are familiar with the tried-and-true copy and paste method of copying files via drag-and-drop in the GUI. Linux systems, provided they have a desktop environment, work almost identically.

Note: we’re using two subdirectories, TechObservatory and TechObservatory II, and four files, techobservatory, techobservatory.com, techobservatory.com.txt, and techobservatory.txt.

Initial listing of files and directories.
Initial listing of files and directories.
  1. To copy a file or a folder from the Linux desktop environment, open both the folder containing the files/folder you want to copy, as well as the folder where you want to copy them to.
  2. Left-click the file or folder you want to copy, then select Copy from the dropdown menu.

    Copy a file to move in the Linux desktop environment.
    Copy a file to move in the Linux desktop environment.

  3. Navigate to the folder you want to copy the file or folder to (if you wish to select multiple files or folders, use the <Ctrl> key to select them selectively). Left-click on the folder itself and select Paste. Be patient while the file or folders are being copied.

    Pasting a file in the Linux desktop environment.
    Pasting a file in the Linux desktop environment.

  4. Note that the original files are still in their original location, as well as in the folder you copied them into.
Final result after copying file.
Final result after copying the file.

Copying Files & Folders with the cp Command

To use the cp command to copy files, you must first launch the terminal (<Ctrl><Alt>T).

Initial directory listing of files.
Initial directory listing of files.

Note that files and directories with space(s) must be referred to in quotes.

How to copy a single file

Simply copying a single file to another file, for example, is straightforward.

From the terminal, enter the following to move the techobservatory file to the techobservatory II folder:

# cp techobservatory "techobservatory II"
Copying a single file.
Copying a single file.

How to Copy a File or Directory

From the terminal, enter the following at the command-line:

# cp "techobservatory II" TechObservatory
Copy a file to a directory.
Copy a file to a directory.

How to Copy Multiple Files and Folders from One Directory to Another

From the terminal, enter the following at the command-line:

# cp "techobservatory techobservatory.com techobservatory.txt TechObservatory

Copy multiple files.Alternately, we could have copied these files using wildcards.

How to Copy Directories

Copying directories via the Linux desktop environment works identically to copying files.

From the terminal, enter the following at the command-line:

# cp -r "TechObservatory II" TechObservatory "TechObservatory III"
Copy multiple directories.
Copy multiple directories.

Note that we first had to create the “TechObservatory III” directory before we copied the directory. Also, note that we used the -r option to copy the directories recursively.

We’ve just touched the surface of the cp command and its possibilities in this article.

Other Options for Copying Files & Folders

The scp and rsync commands can also be used to copy files. However, these commands are generally reserved for copying files from one server to another.

Commands Used In this Article

  • cp –  copy files and directories.
  • scp – OpenSSH secure file copy.
  • rsync – a fast, versatile, remote (and local) file-copying tool.

Note:  You can always use the man command to get information and options on all commands featured on codingcampus.net

Last words

Remember, the man command is a great tool for learning the various options you can use with the cp command.

Leave a Comment