Get timestamp by 'YYYYMMDD'

Hi,
I'd like to get the file timestamp by 'YYYYMMDD' on Solaris 9 9/05.
I can get it on the other UNIX distribution with the following command;
ls -d -l --time-style='+%Y%m%d'$FNAME | awk '{print $6; }'
but cannot get it on Solaris, it comes format error.
Could you give me any advice on this?

You need to install Gnu "ls" to make use of its specific extensions.

try this perl command:

ls |perl -ne'chomp;($d,$m,$y)=(localtime((stat($))[9]))[3,4,5];printf "$ %4d%02d%02d\n",$y+1900,$m,$d'

or you can try the stat command, but i'm not sure if it exists in solaris 10.

regards

Elph,

I was playing around with Solaris 10 and came up with this:

ls -E $FNAME | awk '{print substr($6,1,4) substr($6,6,2) substr($6,9,3);}'

not sure if it works with Solaris 9, but you can try it.

Dear all, I really appreciated all of your advice.

jlliagre, unfortunately, we cannot install new program on our customer's server with 'crazy' security policy.

rugdog, at my environment we don't have stat command.
Tried perl command and I got this ...
>$FILE 20100230

sdanner, unfortunately, we don't have -E option for list command.
> ls -1RaAdCxmnlhogrtucpFbqisfL@ [files]

I'll try more harder on your tips, but further advice would be so much appreciated.

Thanx,
elph

ls -l --time-style='+%Y%m%d' | perl -e 'while (<>){chomp;s/\s+/ /g; my @arr=split(" ");print "$arr[-1]\t$arr[-2]\n";}'

I have not checked this on solaris .but its on the assumption that filename is the last column and time is last before

HTH,
PL

Dear datptal, thanks for your kind advice.

Unfortunately, at this environment, we can't use "time-style".
(jlliagre had kindly advised to install GNU but we can't do it due to security reason)

If you have any furthere solution, I'd be so grateful.
Your perl script helped me a lot.

Rgs,
elph

ls -l | perl -e 'while (<>){chomp;
s/\s+/ /g; 
my @arr=split(" ");
print "$arr[-1]\t";
$date = $arr[-3];
$date =~ s/-//g;
print "$date\n";}' 

HTH,
PL

I hate to tell you this, but it still dosen't seem to go well, due to my Japanese environment.

> ls -l $FNAME | perl -e 'while (<>){chomp; s/\s+/ /g; my @arr=split(" "); print "$arr[-1]\t"; $date = $arr[-3]; $date =~ s/-//g; print "$date\n";}'
test.txt 30

I'll try it more with your precious hint...

#!/bin/ksh

ls $@ | while read line
do
  ts=$(truss -f -v 'lstat,lstat64' ls -d "$line" 2>&1 | grep 'ct =' | nawk '{
  YY=$8;
  DD=$5;
  if($4 == "Jan") MM=1;
  if($4 == "Feb") MM=2;
  if($4 == "Mar") MM=3;
  if($4 == "Apr") MM=4;
  if($4 == "May") MM=5;
  if($4 == "Jun") MM=6;
  if($4 == "Jul") MM=7;
  if($4 == "Aug") MM=8;
  if($4 == "Sep") MM=9;
  if($4 == "Oct") MM=10;
  if($4 == "Nov") MM=11;
  if($4 == "Dec") MM=12;
  printf("%4d%02d%02d",$8,MM,$5);
  }')
  printf "%s %s\n" $ts "$(ls -ld "$line")"
done

Can you post a snippet of ls -l output or whatever you intend to use. so depending on the output of the command refining of the script becomes easy.

PL

Many thanks for your advise. Here's normal ls -l output at my current environment.

>ls -l test.txt
-rw-r----- 1 jpXXXX appXXXX 0 3 31 13:09 test.txt

I'm not sure you can view our own character, but just in case, "3 31" means March 31 (is month is day in Japanese).

And what my intention is I'd like to copy as back up with timestamp if there is already the same name file in the directory.
Following simple script can work on other distribution but not on Soralis...

if ( -f ${FNAME} ) then
set TSP = `ls -d -l --time-style='+%Y%m'$FNAME | awk '{print $6; }'`;
mv $FANME $TSP_$FNAME
endif

Sorry, I should post my intention more earlier.
If you need any other information, pls let me know.

Again, thanks all of your help.
elph

@elph: does the script I posted here Get timestamp by 'YYYYMMDD' Post: 302408599 work for you ?

Sorry for my late reply and rudeness, jlliagre.
I tried your script on my environment and got the error as follows;

> sh test.csh
test.csh: `ts=$' unexpected

With set command it also didn't go well at my place in C shell, as we usually use C shell here.
(Our /bin/ksh should be too old or lack of some module, I'm afraid)

But still I'd like to try modifying your good sample for this trial. Many thanks for all of your kind attention.

Rgs,
elph

I'm confused by your tests. What I wrote is a ksh script. You apparently named it test.csh but finally run it through the sh interpreter. These are three different scripting languages and their syntax isn't compatible.

Please use a proper extension if you want too (.ksh) and make this script executable. Run it directly by providing its full path and without specifying an interpreter.

eg:

mv test.csh ls-timestamp.ksh
chmod +x ls-timestamp.ksh
./ls-timestamp.ksh

@elph
Omitting to mention that this is "C shell" environment with Japanese character set has not helped the posters.

Anyone pursuing this post will need an understanding of this environment.

LC_ALL=C ls -l | perl -e 'my %hash = ( "Jan"=> "01" , "Feb" => "02" , "Mar" => "03" , "Apr" => "04"); # add other months
while (<>){
chomp; s/\s+/ /g;
my @arr=split(" ");
print "$arr[-1]\t";
my $mon_value = $hash{$arr[-4]};
my $day = $arr[-3];
my $date = "2010".$mon_value.$day; # assuming year is 2010 if year present use the appropriate column
print "$date\n";
}'

I dont have solaris or japanese environment :wink: . So may be u can try if this helps. make sure the months specified in the hash are of the format you get in the ls output

Hi, jlliagre
Sorry for my carzy output last time. Thanks to your script, I got it at last what I'd like to have!
Here's the result for test.txt which was made in March 31.

> ./ls-timestamp.ksh
20100331
(This result was just chanded $@ to target file, "ls test.txt | while read line" and delete "$(ls -ld "$line")"
at the end from your script)

I'll try it to work on C shell for our environment. If I'll have any problems, pls let me post it as other topic.
Thank you so much:)

Hi, daptal
I really appreciated all of your advise. To see your script, I'd lovely to learn perl more and more, which will help us our intentions:b:

Hi, methyl
You're right. I'm sure to post the output in universalistic way in my next topic;)

Dear all,
That was my first time to post here and so glad to know I was posting the right place.Thanks for all of your attention and advices.

Sincerly,
elph

Glad you finally got it and thanks for the feedback.