Managing files in Linux: the cp command

M

One of the most used commands in Linux is cp (abbreviated from copy). The cp command copies a file. Its syntax is:

cp [options] destination source

The source parameter can be one or more files. It represents the original file or files we want to copy. The destination option may be a single file (when we copy a single file) or even a directory when copying multiple files.

Copy a single file to the same folder:
$ ls
mvps1.txt new-mvps.txt mvps.txt

$ cp mvps1.txt new_mvps1.txt

$ ls
new_mvps1.txt mvps1.txt new_mvps.txt mvps.txt

 

Copy to other directory

When we copy to other directory, the cp command keeps the original file name. Or we can give him another name under the destination option. In the example below I copied the mvps1.txt into the previously created temp directory (with the same name), then I copied the mvps1.txt into the temp directory with the name mvps2.txt; In this way, the temp directory will contain 2 files – mvps1.txt and mvps2.txt:

$ cp mvps1.txt temp/

$ cp mvps1.txt temp/mvps2.txt

$ ls temp/
mvps1.txt mvps2.txt

 

In the example above we used the slash (/) when I wrote the name of the destination directory. I did this to prevent problems caused by typos. For example, if because of the keyboard or in a hurry I wrote cp mvps1.txt tem (without p at the end), the mvps1.txt file will be copied to the same directory as tem. Adding the slash explicitly states that we intend to copy a file to a directory. If the directory does not exist, the cp command will answer this:

$ cp mvps1.txt tempdir/

cp: Can not create regular file ‘tempdir/’: Not a directory

Copy a directory

We can copy an entire directory so the source and destination are given by the names of the two directories:

cp -R directory_name new_directory_name

The cp command has many options, but very often are used just a few.

Recursive copying

In the last example above we used the -R option, which tells the cp command to copy an entire directory with its contents.

Force overwriting

The -f or –force option forces the system to overwrite any file without query.

Interactive mode

The -i or –interactive option tells to the cp command to ask before overwriting any file.

Keeping the root and the permissions

Normally, the user who initiates the cp command also owns the files that they copy and uses the default account permissions. The -p or –preserve option maintains the copy rights and permissions of copied files as long as possible – for example, if we copy root files to another user, the new resulting files will belong to the same user and will have the same permissions.

Run a copy of the update

The -u or –update option tells to the cp command to only copy files that are newer than the target or do not exist in the target directory.

These are the most used options for the cp command; the list of options supported by this command is incomplete – see the man cp page for more information.

About the author

Ilias spiros
By Ilias spiros

Recent Posts

Archives

Categories