Home > Articles

  • Print
  • + Share This

Copying Files and Directories

The ability to quickly and efficiently copy files and directories is important when using Linux. The following sections show you how to copy single or multiple files and directories.

Copying Files with the cp Command

The cp (copy) command is used to copy files. Use the original filename followed by a new filename on the command line to copy a single file:

$ cp notes notes.old

Use wildcards to copy multiple files to a new location:

$ cp *jpg vacation_pics/

Use the cp command with caution. Unless you use the -i (interactive) command-line option, the cp command overwrites existing files without prompting you first. When you use the -i option, the cp command prompts for a y or n:

$ cp -i notes.old notes 
cp: overwrite ´notes'? y 

An even better approach is to use the -b (backup) option with the cp command. To create a backup of any files that can be overwritten, try the following:

$ cp -bi notes.old notes 
cp: overwrite ´notes'? y 
$ ls note* 
notes.old notes notes~ 

Notice that each backup file has a tilde (~) appended to its name.

Copying Directories with the cp Command

The -P (parent) command-line option, along with the -R (recursive) command-line option, not only copies files within one directory to another directory, but also any directories inside. For example, each of the following command lines copy the directory current and any files or directories within to the lastweek directory:

$ cp -PR current lastweek 
$ cp -R current lastweek 
$ cp -R current lastweek 

The cp command has nearly 40 command-line options. For details, see the cp manual page.

  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.