en
  English
Phone :  +370 (5) 204-1903
Email :  sales@1gbits.com
  1. Dedicated server
  2. Blog
  3. How to copy folder/directory in Linux

How to copy folder/directory in Linux

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 files and directories from one location to another. If you have already worked with the Linux system, you must know how structured the file system is. 

Linux Tutorial Feb 11, 22 by Nisal N 5 min Read
How to copy folder/directory in Linux

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 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 file or directory, you need to have read access to run the "cp" command.

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 "cp" 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 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, as shown below.

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

Copying directories 

Whenever you copy a 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. 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 with the destination location at the end, as shown below.

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

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. 

There is no loophole in this command; even a beginner can get along with it quickly. So try out all the scenarios by yourself, Happy coding!

People also read: 

author img

Nisal N

Computers has always fascinated me since I was a kid and here we are. I love travelling for 2 reasons: the first one to see a new part of the world and second (the most important one) to experience the rich culture hidden among the country and people. I'm pretty good at cooking but very poor when it comes to baking.

Leave A Comment