Copy Linux Files and Copy Directory Using cp Command - Linux Basic Command.

Linux Windows Computer Technology Install Setup Network Configuration Troubleshoot Unix

 

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.

 


Custom Search