With a file manager, we can drag and drop a file from one directory to another, copy and paste files, etc. So why use these old command line programs? The answer is power and flexibility. While it is easy to perform simple file manipulations with a graphical file manager, complicated tasks can be easier with the command line programs. And in Linux, to copy files or directories, you will use the cp
command.
Use simple cp
Table of Contents
The cp
command is quite simple, you call the cp
command under the command structure below, the source and destination here may be the name (or path) of the file or directory.
cp source destination
For example, I would copy the install.log
file into a second file named install2.log
.
About wildcards
These special characters are called wildcards. Using wildcards (which is also known as globbing) allows you to select filenames based on patterns of characters. The table below lists the wildcards and what they select.
Wildcard | Meaning |
* | Matches any characters |
? | Matches any single character |
[characters] | Matches any character that is a member of the set characters |
[!characters] | Matches any character that is not a member of the set characters |
[[:class:]] | Matches any character that is a member of the specified class |
Table below lists the most commonly used character classes:
Character Class | Meaning |
[:alnum:] | Matches any alphanumeric character |
[:alpha:] | Matches any alphabetic character |
[:digit:] | Matches any numeral |
[:lower:] | Matches any lowercase letter |
[:upper:] | Matches any uppercase letter |
Use cp command with wildcards
Here are some examples using the cp
command with wildcards:
- The first example: copy all files/folders in the folder
/root
to the directory/home
cp * /home
- The second example: copy all files beginning with the
install
into/home
directory
cp install* /home
- The third example: copy files starting with
a
ord
characters
cp [ad]* /home
To show more options, run command below:
cp --help
Conclusion
Using the cp
command to make copying in Linux is easy, but very effective in difficult cases that you can not do with graphics management.
(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).