Copy files and directories with “cp” command

by Daniel Pham
Published: Updated:
This entry is part 1 of 4 in the series Linux commands: Part 4 - Manipulating files and directories

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

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.

example about cp command
Example about cp command

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.

WildcardMeaning
*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 ClassMeaning
[: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
use cp command with wildcards ex.01
Use cp command with wildcards (ex. 01)
  • The second example: copy all files beginning with the install into /home directory
cp install* /home
  • The third example: copy files starting with a or d characters
cp [ad]* /home
use cp command with wildcards ex.02
Use cp command with wildcards (ex. 02)

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).

0 0 votes
Article Rating
Series NavigationNext part: Move and rename in Linux with mv command »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.