|
|
|
| |||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
Unix timestamps
I haven't done a whole lot with scripting. I need to get a timestamp from unix files displayed in strictly number format. ie - YYMMDDhhmmss with nothing else shown but the time stamp (not even the filename). Anybody know who to do this?
Actually I really just want to compare files to see which is the oldest. Any ideas? |
|
#2
|
|||
|
|||
|
You cannot get seconds in a timestamp using just shell commands You have to resort to some facility that can access stat() - perl, python C.
Here is a perl version. Just pass it one file name at a time. [ Code:
#!/bin/perl
#^###################################################################
#^ $Header: inodetime.pl,v 1.1 97/01/20 10:17:00 http srvr? $
#^###################################################################
#^ PROGRAM DESCRIPTION
#^ -------------------
#^ This program prints the modification times and names of files.
#^ It uses the following format: inodetime.pl filename
#^ It will accept: inodetime.pl filename1 filename2 filename3
#^ inodetime.pl /tmp/file*
#^ The format of the output is: YYYYMMDDhhmmss (changed for this post) jmc
#^ example:
#^ $ inodetime.pl /tmp/t*
#^ 19961115105425
#^ 19970116113616
#^
#^ TRICK To Remember The Program Name
#^ ----------------------------------
#^ inodetime == I No De Time
#^ ^^ ^ ^
#^ KSG
#^###################################################################
############################################
# Get the (next) input from the command line
############################################
while ($curfile = $ARGV[0])
{
#################################################
# Do following code block only if $curfile exists
#################################################
if (-e $curfile)
{
###############################
# stat structure into variables
###############################
($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,
$atime,$mtime,$ctime,$blksize,$blocks)
= stat("$curfile");
###############################
# time structure into variables
###############################
local($sec,$min,$hr,$day,$mon,$yr,$wday,@dntcare) = localtime($mtime);
$yr = ($yr>=70) ? $yr+1900 : $yr+2000;
$yr="$yr";
$mon = (++$mon < 10) ? "0$mon" : "$mon";
$day = ($day < 10) ? "0$day" : "$day";
$hr = ($hr < 10) ? "0$hr" : "$hr";
$min = ($min < 10) ? "0$min" : "$min";
$sec = ($sec < 10) ? "0$sec" : "$sec";
##################################################################
# Rearrange in the YYYYMMDDhhmmss format and assign to $dte variable
##################################################################
$dte = join('',$yr,$mon,$day,$hr,$min,$sec);
######################################
# Print modification date and filename
######################################
print ("$dte \n");
}
########################################
# Shift to next position in command line
########################################
shift (@ARGV);
}
|
|
#3
|
|||
|
|||
|
Wow, thank you! Can't wait to try it out! Do you know how to do it with shell commands if you don't include seconds?
Thanks for your help. I'll let you know how it works. |
|
#4
|
|||
|
|||
|
Code:
ll <filename> gives you time information about the last access to file. |
|
#5
|
|||
|
|||
|
Code:
ls -lrt <filenames> will sort files by time, oldest first, newsest last. |
|
#6
|
|||
|
|||
|
If you have two files, say, filea and fileb, and you want the older file, do:
ls -tr filea fileb | sed 1q |
|
#7
|
|||
|
|||
|
Thank you very much everyone for your info, it's proven extremely helpful. I think I have what I need to know now. Thanks again.
|
![]() |
| Viewing: Dev Shed Forums > Operating Systems > UNIX Help > Unix timestamps |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|