LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-15-2021, 04:53 PM   #1
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Rep: Reputation: Disabled
Post Copy Script append timestamp to files preserve modified date


Hi I need to put something together that copies the contents of this directory, and appends the current timestamp to the filename(s) but the modified date in the destination directory should be the same as the original.


If anyone would like to share some pointers it would be greatly appreciated .

Thank you have a great day.
 
Old 06-15-2021, 04:58 PM   #2
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Code:
cp -p sourcefilename sourcefilenamedate
 
Old 06-15-2021, 05:08 PM   #3
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by scasey View Post
Code:
cp -p sourcefilename sourcefilenamedate


Hi scasey, I tried this but the destination file just has the word date appended the end ,I would like to have the have the timestamp of the copy operation, appended in the filename? Thank you,
 
Old 06-15-2021, 05:12 PM   #4
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
That’s pseudocode…you need to replace the string date with the date you want to append.
man date
 
Old 06-15-2021, 06:32 PM   #5
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Original Poster
Rep: Reputation: Disabled
I did this,


cp -p fullpathandnameofsourcefile fullpathandnameofdestinationfile-`date +%Y%m%d%H%M%S`


Do you approve?


Thank you,
 
Old 06-15-2021, 08:08 PM   #6
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Quote:
Originally Posted by solidbulk View Post
I did this,


cp -p fullpathandnameofsourcefile fullpathandnameofdestinationfile-`date +%Y%m%d%H%M%S`


Do you approve?


Thank you,
It’s not up to me to approve. Does it give you what you want?
 
Old 06-16-2021, 08:16 AM   #7
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546

The "`...`" is the older form of command substitution - you may prefer to use "$(...)" instead. (It'll work the same way here, but can make things more readable with more complex commands.)

If "fullpathandnameofsourcefile" is actually the same as "fullpathandnameofdestinationfile", consider brace expansion as a way to avoid writing the same thing twice:
Code:
cp -p "filename"{,-$(date +%Y%m%d%H%M%S)}
 
1 members found this post helpful.
Old 06-16-2021, 10:26 AM   #8
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
Quote:
but the modified date in the destination directory should be the same as the original.
As opposed to cp, mv will retain the original date and time.

Something perhaps like
mv * newDir
for i in newDir/*
cp $i $i-`date +%Y%m%d%H%M%S`
 
Old 06-16-2021, 11:00 AM   #9
boughtonp
Senior Member
 
Registered: Feb 2007
Location: UK
Distribution: Debian
Posts: 3,597

Rep: Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546Reputation: 2546
Quote:
Originally Posted by AnanthaP View Post
As opposed to cp, mv will retain the original date and time.
That's probably because mv moves the original file; it does not create a copy nor leave the original intact.

Every (previous) reference to cp in this thread includes the -p (preserve) parameter.
See: man cp | less --pattern '--preserve'


(Also, filename variables should be quoted!)

 
Old 06-16-2021, 12:33 PM   #10
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by boughtonp View Post
The "`...`" is the older form of command substitution - you may prefer to use "$(...)" instead. (It'll work the same way here, but can make things more readable with more complex commands.)

If "fullpathandnameofsourcefile" is actually the same as "fullpathandnameofdestinationfile", consider brace expansion as a way to avoid writing the same thing twice:
Code:
cp -p "filename"{,-$(date +%Y%m%d%H%M%S)}

I appreciate your responses folks. The destination where the copies will be stored is not the same as the source location.

Thank you
 
Old 06-16-2021, 12:54 PM   #11
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Original Poster
Rep: Reputation: Disabled
Can these commands be added to a .sh file and added to the crontab. I seem to be unable to do that.
 
Old 06-16-2021, 01:35 PM   #12
solidbulk
LQ Newbie
 
Registered: Sep 2020
Posts: 29

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by solidbulk View Post
Can these commands be added to a .sh file and added to the crontab. I seem to be unable to do that.


Disregard that, I was able to add these to a .sh file and schedule with crontab.

Thanks!
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Recursively Append File's Modified Date to Filename ericlindellnyc Linux - Newbie 2 08-29-2018 09:14 PM
Append modified-date to filename before extension -- ONLY IF THERE'S AN EXTENSION EVL Linux - Newbie 6 02-04-2017 01:50 PM
script to timestamp files with timestamp from directory eRJe Programming 4 11-13-2013 06:52 PM
Copying a set of files through CIFS from Linux to Windows 7 to preserve the timestamp Rupadhya Linux - Software 2 08-28-2013 08:33 PM
Help - My Samba share changes the timestamp when copying files to it.. Preserve Time. hheejj Linux - Software 2 06-28-2005 09:34 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:32 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration