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

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  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
DBA Tips Data Pump Reference


Post a Comment


dba topics. Copyright 2011-16 All Rights Reserved | Site Map | Contact | Disclaimer | Google