Introduction

An essential task for Linux administrators is understanding the commands for copying. This guide will show you how to copy files and directories from the command line in Linux.

copying files and directories in Linux


Note: These Linux commands can only be run from a terminal window. If your version of Linux boots to a graphical interface, you can launch a terminal window by pressing ctrl-alt-f2 or ctrl-alt-T.


cp Command to Copy Files and Directories

The cp command is the primary method for copying files and directories in Linux. Virtually all Linux distributions can use the cp command. The basic format of the cp command is:

cp [options] source_file target_file

For example:

cp my_file.txt my_file2.txt

This creates a copy of the my_file.txt file and renames the new file my_file2.txt.

By default, the cp command runs in the same directory you’re working in. However, the same file can’t exist twice in the same directory. You’ll need to change the name of the target file to copy in the same location. Some users will add _old, some will add a number, and some will even change the three-letter extension (so .txt might be renamed .bak).

You may not get a warning before Linux overwrites your file – be careful, or see below for the –i option.

How to Copy to a Different Location in Linux

Toopy a file from the directory you’re working in to a different location, use the command:

cp my_file.txt /new_location

You don’t need to rename the file unless there’s already one with the same name in the target directory.

To specify a location for the source file:

cp /etc/my_file.txt /new_location

This lets you copy without having to change directories. The cp command will create the /new_location directory if it doesn’t exist.

To rename a file at the same time you copy it to a different location:

cp my_file.txt /new_location/my_file2.txt

This option is useful for creating backups of configuration files, or for copying data to a storage device.

How to Copy Multiple Files in Linux

You may need to copy more than one file in an operation.

To list each file to be copied before the target directory:

cp my_file.txt my_file2.txt my_file3.txt /new_location

This example created a copy of all three files in the /new_location directory.

Use a wildcard to specify all files that share a string of characters:

cp /pictures/*.jpg /new_location

This would find all the files with the .jpg extension in the /pictures directory, and copy them into the /new_location directory.

To copy an entire directory and its contents by using the –R option:

cp –R /documents /new_location

The –R option stands for recursive, which means “everything in that location.” This would copy all the files, plus all the directories, to the /new_location directory.

Copy Using rsync Command

The rsync command in Linux is used to synchronize data between two locations. Usage is similar to cp, but there are a few key differences to note.

To copy a single file, enter the following into a terminal:

rsync –a my_file.txt /new_location/my_file_backup.txt
  • The –a option means all, and is included with rsync commands – this preserves subdirectories, symbolic links, and other metadata
  • Replace my_file.txt any file in the working directory
  • Replace /new_location/ with the destination you’re copying to
  • Using my_file_backup.txt as the target indicates the file will be renamed during the copy

To copy a directory with rsync, enter the following:

rsync –a /etc/docker/ /home/backup/docker/

This copies the contents of the /etc/docker/ directory to /home/backup/docker/. Make sure to keep the slashes. Omitting the slash on the source directory will copy the contents into a subdirectory.

Other Options

The ls command is a handy partner to the cp command in Linux.

To list the contents of a directory enter the command:

ls /new_location

This displays all the files in the /new_location directory. Use this command after copying to verify the files were copied successfully.

Change directories by entering cd and the name of the directory. For example:

cd new_location

The command prompt will change to show that you’ve changed directories.

Like the –R option above, other options can enhance the cp command:

  • –v – verbose: shows the progress of multiple copied files
  • –p – preserve: keeps the same attributes, like creation date and permissions
  • –f – force: force the copy by deleting an existing file first
  • –i – interactive: prompts for confirmation

Conclusion

By now, you should know how to copy files at a Linux command line. This gives you a versatile and powerful tool for managing files.