Copy files and directories with “cp” command

Share this post to your friends !

This entry is part 1 of 4 in the series Linux commands: Part 4 - Manipulating files and directories

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

copy-files-and-directories-with-cp-command-01 Copy files and directories with "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.

Wildcards Table
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:

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
copy-files-and-directories-with-cp-command-02 Copy files and directories with "cp" command
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
copy-files-and-directories-with-cp-command-03 Copy files and directories with "cp" command
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.

Continue reading the seriesNext part: Move and rename in Linux with mv command »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.