In Linux based operating system such as Ubuntu, Devian, Fedora , etc. All shares common command to copy files and directories using command line. If you are a Linux user, I am sure everyday you use to copy files and directories. To perform file copy and move, you must have read permission of source file and write permission for destination directory. In this article, I am going to explain how to copy files and directories in Linux.
So let’s get started.
What is cp command
‘cp’ is the most commonly used command to copy a file from one directory to another in Unix based operating systems such as Ubuntu, Fedora, etc. ‘cp’ creates an exact copy of the file on a disk with a different name.
Syntax of commonly used cp command syntax
$ cp [option] [source] [destination]
Explanation of the options use cp command syntax
|-i||Interactive copying, it will warns you before copying.|
|-b||This option will create a backup of the file in the destination directory with different name and format.|
|-f||force the copy by deleting an existing file first if the user has no write permission|
|-r or -R||Copies all files and sub directories in a directory recursively.|
|-v||It shows the progress of copying.|
|-p||It keeps the file attributes same, such as creation date and file permissions.|
|-u||Only copy if source file is newer than destination file|
How to Copy Files and Directories in Linux
Let’s discuss the command with all the options one by one. To explain, I am taking two files
Copy a file within the same directory
For, example to copying a file name
sample.txt to the file name
sample_new.txt in the same directory, you would run following command.
$ cp sample.txt sample_new.txt
Above command will copy the
sample.txt file to
sample_new.txt without any error if the
sample_new.txt is not exist on the directory.
-i option usage in
If the destination file name as mentioned above already exist, it will override the file
sample_new.txt without any error, unless you use flag option
$ cp -i sample.txt sample_new.txt
Output will be: cp: overwrite ‘sample_new.txt’?
-p option usage in
When a user perform copy operation by
cp command, the new file will be owned by the user who perform copy operation. To keep the attribute same as before use
$ cp -p sample.txt sample_new.txt
-v option usage in
When you use
-v option, it will print the progress.
$ cp -v sample.txt sample_new.txt
Output will be: ‘sample.txt’ -> ‘sample_new.txt’
Copy file to the directory
To copy a file to a directory, always specify the absolute or the relative path to the directory. Let’s take an example file name
file.txt and directory name
New_Files. Now we will copy
$ cp file.txt New_Files/
Above command will copy the file with same name in the directory. If you want to copy the file with different name, command will be
$ cp file.txt New_Files/rajesh.txt
Copy multiple files in Linux
If you want to copy multiple files to a directory, you have to specify the files name one by one followed by directory name at last argument.
$ cp a.txt b.txt c.txt multiple_files
You can also copy by pattern matching, suppose you have some file with
.txt extension and some files have other extension. Now, your mission is to copy only
.txt extension files to the directory, in that case you have use following command.
$ cp *.txt multiple_files
Copy directories using
Copy all the files and directories to another directories using following command.
$ cp -R multiple_file new_directory
Above command will copy all the files and directories from
multiple_file to the
rsync command to copy files and directories
Rsync is a Linux-based tool that can be used to sync files between remote and local servers. It is great for complex file syncs and for transferring a large number of files.
The basic command of rsync is
rsync [option] [origin] [destination]
Read More: How to Upload File to AWS EC2 Instance
In this article, I explained how to copy files and directories in Linux. If you have any doubts feel free to contact me, I will definitely try to help you. Also, you can join our elite Facebook group to get direct help from me. If you like this tutorial, please share this article on your social media handle.