CentOS Linux Server Guide

Copy or duplicate file in CentOS Linux

Copy command for Linux  is "cp".

Unless otherwise specified by option, copy command will create new timestamp when the copy was done. 
 

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 DESTINATION, or multiple SOURCE(s) to DIRECTORY.

 
Command and options Description

 

cp existingfilename newfilename

 

 

 

 

 

 

we are copying A.txt to new file called B.txt. The system will create newfile called B.txt
# cat A.txt
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=srv1.example.local
 

# cp A.txt B.txt
 

# ls *.txt
A.txt B.txt
 

# cat B.txt
NETWORKING=yes
NETWORKING_IPV6=no
HOSTNAME=srv1.example.local
 

cp -r foldername newfolder

 

 

copy folder and all the files (recursively) to new folder.

# cp -r myDir/ newmyDir/

# ls -l * d/
ls: d/: No such file or directory
-rw-r--r-- 1 root root 62 Nov 28 15:36 A.txt
-rw-r--r-- 1 root root 62 Nov 28 15:36 B.txt
-rw-r--r-- 1 root root 16 Nov 28 11:17 My Work
-rw-r--r-- 1 root root 13 Nov 28 11:19 My Work Linux
-rw-r--r-- 1 root root 14 Nov 28 11:21 My Work of Centos
-rw-r--r-- 1 root root 67 Nov 28 15:04 network

myDir:
total 8
-rw-r--r-- 1 root root 14 Nov 28 13:15 a.txt

newmyDir:
total 16
-rw-r--r-- 1 root root 14 Nov 28 15:42 a.txt

 

 

cp -p , --preserve

copy file or folder to new location while preserving its file attribute. Take note that we are copying from /etc/httpd/conf.d/README to our current folder "." and we preserve the timestamp, user rights.
# ls -l /etc/httpd/conf.d/README
-rw-r--r-- 1 root root 392 Jan 21 2009 /etc/httpd/conf.d/README
 

# cp -p /etc/httpd/conf.d/README .
 

# ls -l README
-rw-r--r-- 1 root root 392 Jan 21 2009 README

 

 

Back to Managing Files and Folders in CentOS Linux



Copyright 2011 http://www.1a-centosserver.com All Rights Reserved

All trademarks are the property of their respective owners.

Contact Us | Terms of Use | Privacy Policy