Académique Documents
Professionnel Documents
Culture Documents
directory
http://www.basicconfig.com/linux/how_to_copy_files_directory_linux
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.
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:
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:
Let's see how to use the cp options above with some examples.
Here are examples and explanations on how to copy files and directories in Linux
system.
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:
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:
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:
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.
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:
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:
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:
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:
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$
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.