As a Linux administrator, you must understand the importance of running commands via a command-line interface. They perform various system-related tasks on a daily basis. One of the frequent tasks is to copy directory in Linux from one location to another. If you have already worked with the Linux system, you must know how structured the file system is. 

If you are new to executing Linux commands, this guide will help you understand the working of various commands and their different options or arguments. 

Copy Linux Command

Linux offers a simple command for copying a file from one location to another. The command is "cp" with a straightforward argument. You can use the following syntax-

cp [OPTIONS] SOURCE... DESTINATION

The source can have more than one file or directory in the above syntax, and the destination can be a file or directory to store the copied data.

**Note: But whenever you execute the cp command, make sure to keep the following points in mind to eliminate the chances of human error.

  • For copying a file from a source location to a destination, the source and destination arguments should be a file with the proper file extension. If the destination file does not exist already within the desired path, this command will create one.
  • If you are copying multiple files or directories and passing them as a source argument, then the destination should be a directory to store all the files at once. If the destination argument is not a directory, you will get an error.
  • For copying the directories, both source and destination have to be a directory. If any argument is not a directory, you will get an error.

Also, if you are executing a copy command on a file or directory, you need to have read access to run the cp command in Linux.

Copying Files

Copying a file from source to destination is a simple process if both are in the current working directory. Suppose you want to copy the file a test1.txt to backup.txt, you can execute the following command.

For copying a file to another directory, you can also specify the relative or absolute path with the destination path. Whenever you copy a file to a directory, the copied file name should be the same as the original file. Suppose you want to copy a file "file.txt" to the demo directory as shown below.

cp file.txt ./test/backup

For copying the file to a different file name under the directory, you need to mention it specifically. Earlier, we copied the file.txt to the backup folder. Now, we are trying to copy the test.txt file to file1.txt in the backup folder. You can do this by executing the following command.

cp test.txt ./test/backup/file1.txt

Earlier, we don't have the file1.txt file under the backup folder. When the cp command does not find the file1.txt file, it will automatically create and copy the desired file.

If you have used the file.txt file instead of the file1.txt, the Linux copy file command will overwrite the existing file. To force overwrite the file, you can use the "-f" option along with the cp command, as shown below.

cp -f test.txt ./test/backup/file.txt

If you want confirmation before copying a file to another, you can use the "-i" option along with the cp command as shown below whenever you are asked; type y if you want to continue with the copying.

cp -i test.txt ./test/backup/file.txt

If you have some files to copy to a destination and those files are newer than the destination, you can use the "-u" options and the cp command to continue the copy process.

cp -u demo.txt ./test/backup/file1.txt

To preserve the newly created file mode while copying, you can use the "-p" option along with the cp command (can be interchanged with scp command) shown below.

cp -p demo.txt ./test/backup/file1.txt

For copying the file from source to destination with verbose output, you can use the "-v" option along with the cp command (scp Linux command), as shown below.

cp -v test.txt ./test/backup/file.txt

Copy Directory Linux

Whenever you copy directory, it will copy all of its content, including other files and directories, to the destination location. To copy a directory, you need to mention the "R" or "r" option along with the cp command. (scp command in Linux) Here, "R" or "r" stands for recursive, meaning the copy will continue until all the contents are copied.

cp -R demo ./test/backup

If you want to copy only the files and subdirectories without copying the original directory, you can mention the "-RT" option along with the cp command, as shown below.

cp -RT hello ./test/backup/

OR 

cp -RT hello/* ./test/backup/

Copying multiple files

For copying multiple files to the destination folder, you can mention multiple files named with the cp command (or scp Linux command) with the destination location at the end, as shown below.

cp cat.txt bat.txt ./test/backup/

Copy a directory on Linux to Remote Hosts

In certain circumstances, you might want to replicate a directory such that a backup copy is kept on a backup server. Naturally, your backup server is located locally, therefore you must replicate your directory over the network.

Copy a directory on Linux using rsync

You must use the rsync command, and provide the source folder, and the remote destination to be copied to to copy a Linux directory to remote sites.

Include the "-r" and "-a" options to indicate "recursive" and "all," respectively. If you don’t, non-regular files are going to be skipped.

$ rsync -ar <source_folder> <destination_user>@<destination_host>:<path>

Additionally, be careful to install the "rsync" software with sudo privileges if it isn't installed on your server.

$ sudo apt-get install rsync

$ sudo yum install rsync

As an example, let's use the necessity to copy the "/etc" folder to a backup server at 192.168.178.35/24.

With the "devconnected" username, we wish to copy a Linux directory to the remote server's "/etc backup".

We would use the following command to accomplish that:

$ rsync -ar /etc [email protected]:/etc_backup

Similarly, by adding a wildcard character after the directory to be copied, you can decide to copy the contents of the "/etc/ directory rather than the directory itself.

$ rsync -ar /etc/* [email protected]:/etc_backup/

Finally, you can use Bash parameter substitution to add the current date while running a directory backup.

$ rsync -ar /etc/* [email protected]:/etc_backup/etc_$(date "+%F")

Copy a directory on Linux using SCP

Linux users can use the "scp" command along with the "-r" option for recursive, the directory they wish to copy, and the destination folder to copy a directory to a remote location.

$ scp -r <source_folder> <destination_user>@<destination_host>:<path> 

As an illustration, suppose we want to copy the "/etc" directory to the "/etc backup" folder on the backup server at 192.168.178.35.

You would enter the following command to accomplish that:

$ scp -r /etc [email protected]:/etc_backup/

You can select to copy your directory to a specific directory on your server using Bash parameter substitution, which works very similarly to the rsync command.

$ scp -r /etc [email protected]:/etc_backup/etc_$(date "+%F")

Conclusion 

Copying files and directories is one of the everyday tasks performed by any Linux user, such as writing scripts or copying logs from one location to another. If you are a new user and do not understand the cp command's working, you can go through this guide to know how the command works differently with different options. 

This command has no loophole; even a beginner can get along with it quickly. So try out all the scenarios by yourself; happy coding! Also you can buy linux vps to practice it on a routine basis.

People also read: