This post may contain affiliate links/ads and I may earn a small commission when you click on the links/ads at no additional cost to you. As an Amazon Affiliate, I earn from qualifying purchases. Techsphinx also participates in the StationX Affiliate program. You can read my full disclaimer here.
In this guide, I will show you how to copy files and directories in Linux using the terminal.
Moving, renaming, deleting and copying files and directories are the basic tasks that you need to perform every now and then on your system. Surely you can use the graphical way of copying files (using mouse or keyboard shortcut). However, copying files and directories in Linux using a terminal can help you achieve some complicated tasks very easily.
How to Copy Files in Linux using Terminal?
We will use the “cp” command (short for copy) to copy files in Linux.
Here’s the basic syntax of the “cp” command:
cp <OPTIONS> <SOURCE> <DESTINATION>
Things to Remember:
- The SOURCE can contain one or more files or directories as arguments, and the DESTINATION argument can be a single file or directory.
- If the SOURCE has multiple files or directories as arguments, then the DESTINATION argument must be a directory. In that case, the SOURCE files and directories will be copied to the DESTINATION directory.
- If the SOURCE and DESTINATION arguments are both directories, then the cp command copies the first (SOURCE) directory into the second (DESTINATION) directory.
- To copy files and directories, you must have at least read permissions on the SOURCE file and write permissions on the DESTINATION directory.
Copy files using cp command in Linux
Now, let’s see the “cp” command in action through different examples.
Copy file to the same location with a different name
cp file.txt new_file.txt
The above command will create a copy of “file.txt” on the same location with a different name (new_file.txt). This way you don’t have to rename the file after creating a copy of it.
Copy file to a Directory
cp file.txt data
The above command will copy “file.txt” to a directory called ‘data’ present in the same location.
Copying a file to a directory using absolute pathnames
You have to provide the full path of the source and destination if the source file and destination directory is not present in the same location.
cp /home/rahul/Documents/file.txt /home/rahul/Desktop/file.txt
This command will copy “file.txt” present in the “Documents” directory to the “Desktop” directory of the user.
Copy file to a directory with a different name
cp file.txt data/new_file.txt
This command will copy the “file.txt” to the “data” directory with a different name “new_file.txt”.
Copy Multiple Files
You can insert multiple files as sources in the “cp” command but the destination should be a directory. Just separate the names with a space.
cp file1.txt file2.txt file3.txt data
The above command will copy ‘file1.txt’, ‘file2.txt’ and ‘file3.txt’ to the ‘data’ directory present in the same location.
Prompt for confirmation
There are many options that you can use with the ‘cp’ command. These options provide additional functionalities.
cp -i file.txt data/file.txt
If the ‘file.txt’ already exists in the ‘data’ directory, then using the interactive (-i) option will prompt for confirmation to overwrite the destination file.
Note: If you don’t use the (-i) option in this scenario, then the ‘cp’ command will overwrite the file without asking. In this case, you might lose some important data. To avoid accidentally overwriting, it is recommended to always use the (-i) option while using the ‘cp’ command in Linux.
Copy the file only if it is newer than the destination
cp -u file.txt new_file.txt
The update (-u) option will only overwrite the file if the source file is newer than the destination file. This is useful when you’re taking backups.
Verbose cp command output
cp -v file.txt new_file.txt
The verbose (-v) option will allow you to see the actions taken by the ‘cp’ command.
Copy files using Wildcards
You can use wildcards to copy all the files that follow a particular pattern in their name.
For example, to copy all the files that end with the ‘.png’ extension to the ‘dir1’ directory in the backup directory, run the following command:
cp -v *.png backup/dir1
Copy files from multiple source locations to a single destination directory
cp data1/file1.txt data2/file2.txt data_backup
The above command will copy ‘file1.txt’ from the ‘data1’ directory and ‘file2.txt’ from the ‘data2’ directory to the ‘data_backup’ directory.
Copy files from multiple source locations to present location
cp data1/file1.txt data2/file2.txt .
The dot (.) in destination means present location. The above command will copy file1.txt and file2.txt to the present working directory (from the location you are executing the ‘cp’ command. Use ‘pwd’ command to check.)
How to Copy Directories in Linux using Terminal?
Now, let’s see how to copy directories in Linux using the ‘cp’ command.
Copy Directories using cp command in Linux
Copy single directory
If you try to copy a directory just like copying a file using the ‘cp’ command, you’ll encounter an error saying ‘-r’ is not specified.
To copy a directory using the ‘cp’ command, all you have to do is use the recursive (-R or -r) option.
cp -r data data_backup
The above command will make a copy of the ‘data’ directory with a different name ‘data_backup’ at the same location. If the “data_backup” directory exists, then the ‘cp’ command will copy the ‘data’ directory into the ‘data_backup’ directory.
Copy multiple Directories
Just like copying multiple files, you can also copy multiple directories using the ‘cp’ command.
cp -r data1 data2 data3 data_backup
The above command will copy ‘data1’, ‘data2’ and ’data3’ to the ‘data_backup’ directory.
Copy only subdirectories and files
If you want to only copy the subdirectories and files of a directory instead of the entire parent directory, then you can use the “no target directory” (-T) option.
cp -rT data data_backup
The above command will copy only the contents of the ‘data’ directory to the ‘data_backup’ directory.
Copying directories using Wildcard
Similar to copying files, you can use wildcards to copy directories.
cp -r data* backup
The above command will copy all the directories that start with the word “data” to the ‘backup’ directory.
Note: If you have any file that starts with the word “data” along with the directories, then the command will also copy the files to the ‘backup’ directory. This is because the recursive (-R) option is not only meant for copying directories, it will copy both files and directories recursively.
Verbose cp command output
You can use the verbose (-v) with the recursive (-R) option to see the actions taken by the ‘cp’ command.
cp -rv data data_backup
When copying directories, you can also use all the other options of the ‘cp’ command that you used while copying files. The only difference is that you have to use it with the recursive (-R) option.
Additional Options of ‘cp’ Command
Below mentioned are some of the options that you can use with the ‘cp’ command. You’ve already seen some of these options above.
|-v||verbose||Shows the progress of multiple copied files.|
|-p||preserve||Keeps the same attributes like creation date, last modified time and file permissions.|
|-f||force||Force the copy by deleting an existing file first.|
|-i||interactive||Prompts for confirmation before overwriting.|
|-r, -R||recursive||Copies directory along will its contents (sub-files and sub-directories) recursively.|
|-T||no-target directory||Copy only contents of the source directory to the destination directory.|
|-u||update||Copy only if the source is newer than the destination.|
There are many other options that you can use with the ‘cp’ command. Check the “man page” for more details.
This is how you copy files in Linux using the terminal. There are also other command-line utilities (Rsync, SCP etc.) that offer more features or allow you to copy files and directories from one system to another.
However, using the ‘cp’ command is the most basic and simple way to copy files and directories on a Linux system. You’ve seen how it can help you save a lot of time and improve your workflow over using the mouse or keyboard shortcuts. If you have any questions, feel free to ask them in the comments.