LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 09-17-2010, 01:41 PM   #1
msmith88
LQ Newbie
 
Registered: Sep 2010
Posts: 1

Rep: Reputation: 0
Adding a files creation date to it's file name?


Hi all,

New to the forums here and fairly green when it comes to Linux. I have some basic experiencing creating simple scripts/making directories/changing permissions/etc. but I'm stumped on this one.

I have two linux boxes. I have a script set up on box 'A' to SCP into box 'B', grab a copy of a database backup and store it on box 'A'. It looks like this:

scp root@X.X.X.X:/blah/blah/blah/dump.23.gz /home/blah/DB_Backups/

I have generated a public key on box 'A' and placed it into the authorized_keys file on box 'B', so a password is not required and the file copies over successfully when the script is run. On to my problem...

I need to know what date the 'dump.23.gz' file was originally created when I'm viewing it after it's been copied to box 'A'. If I ls -l on box 'A' it only shows me the date it was created on box 'A' when it was copied.

What would I need to add to my script to append the backup's original creation date on box 'B' to the filename so that when it gets copied to box 'A' I know when the backup was created on box 'B'. I'm sure this is probably confusing. I've done lots of searching and can only find information on how to append the current date and time to a file name. I need to append it's original creation timestamp to the filename when it copies over. Any help is appreciated.

Thanks,
Matt
 
Old 09-17-2010, 02:03 PM   #2
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Would scp's -p option help?

Quote:
-p Preserves modification times, access times, and modes from the original file.
scp -p root@X.X.X.X:/blah/blah/blah/dump.23.gz /home/blah/DB_Backups/

Hope this helps.

Last edited by druuna; 09-17-2010 at 03:07 PM. Reason: fixed typo
 
Old 09-17-2010, 02:13 PM   #3
gdejonge
Member
 
Registered: Aug 2010
Location: Netherlands
Distribution: Kubuntu, Debian, Suse, Slackware
Posts: 317

Rep: Reputation: 73
Take a look at the date command. It has the possibillity to format its output.
Example:
Code:
$ date +%a-%d-%b
Fri-17-Sep
Combine this with your copy command
Code:
scp root@X.X.X.X:/blah/blah/blah/dump.23.gz /home/blah/DB_Backups/`date +%a-%d-%b`-dump.23.gz
Notice the backquotes around the date command. The will make sure that the date command will be executed and the output from it will be used as text in the scp command.

____________________
Gerrard
 
Old 09-17-2010, 02:34 PM   #4
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
@gdejonge: That will add the current date to the file, not the original date of the scp'ed file.
 
Old 09-17-2010, 11:44 PM   #5
sem007
Member
 
Registered: Nov 2006
Distribution: RHEL, CentOS, Debian Lenny, Ubuntu
Posts: 638

Rep: Reputation: 113Reputation: 113
Quote:
Originally Posted by msmith88 View Post
I need to append it's original creation timestamp to the filename when it copies over.
As i under stand your question you want original file creation time stamp.

just say example you create one file on 1st September and this file modify many time after creation. now you want to creation time (1st September) am i right ?

As i know linux filesystem do not store file creation date and time.

BTW you can find last access modify time by stat command.
Code:
[root@localhost ~]# stat test
File: `test'
  Size: 9703            Blocks: 24         IO Block: 4096   regular file
Device: 801h/2049d      Inode: 1558337     Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2010-09-18 10:21:01.000000000 +0530
Modify: 2010-09-18 10:21:15.000000000 +0530
Change: 2010-09-18 10:21:15.000000000 +0530
or you can preserve time -p option as suggest by druuna.

HTH

Regards,

Last edited by sem007; 09-17-2010 at 11:46 PM. Reason: add command
 
Old 09-18-2010, 12:03 AM   #6
prayag_pjs
Senior Member
 
Registered: Feb 2008
Location: Pune - India
Distribution: RHEL/Ubuntu/Debian/Fedora/Centos/K3OS
Posts: 1,159
Blog Entries: 4

Rep: Reputation: 149Reputation: 149
scp -p
 
Old 09-18-2010, 06:51 AM   #7
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Posts: 952

Rep: Reputation: 217Reputation: 217Reputation: 217
ls -l <filename> | awk '{get the date column'} | mv filename filename_output_of_awk.

What did you try so far?
 
0 members found this post helpful.
Old 09-18-2010, 06:56 AM   #8
druuna
LQ Veteran
 
Registered: Sep 2003
Posts: 10,532
Blog Entries: 7

Rep: Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405Reputation: 2405
Hi,

Did you read the question?

Quote:
Originally Posted by AnanthaP View Post
ls -l <filename> | awk '{get the date column'} | mv filename filename_output_of_awk.
How would you implement this when the file is on box A, you copy it to box B (time-stamp is now current time on box B, not creation time as mentioned on box A)???

As mentioned by me and other later on: use scp -p ........ Problem solved.
 
Old 09-19-2010, 12:07 AM   #9
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
As said by sem007
Quote:
linux filesystem do not store file creation date and time.
You need to have the original creation of the dump file include the date/time as part of the name...
 
Old 01-20-2011, 11:23 AM   #10
reg360
LQ Newbie
 
Registered: Jan 2011
Distribution: jaunty
Posts: 2

Rep: Reputation: 0
I submitted a script at this other thread because I had the same task as the original poster there:
http://www.linuxquestions.org/questi...lename-800333/

As already suggested on this thread, preserve the original dump file date with "scp -p". Then maybe use "stat" or something else to get file metadata. Then script something like I did to add that info back into your filename on box "A" (or on whatever box/filename you want the timestamp to be stored)
 
Old 01-20-2011, 08:16 PM   #11
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Rocky 9.2
Posts: 18,360

Rep: Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751Reputation: 2751
The problem with that is if you want/need to copy or access that file again, or even the original on box B via another cmd etc.
It really is best to amend the actual backup script to insert the date (& optionally time) in the backup filename. Then you never need to worry.
Think about what would happen if you back it up to external storage eg tape, DVD. You'd always have to remember (& be able) to find a cmd that does the -p feature. This may not always be the case...
 
Old 09-28-2018, 03:48 PM   #12
ncollette
LQ Newbie
 
Registered: Sep 2018
Posts: 1

Rep: Reputation: Disabled
Here's what I came up with. Might be useful to others

I used the following with success:

Code:
# Rename all the files with timestamps.
for filename in ./*; do
    NEW_NAME=`stat $filename | grep -E '^(  File:|Modify:)' | awk '{print $2$3}' | sed 's/[^0-9a-zA-Z_\.]//g' | awk -F. '{printf "%s:%s:", $1,$2}' | awk -F: '{printf "%s_%s.%s", $1, $3, $2}'`
    sudo mv $filename $NEW_NAME
done

Last edited by ncollette; 09-28-2018 at 03:50 PM. Reason: was interpretting the code as smiley faces
 
  


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
File creation date of vfat / fat32 files on linux snail Linux - Newbie 3 06-03-2009 05:56 PM
Listing of files on creation date ZAMO Linux - General 1 01-25-2008 06:45 AM
file creation date prashantbhushan Linux - General 2 05-29-2007 08:32 AM
Finding out the creation date for files/folders... darkarcon2015 Linux - Software 1 03-24-2005 05:59 PM
ls -al file creation date blackzone Linux - Newbie 2 08-23-2004 01:31 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 02:20 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