How to Copy File without Changing Last Modified Date, Time stamp and ownership in Linux / Unix?

advertisements

_____________________________________________________________________________________________________________________

The Eucharistic Miracles of the World

cp command provides an option –p for copying the file without changing the mode, ownership and timestamps.

--preserve option gives you the provision to choose the options from
ownership, mode and timestamp.

Note: suppose if you are trying to copy the files from one user using a different user other than root, the mode, ownership will change and will create files with same timestamp. You have to try with root user to copy the files to different user to keep the same timestamp, mode and ownership(all attributes)


-p     same as --preserve=mode,ownership,timestamps

--preserve[=ATTR_LIST]
              preserve  the  specified  attributes  (default:  mode,ownership,timestamps),  if possible additional attributes: context, links, xattr, all

Example 1: Copy Under same Unix user
$ id
uid=1001(oracle) gid=1001(oinstall)
$ cp num.txt num_bkup.txt  -- This command will copy with new timestamp
$ cp -p num.txt num_bkup_1.txt –- Retains the permission and timestamp.
$ ls -ltr num*
-rw-r--r-- 1 oracle oinstall 68 Nov  6 04:47 num.txt
-rw-r--r-- 1 oracle oinstall 68 Nov  6 04:47 num_bkup_1.txt
-rw-r--r-- 1 oracle oinstall 68 Nov 20 06:07 num_bkup.txt

Example 2: Copy using different user oradba
In this example we are using different user other than root which will not keep the same privileges, ownership. It preservs only same time stamp.
$ id
uid=1120(oradba) gid=1001(oinstall)
$ cd /home/oradba/test
$ cp -p /home/oracle/test/num.txt ./num.txt –- preserve only timestamp
$ ls -ltr
total 20
-rw-r--r-- 1 oradba oinstall 68 Nov  6 04:47 num.txt

Even though if you try with ownership attribute, it will change the ownership to oradba.

$ cp --preserve=ownership /home/oracle/test/num.txt ./num_bkup_1.txt
$ ls –ltr num_bkup_1.txt
total 8
-rw-r--r-- 1 oradba oinstall 68 Nov 21 02:25 num_bkup_1.txt

Example 3: Copy using root user

# id
uid=0(root) gid=0(root)
# cd /home/oradba/test
# ls -ltr
total 8
-rw-r--r-- 1 oradba oinstall 68 Nov 21 02:23 num.txt
-rw-r--r-- 1 oradba oinstall 68 Nov 21 02:25 num_bkup_1.txt
# cp -p /home/oracle/test/num.txt ./num_bkup_root.txt –-Preserves all attributes.
# ls –ltr ./num_bkup_root.txt
total 12
-rw-r--r-- 1 oracle oinstall 68 Nov  6 04:47 num_bkup_root.txt

# cp --preserve=timestamp /home/oracle/test/num.txt ./num_bkup_root_timestamp.txt
# ls –ltr num_bkup_root_timestamp.txt
total 16
-rw-r--r-- 1 root   root     68 Nov  6 04:47 num_bkup_root_timestamp.txt

_____________________________________________________________________________________________________________________

Website Stats

0 comments:

Post a Comment

Labels

Oracle (629) Script (86) General (77) Unix (47) Blog (23) Technology (19) gadget (6) games (6) Business (3) OCI (3) SQL* Loader (3) Datapump (2)
 

acehints.com Copyright 2011-23 All Rights Reserved | Site Map | Contact | Disclaimer