Copy Linux Files and Copy Directory Using cp Command - Linux Basic Command.
Using Linux cp command to copy Linux files and copy directory .
The Linux cp command usually use to copy file and copy directory in Linux operating system. The cp command also can be use to make an link to other file in the system. The execution of cp command and all of the example below tested using bash shell on Linux Fedora Core operating system.
This article demonstrate step by step procedure on how to use Linux cp command to:
• Copy Linux files.
• Copy all file in directory.
• Make an soft link to a file.
Command name: cp
Command Description:
Copy files and directory.
Command type:
On Fedora Core Linux operating system 'cp' command is aliased to the 'cp -i'
[root@fedora ~]# type cp
cp is aliased to `cp -i'
Copy the content of Linux files using Linux cp command.
The cp command below copy the passwd file from the /etc folder to current directory using the same filename.
[root@fedora ~]# pwd
/root
[root@fedora ~]# cp /etc/passwd .
[root@fedora ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog mbox passwd test
[root@fedora ~]#
The cp command also can be use to copy the contents of file into another files. The example below show the steps to copy file name passwd from the /etc directory and put the file into current directory using new filename.
[root@fedora ~]# pwd
/root
[root@fedora ~]# cp /etc/passwd new-pass
[root@fedora ~]# ls
anaconda-ks.cfg Desktop install.log install.log.syslog mbox new-pass test
[root@fedora ~]#
To copy a file and preserve the file mode, ownership and timestamps, use cp with -p option.
The example below show the use of -p option:
[root@fedora5 ~]# cp -p /etc/passwd .
[root@fedora5 ~]# ls -l
total 112
-rw------- 1 root root 1209 Apr 9 21:38 anaconda-ks.cfg
drwx------ 2 root root 4096 Apr 8 19:06 Desktop
-rw-r--r-- 1 root root 28948 Apr 9 21:26 install-fedora.log
-rw-r--r-- 1 root root 4152 Apr 9 19:56 install.log.syslog
-rw-r--r-- 1 root root 1750 Apr 8 19:00 passwd
[root@fedora5 ~]# ls -l /etc/passwd
-rw-r--r-- 1 root root 1750 Apr 8 19:00 /etc/passwd
[root@fedora5 ~]#
Note: The ls -l command use in above example is to verify the passwd file is copy in preserve mode.
Copy file in verbose mode.
The example below show the step of using cp command in verbose mode to copy file on Fedora system. By using the cp command with verbose mode we can display the details of the running cp command.
[root@crooked test]# cp -v test.txt linux.txt
`test.txt' -> `linux.txt'
[root@crooked test]# ls -l
total 8
-rw-r--r-- 1 root root 28 Jun 29 17:05 linux.txt
-rw-r--r-- 1 root root 28 Jun 29 16:50 test.txt
The cp command with –v option produce the verbose output shows that test.txt file is copy to linux.txt file.
Using cp command to create identical copy of directory (archive).
To copy directory usually the cp command use with the -a option to make the sure the cp command archive the directory contents.
[root@crooked test]# cp –a directory new_directory
The cp command with –a option create an identical copy of directory called directory and named the directory to new_directory.
Using cp command to create an soft link to a file.
This example show how to create soft link using cp command, in this example the cp command is use to make a link to a linux.txt file.
[root@crooked test]# cp -vs linux.txt linux-link.txt
`test.txt' -> `link.txt'
[root@crooked test]# ls -l
total 8
lrwxrwxrwx 1 root root 8 Jun 29 17:06 linux-link.txt -> linux.txt
-rw-r--r-- 1 root root 28 Jun 29 16:50 test.txt
The cp command with –vs option create a link (name linux-link.txt) to the linux.txt file in verbose mode.
Using cp command to copy all files and folder.
[root@fedora test]# cd ..
[root@fedora ~]# cp -pr /home/fedoracore5/ test/fedoracore5
[root@fedora ~]# ls test/fedoracore5/
Desktop
[root@fedora ~]# ls /home/fedoracore5/
Desktop
[root@fedora ~]# ls -al /home/fedoracore5/
Cp command with option 'p' is to preserve the mode, ownership and timestamps of the file an the option 'r' is use to copy directory recursively.
The following are some of the flags and arguments that can be used for the cp command:
• -f, --force if an existing destination file cannot be opened, remove it and try again
• -i, --interactive prompt before overwrite
• -l, --link link files instead of copying.
• -p same as --preserve=mode,ownership,timestamps
• -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
NAME
cp - copy files and directories
for more information Linux cp command use:
# info cp
# man cp
# cp –help
Related:
Copy contents of directory include hidden files and hidden directory using cp command.
Keywords: cp command, copy linux file, copy file, copy folder, copy directory, copy linux directory, linux cp command, linux copy command, link files, copy command, copy file and directory, copy content of linux files, copy linux folder.
- 166528 reads
- Email this page
7 comments
Very nice article on how to
Submitted by Balu (not verified) on Sat, 08/27/2011 - 18:45.Very nice article on how to copy files in linux, The content is in depth
I was expecting to work a
Submitted by Donnald (not verified) on Fri, 11/04/2011 - 13:10.I was expecting to work a little bit more with file extensions instead of setting up commands. I know Linux is mostly based on commands but I think it's time for this operating system to become more accessible for wider use.
Install Windows 7 using
Submitted by wasteoftime (not verified) on Fri, 11/11/2011 - 21:49.Install Windows 7 using RootKit virus
Open desired folder
Find file needed to be copied.
Copy file in new folder.
Done.
I love using other operating systems to do tasks. Just like I use my windows to get in and out of my house, instead of using my front door.
I was also thinking of purchasing a vehicle for 70,000$ that I am unable to drive on 95% of the roads. Oh but wait! It cannot be broken into! That will come in handy when I drive it everywhere... Oh wait.. I can't. I'm not allowed to drive on 95% of the roads unless I modify my 70,000$ car.
Most of you will never understand these analogies
Calculate the file copying time
Submitted by Arun (not verified) on Wed, 12/07/2011 - 10:23.Hi,
I would like to copy one file from my computer to another. Both are linux and I need the log file showing the time taken to copy the file. Please help me asap.
Thanks,
Very nice tutor.. Thanks..
Submitted by Fashion, Pakaian (not verified) on Tue, 01/03/2012 - 08:44.Very nice tutor.. Thanks..
The real basic command
Submitted by Kemeja Kerja Wanita (not verified) on Fri, 10/05/2012 - 03:04.This is the real basic command, i have use this command, it works ! Thanks.
Thanks for your great post, i love it
Submitted by Pelatihan Motivasi (not verified) on Fri, 10/05/2012 - 03:05.I just looking for a basic tutorial for linux, i found your site, and after try your tutorial, now i get some basic knowledge, thanks.