How to Copy Files and Directories Using Linux Terminal

Suraj Joshi Feb 02, 2024
  1. Copy Files Using the cp Command
  2. Copy Files to a Directory Using cp Command
  3. Copy Multiple Files
  4. Copy Directories Using cp Command
  5. Copy Files and Directories Using the rsync Command
How to Copy Files and Directories Using Linux Terminal

We can copy files and directories with cp and rsync commands using Linux Terminal. The cp command is generally used to copy files while rsync command is generally used to copy directories.

Copy Files Using the cp Command

We use the cp command in Linux and Unix Operating Systems for copying files and directories.

To copy the contents of the file abc.txt to another file backup.txt, we use the following command:

cp abc.txt backup.txt

It copies the content in abc.txt to backup.txt. Here both the files must be in the current working directory.

If the destination file already exists, the contents of the destination file will be overwritten.

We can add the -i flag to the cp command to get a confirmation prompt before copying.

cp -i abc.txt backup.txt

Output:

cp: overwrite 'backup.txt'?

If we wish to copy the file, we press the Y key and hit Enter.

To get information or idea of what is being done using the cp command, we can use the -v flag with the cp command.

cp -v abc.txt backup.txt

Output:

'abc.txt' -> 'backup.txt'

It shows the contents of abc.txt are being copied to backup.txt.

Copy Files to a Directory Using cp Command

To copy a file to a directory, we use the cp command. The first argument to the cp command will be the name of the file to be copied, and the second argument will be the absolute path or relative path of the directory in which the file needs to be copied.

cp abc.txt ./test

It copies the file abc.txt in the current working directory to the folder in the current working directory test.

To copy the file in the particular directory with a different name from the parent file, we can specify the file’s name as:

cp abc.txt ./test/test.txt

It copies the file abc.txt into the folder test with the name test.txt.

Copy Multiple Files

We can copy multiple files and directories to a particular directory using the cp command by specifying all the source files and directories followed by the destination directory at the end.

cp abc.txt backup.txt test backup

This copies the files abc.txt and backup.txt and the folder test into the folder backup.

The cp command also allows pattern matching.

cp *.txt backup

This copies all the files with .txt extension in the current working directory to the backup folder.

Copy Directories Using cp Command

We use -r or -R flag along with the cp command to copy the directory and its subdirectories and files to the destination directory.

cp -r pp Project

It copies the entire pp directory and its subdirectories and files to the destination directory Project.

In this example, there will be a pp directory inside the Project directory.

However, if we only wish to copy files and subdirectories, we use the -T flag along with the -R flag.

cp -RT pp Project

It copies the subdirectories and files of the pp directory to the destination directory Project.

Copy Files and Directories Using the rsync Command

rsync is a command-line utility to synchronizes files and directories between two hosts. If both the source and destination paths represent the localhost, the rsync command behaves as a copy command.

rsync -a abc.txt backup.txt

It copies the content of abc.txt into backup.txt.

Here, -a flag accounts for preserving symbolic links, modification times, group, ownership, and permissions while copying.

Similarly, we can copy a directory to another directory.

rsync -a /abc /backup-abc

It copies the contents of directory abc into the destination directory backup-abc.

Here, if we omit the trailing slash /, then the source directory would be copied inside the destination directory.

Author: Suraj Joshi
Suraj Joshi avatar Suraj Joshi avatar

Suraj Joshi is a backend software engineer at Matrice.ai.

LinkedIn

Related Article - Linux Files and Directories