1. Overview
In Linux, we use the cp command to copy files and directories. It offers a quick and efficient way to duplicate data within our system. Furthermore, we can use it to create backups, share documents, and organize data. Understanding how to use this command can simplify our workflow and ensure efficient data management.
In this tutorial, we’ll discuss the cp command along with some of its options.
2. Understanding the Basics
The cp command allows us to copy files and directories from one location to another.
Furthermore, it follows a basic syntax:
$ cp [options] source destination
Let’s break it down:
- [options] – represents one or more optional flags used to modify the behavior of the cp command
- source – specifies the path to the file or directory we want to copy
- destination – specifies the location where we want the file or directory to be copied
To illustrate, let’s explore some basic usage of this command.
2.1. Copying a File Within the Same Directory
We can copy a file and give it a new name by specifying the source file and the destination file within the same directory:
$ cp results.txt results_backup.txt
In the example above, we create a copy of the results.txt file and name it results_backup.txt within the same directory. We can use it to make a backup copy of a file before changing it.
2.2. Copying a File to Another Location
So, let’s copy a single file from one location to another:
$ cp names.txt /home/samuel/Documents
Using the above command, we copy the names.txt file from the current location to the Documents directory within the user samuel‘s home directory.
To clarify, if there’s already a file named names.txt in the destination directory, it will be overwritten by the copied file.
Additionally, we can also copy multiple files:
$ cp receipt.pdf results.txt fruits.txt /home/samuel/Documents
Here, we copy the receipt.pdf, results.txt, and fruits.txt files from the current directory to the /home/samuel/Documents directory.
2.3. Copying a Directory to Another Location
To copy a directory and all its contents, we’ll use the -r option. This option ensures all files and subdirectories within the source directory are copied:
$ cp -r icons /home/samuel/Desktop
Above, we copy the icons directory and all its contents from its current location to the Desktop directory within the samuel user’s home directory.
3. cp Command Options
The basic usage of cp is essential, but understanding its various options can help us efficiently handle more complex copying tasks. Let’s explore some advanced usage of cp using various options and wildcards.
3.1. Preserving File Attributes
By default, cp copies only the content of files without preserving their attributes such as permissions, timestamps, and ownership. However, by using the -p option, we can retain these attributes:
$ cp -p Students.csv /home/samuel/Downloads
In the above command, we copy the Students.csv file to the /home/samuel/Downloads directory while preserving its original attributes.
3.2. Verbose Output
Using the -v option, we can display detailed information about each file as it is copied:
$ cp -v receipt.pdf Students.csv /home/samuel/Documents
'receipt.pdf' -> '/home/samuel/Documents/receipt.pdf'
'Students.csv' -> '/home/samuel/Documents/Students.csv'
Here, we copy the receipt.pdf and Students.csv files to the /home/samuel/Documents directory. The -v option displays each file copied along with its destination.
What’s more, we can combine the -r and -v options when copying files:
$ cp -rv geoplug icons /home/samuel/Documents
'geoplug' -> '/home/samuel/Documents/geoplug'
'geoplug/assets' -> '/home/samuel/Documents/geoplug/assets'
'geoplug/assets/css' -> '/home/samuel/Documents/geoplug/assets/css'
'geoplug/Book1.csv' -> '/home/samuel/Documents/geoplug/Book1.csv'
'geoplug/Book1.xlsx' -> '/home/samuel/Documents/geoplug/Book1.xlsx'
'icons/' -> '/home/samuel/Documents/icons'
'icons/Identification Documents.png' -> '/home/samuel/Documents/icons/Identification Documents.png'
'icons/User.png' -> '/home/samuel/Documents/icons/User.png'
'icons/Phone.png' -> '/home/samuel/Documents/icons/Phone.png'
'icons/Lock.png' -> '/home/samuel/Documents/icons/Lock.png'
Using the above command, we recursively (-r) copy the contents of the geoplug and icons directories to the /home/samuel/Documents directory. We also display a verbose output of each file and subdirectory as it’s copied, making it easier to track the progress and identify any issues.
This option is useful when copying multiple files or directories.
3.3. Interactive Mode
When we copy a file to another directory and the file already exists in the destination directory, it will be overwritten. To prevent this, we use the -i option. This option prompts us for confirmation before overwriting existing files in the destination directory, helping us prevent data loss:
$ cp -i names.txt /home/samuel/Desktop
cp: overwrite '/home/samuel/Desktop/names.txt'?
Here, we try to copy the names.txt file to the /home/samuel/Desktop directory. However, we’re prompted with a message asking whether we want to overwrite the existing file since there’s already a file with the same name in the destination directory. We can type y to proceed or n to abort.
On the other hand, when copying directories, the interactive mode behaves differently. When we use interactive mode with directories, cp prompts us for confirmation before copying each file within the directory tree.
To clarify, for every file we encounter during the recursive copying process, cp prompts us whether to overwrite an existing file with the same name in the destination directory. Here, we’ll combine the -r and -i options:
$ cp -ri VoiceOver /home/samuel/Desktop
cp: overwrite '/home/samuel/Desktop/VoiceOver/.git/description'? y
cp: overwrite '/home/samuel/Desktop/VoiceOver/.git/info/exclude'? y
cp: overwrite '/home/samuel/Desktop/VoiceOver/.git/hooks/pre-rebase.sample'? n
cp: overwrite '/home/samuel/Desktop/VoiceOver/.git/hooks/prepare-commit-msg.sample'? n
cp: overwrite '/home/samuel/Desktop/VoiceOver/.git/hooks/pre-merge-commit.sample'? y
...
Above, we try to copy the VoiceOver directory to the /home/samuel/Desktop directory. However, we encounter some existing files in the destination directory with the same names as files in the source directory. So, cp prompts us whether to overwrite these files or not.
3.4. Copying Files Matching a Specific Pattern
We can copy files matching a specific pattern by using wildcards. These are special characters representing one or more characters in a string. The asterisk (*) wildcard represents zero or more characters, while the question mark (?) wildcard represents exactly one character.
To demonstrate, let’s copy all pdf files in the current directory. We’ll use the asterisk wildcard to match all files with a .pdf extension:
$ cp *.pdf /home/samuel/Documents
In the example above, we copy all files in the current directory with the .pdf extension to the /home/samuel/Documents directory.
Furthermore, we can copy all files containing a specific string:
$ cp *report* /home/samuel/Documents/
Using the above command, we’ll copy all files in the current directory that contain the string report in their filenames to the /home/samuel/Documents directory.
Finally, let’s copy files from multiple directories matching a specific pattern:
$ cp /home/samuel/Desktop/*.csv /home/samuel/Downloads/*.csv /home/samuel/Documents
The above command copies all .csv files from the /home/samuel/Desktop/ and /home/samuel/Downloads/ directories to the /home/samuel/Documents directory. Using the asterisk wildcard, we ensure only files with a .csv extension are copied.
4. Conclusion
In this article, we explored how to use the Linux cp command to copy files and directories through examples.
First, we discussed the basic usage of cp, such as copying single and multiple files, as well as copying entire directories. Next, using various options, we customized the copying process to manage files and directories on our system efficiently. Finally, using wildcards, we copied files matching a specific pattern.