Preserve destination timestamp while copying


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Preserve destination timestamp while copying
# 1  
Old 09-07-2017
Preserve destination timestamp while copying

Hi Guys,

I am trying to copy a file from source to destination, I need to preseve timestamp of destination

Code:
cd desitnation
ll file.txt
Aug 17 17:22 file.txt


cp -p source/file.txt destination/

I need to preseve same timestamp after copying
Code:
cd desitnation
ll file.txt
Aug 17 17:22 file.txt

# 2  
Old 09-07-2017
It looks to me that you are doing it right. What is the problem?

Also please give OS version and the shell you are using. Also, just in case this is significant (it shouldn't be), are source and destination on the same file system? Use
Code:
df <path_to_source>
df <path_to_destination>

to find out.

Andrew
# 3  
Old 09-07-2017
Do you mean that there is an existing file that will get overwritten and you want the time of that? Okay, well you will have to record it and then set the value afterwards.

Something like a combination of stat before the copy and touch afterwards should do it.

How about:-
Code:
#!/bin/bash

src_file="/path/to/source/file"             # Define source file to copy
dst_file="/path/to/destin/file"             # Define target file to overwrite and preserve time on

ls -l "${dst_file}"                         # Let's just check it first
file_time=$(stat -c %Y "${dst_file}")       # Get the modification time in seconds since the Epoch

cp "${src_file}" "${dst_file}"              # Copy the file

touch -md "@${file_time}" "${dst_file}"     # Set the modification to the stored value (in seconds)

ls -l "${dst_file}"                         # Let's have a look now

Does that get you near?

Robin
This User Gave Thanks to rbatte1 For This Post:
# 4  
Old 09-07-2017
perfect yes this is what i was expecting

But can it be done use cp command because sometimes permission wont be there to do touch -md command in destination folder

Last edited by rohit_shinez; 09-07-2017 at 07:20 AM.. Reason: question
# 5  
Old 09-07-2017
Code:
cp -p source dest

# 6  
Old 09-07-2017
Hi,

This will preserve source timestamp not the destination time stamp.

problem here is i dont have permission to do touch -r or touch -md as suggested bt rbatte1 since my id is not owner for that directory. I am only part of that unix id group . I can able to copy but not able to preserve the timestamp of old one
# 7  
Old 09-07-2017
Would this help?

Code:
src_file="/path/to/source/file"
dst_file="/path/to/destin/file"

cp "${src_file}" "${src_file}.tmp"
touch -r "${dst_file}"  "${src_file}.tmp"
mv "${src_file}.tmp" "${dst_file}"

Andrew
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Grep lines between last hour timestamp and current timestamp

So basically I have a log file and each line in this log file starts with a timestamp: MON DD HH:MM:SS SEP 15 07:30:01 I need to grep all the lines between last hour timestamp and current timestamp. Then these lines will be moved to a tmp file from which I will grep for particular strings. ... (1 Reply)
Discussion started by: nms
1 Replies

2. Shell Programming and Scripting

AIX : Need to convert UNIX Timestamp to normal timestamp

Hello , I am working on AIX. I have to convert Unix timestamp to normal timestamp. Below is the file. The Unix timestamp will always be preceded by EFFECTIVE_TIME as first field as shown and there could be multiple EFFECTIVE_TIME in the file : 3.txt Contents of... (6 Replies)
Discussion started by: rahul2662
6 Replies

3. Shell Programming and Scripting

How to preserve time stamp while copying a directory from Server B to Server A?

Experts, Please help me out here. How to preserve time stamp while copying a directory from Server B to Server A (3 Replies)
Discussion started by: ahmed.vaghar
3 Replies

4. Shell Programming and Scripting

To check timestamp in logfile and display lines upto 3 hours before current timestamp

Hi Friends, I have the following logfile. Currently time in india is 07/31/2014 12:33:34 and i have the following content in logfile. I want to display only those entries which contain string 'Exception' within last 3 hours. In this case, it would be the last line only I can get the... (12 Replies)
Discussion started by: srkmish
12 Replies

5. UNIX for Dummies Questions & Answers

How to compare a file by its timestamp and store in a different location whenever timestamp changes?

Hi All, I am new to unix programming. I am trying for a requirement and the requirement goes like this..... I have a test folder. Which tracks log files. After certain time, the log file is getting overwritten by another file (randomly as the time interval is not periodic). I need to preserve... (2 Replies)
Discussion started by: mailsara
2 Replies

6. Shell Programming and Scripting

Timestamp copying giving file path and name as inputs

PLAESE CHECK WHETHER THIS SCRIPT WORKS OR NOT. THIS IS MY FIRST SCRIPT, HELP ME out if any ERRORS are present. a=0; while do echo "ENTER path" read path if then echo "path found" cd $path DateTimeStamp=$(date '+%d_%m_%y_%H_%M') echo "enter file name" read file (2 Replies)
Discussion started by: arunreddy01
2 Replies

7. UNIX for Advanced & Expert Users

How to copy a file from remote server and preserve timestamp

Hi How to copy a file from remote server and preserve timestamp. Please not, i have to pass username and password to connect to the remote server in the shell script. Can this be achieved with simple ftp ? are there any options in ftp ? Thanks (4 Replies)
Discussion started by: skumar75
4 Replies

8. UNIX for Advanced & Expert Users

Automated SCP script passing password to preserve source file timestamp

Hi My requirement is i want to copy files from remote server to the local server and also i need to preserve the timestamp of the remote file. By using scp -p , it is working fine in the interactive call but it is not preserving he file timestamp when i use it in the non interactive scp call... (1 Reply)
Discussion started by: skumar75
1 Replies

9. Shell Programming and Scripting

Copying files with increment in destination

Hi.. i have a source and destination directories & want to copy files from source to destination those files are unique to source directory . And those files are common to both the directory copy source to destination with a numeric increment as extension. If in the destination there are more... (2 Replies)
Discussion started by: posix
2 Replies

10. Shell Programming and Scripting

preserve timestamp of symlinks using rsync

Hi, In my shell programming, When i copy sym links from one server to another server using rsync it does not preserve time stamp . it showing current time after copying in destination server.the command i used is rsync -vaR -- How can i preserve sym links time stamp using rsync. Thanx (1 Reply)
Discussion started by: KiranKumarKarre
1 Replies
Login or Register to Ask a Question