Vous êtes sur la page 1sur 5

Linux cp command - Copy file and

directory
http://www.basicconfig.com/linux/how_to_copy_files_directory_linux

Submitted by jinlusuh on Wed, 02/25/2009 - 06:52

Linux 'cp' command is used to copy files and directories in Linux command line
terminal. To copy file means to duplicate a file or make an extra copy of a file. The cp
command is quite important and used frequently when we work in the command line
terminal. Normally, we use the cp command to backup a file by making extra copy of
the original file. A situation example is when we are going to edit an important file such
as a server configuration file. We can either copy one file to a given destination, or copy
many files to a destination directory. The cp command also can be used to copy
directory in Linux. Let's see how to use the Linux cp command.

Linux cp command syntax

A command syntax is a synopsis, the format on how to issue the command to do


specific task. We can check Linux cp command syntax using the 'man cp' command.
Here is a part of the cp command's manual page:

NAME
cp - copy files and directories

SYNOPSIS
cp [OPTION]... [-T] SOURCE DEST
cp [OPTION]... SOURCE... DIRECTORY
cp [OPTION]... -t DIRECTORY SOURCE...

DESCRIPTION
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

If you are really a Linux beginner and cannot understand the synopsis in manual page
above, below are the Linux cp command syntax which we can use to copy file or
directory in Linux system:

Linux cp command syntax to copy a single file or directory:


cp [option] SOURCE DESTINATION

Here are the syntax explanations:

[option] - options available that we can used to customize cp command.


SOURCE - the original file (or directory). Provide a full directory path if you are running
cp command from other directory.
DESTINATION - the new target or directory to put the duplicate file (or directory).

1
Linux cp command syntax to copy multiple files:
cp [option] [SOURCE1 SOURCE2 SOURCE3 ...] DESTINATION

The [SOURCE1 SOURCE2 SOURCE3 ...] is multiple files that are going to be
copied. If sources are from different directories, you have to provide a complete path for
each source.

Here are important Linux cp command's options that regularly used to copy files in
Linux system:

-f, --force if an existing destination file cannot be


opened, remove it and try again
-i, --interactive prompt before overwrite
-R, -r, --recursive copy directories recursively
-s, --symbolic-link make symbolic links instead of copying
-u, --update copy only when the SOURCE file is newer
than the destination file or when the
destination file is missing
-v, --verbose explain what is being done
--help display this help and exit

Let's see how to use the cp options above with some examples.

Linux cp command examples

Here are examples and explanations on how to copy files and directories in Linux
system.

Linux copy file examples

The Linux cp command's option is not mandatory if we want to copy files only. The cp
command can be used without any option. See the Linux copy file example below:

luzar@slackware:~/etc$ cp BUKUMERAH-BM.pdf ../examples/

In the example above, BUKUMERAH-BM.pdf is the source file that we want to copy.
The '../example' is the destination directory. If you wonder why there is a '..' before the
/example, the explanation is, the '..' is a 'path' for the destination directory. The '..' means
the /example directory is one directory above the current directory we are working now.
We can also write the full path like '/home/luzar/examples/'. You can check Linux file
system structure overview for more information about Linux directory.

Our next example is how to use Linux cp command to copy multiple files to a target
directory. Here is the example:

luzar@slackware:~/etc$ cp -i UserManual.pdf BUKUMERAH-BM.pdf


/home/luzar/examples/

2
cp: overwrite `/home/luzar/examples/BUKUMERAH-BM.pdf'? n

When we use '-i' option with the cp command, it will prompt us if the file we are
copying already exist in the destination directory, so we can confirm whether to
overwrite it or not. You can answer with y (yes) or n (No). This is perfect if we are
going to copy multiple files in a regular update directory. Also notice that this time we
used a full path for the destination directory.

We can also force Linux to copy file using the 'cp -f' command. Here is the example:

luzar@slackware:~$ cp -f examples/new-file.txt examples/awareness/

Another example of Linux cp command's option usage is the 'cp -s', which makes a
symbolic links instead of copying the real file. The command syntax is different here.
When using cp with -s option, we mention the target file first followed by the name of
the symbolic link. Here is the example:

luzar@slackware:~$ cd examples/
luzar@slackware:~/examples$ ls
awareness errors.txt linux-manual.txt new-file.txt
luzar@slackware:~/examples$ rm new-file.txt
luzar@slackware:~/examples$ cp -s awareness/new-file.txt new-file.txt
luzar@slackware:~/examples$ ls -l
total 40
drwxr-xr-x 2 luzar users 4096 2009-11-03 20:55 awareness
-rw-r--r-- 1 luzar users 53 2009-07-13 14:11 errors.txt
-rw-r--r-- 1 luzar users 30804 2009-07-13 13:22 linux-manual.txt
lrwxrwxrwx 1 luzar users 22 2009-11-03 21:10 new-file.txt ->
awareness/new-file.txt

Here is the explanation. First I change directory to the example directory and delete a
file named 'new-file.txt, the file that I forcefully copied in the Linux copy file example
earlier. Then I use the 'cp -s' command to create a symbolic link in the current directory
pointing to the original file in awareness directory.

Linux copy directory examples

We can also use cp command to copy directory. The '-R' or 'r' option is needed to
recursively copy the sub-directory and files in that directory. See the Linux copy
directory example below:

luzar@slackware:~$ cp -r slackware examples/


luzar@slackware:~$ ls -l examples/
total 3940
-rw-r--r-- 1 luzar users 113556 2009-02-25 23:13 BUKUMERAH-BM.pdf
-rw-r--r-- 1 luzar users 3903990 2009-02-25 23:29 UserManual.pdf
drwxr-xr-x 2 luzar users 4096 2009-02-25 23:39 slackware/

3
luzar@slackware:~$

In the example above, we copy a directory named slackware to another directory named
examples.

We can verbosely copy directory to see what files and directories have been copied.
Invoke cp with -v from the command prompt will print details about what happens. See
Linux cp example below:

luzar@slackware:~$ cp -rv awareness/ examples/


`awareness/' -> `examples/awareness'
`awareness/iklan.png' -> `examples/awareness/iklan.png'
`awareness/syarat-diploma.png' -> `examples/awareness/syarat-
diploma.png'
`awareness/permohonan01.png' -> `examples/awareness/permohonan01.png'
`awareness/bpkkedumy.png' -> `examples/awareness/bpkkedumy.png'
`awareness/syarat_permohonan.png' ->
`examples/awareness/syarat_permohonan.png'
`awareness/cara_memohon.png' -> `examples/awareness/cara_memohon.png'
`awareness/info_sijil.png' -> `examples/awareness/info_sijil.png'
`awareness/eborang.png' -> `examples/awareness/eborang.png'
luzar@slackware:~$

This is an example of the Linux cp command with '-u' option. The cp -u copy only when
the SOURCE file is newer than the destination file or when the destination file is
missing:

luzar@slackware:~$ touch awareness/newfile.txt


luzar@slackware:~$ cp -rvu awareness/ examples/
`awareness/newfile.txt' -> `examples/awareness/newfile.txt'
luzar@slackware:~$

Here is the explanation of the example above. To show how 'cp -u' command works, we
create a new file in the awareness directory (touch awareness/newfile.txt). When we
invoke cp -rvu awareness/ examples/, only the 'newfile.txt' which is the new file were
copied. Don't worry about duplicate file or directory. This is really useful when backing
up a regularly updated directory.

Here is another example on how to use Linux copy command to copy file and directory
in Linux command line terminal. Example below shows how to copy file to the current
directory:

First, here is an example content of a directory named 'awareness':

luzar@slackware:~$ cd awareness/
luzar@slackware:~/awareness$ ls ../Desktop/kursus/
configuration.php images search-engine-optimization-starter-guide.pdf
ebooks notes templates

4
extensions releases training-workflow.odt
luzar@slackware:~/awareness$

If I want to copy 'configuration.php' file to the current directory, the copy command
would be like the example below:

luzar@slackware:~/awareness$ cp ../Desktop/kursus/configuration.php .
luzar@slackware:~/awareness$ ls
bpkkedumy.png eborang.png newfile.txt syarat_permohonan.png
cara_memohon.png iklan.png permohonan01.png
configuration.php info_sijil.png syarat-diploma.png
luzar@slackware:~/awareness$

The symbol '.' represents current directory.

To copy the whole directory to the current directory, we can use the same method but
with -r option added. See the example below:

luzar@slackware:~/awareness$ cp -r ../Desktop/kursus/ .
luzar@slackware:~/awareness$ ls
bpkkedumy.png eborang.png kursus syarat-diploma.png
cara_memohon.png iklan.png newfile.txt syarat_permohonan.png
configuration.php info_sijil.png permohonan01.png
luzar@slackware:~/awareness$

That's all examples of Linux copy file and Linux copy directory using cp command. The
important thing to remember about Linux cp command is, to copy directory, you have
to provide the -r or -R option(switch). It's optional to use an option(or arguments) with
cp command to copy file depends on your need.

The different between Linux cp command and Linux mv command is, the cp command
duplicate the source file and move it to a new location while the mv command move the
source (original) file. So the Linux cp command is the perfect choice to backup files and
directories in Linux system.

Vous aimerez peut-être aussi