List files and display last modify time in a particular format

hi everyone,

can someone suggest how i can list the contents of a directory and display their corresponding last modify time in the format yyyymmddhhmm?

thanks in advance!

If you have GNU ls:

ls -l --time-style="+%Y%m%d%H%M"

^thanks, but im using csh on solaris...

It doesn't matter what shell you are using if you have the GNU version of ls. (I believe it is often installed on solaris; it certainly can be.)

^ I don't have GNU.

Hello Deanne,

You most likely would be having perl .
try this.

ls -l dir | perl -wln -e 'split(/\s+/);@f1=split("-",$_[5]),@f2=split(":",$_[6]);print @f1,@f2'

where dir is the directory containing the files.

Regards.

hi gaurav, thanks.
i tried your code and the following is what the shell returns:

Use of implicit split to @_ is deprecated at -e line 1.
Use of uninitialized value in split at -e line 1, <> line 1.
Use of uninitialized value in split at -e line 1, <> line 1.

Apr1
Jan22
Jan6
Jan5
Jan5
Jan5
Mar14
Jan5
Jan5
Jun30
Aug22
Apr2
Apr1
Nov4

Hmmm, sorry im perl clueless. please help.
thanks in advance.

Hello Deane,

watch below->

gaurav@localhost:~$ ls perl_code/
1a.pl          circle.pl    extract.pl    list.pl                      new_boy.pl   parse.pl          simple.pl   sub.pl   while_undef.pl
1.pl           core.pl      file.txt      match.pl                     new_cgi.cgi  pdf1.pl           sor.pl      test     xop.pl
anew           count.pl     first.html    Module-CoreList-2.17         newd.pl      place_holders.pl  spider.pl   tree.pl
backup.pl      dab2.pl      glob.pl       Module-CoreList-2.17.tar.gz  new_.html    radio-cgi.cgi     split.pl    tt
base.pl        data.pl      hello.pl      my_first.html                odd.pl       radio.html        sub1.pl     tt.tar
cgi_first.cgi  dbi_main.pl  htmlparse.pl  mymodule                     one.cgi      rev.pl            submit.cgi  up.pl
gaurav@localhost:~$ ls -l perl_code/ | perl -ln -e 'split(/\s+/);@f1=split("-",$_[5]),@f2=split(":",$_[6]);print @f1,@f2,"\t",$_[7]'
	
200907231348	1a.pl
200907231324	1.pl
200908221347	anew
200907150024	backup.pl
200907230324	base.pl
200907291235	cgi_first.cgi
200907101449	circle.pl
200907231345	core.pl
200907101445	count.pl
200907261412	dab2.pl
200907261413	data.pl
200907270135	dbi_main.pl
200908281739	extract.pl
200907140919	file.txt
200907291425	first.html
200907150029	glob.pl
200907101434	hello.pl
200908221149	htmlparse.pl
200907141042	list.pl
200907140822	match.pl
200907231214	Module-CoreList-2.17
200907231204	Module-CoreList-2.17.tar.gz
200908221430	my_first.html
200907231306	mymodule
200907270102	new_boy.pl
200907291422	new_cgi.cgi
200907261436	newd.pl
200908221341	new_.html
200907181414	odd.pl
200908081353	one.cgi
200908281735	parse.pl
200908270347	pdf1.pl
200907270051	place_holders.pl
200908081344	radio-cgi.cgi
200907300801	radio.html
200907101557	rev.pl
200908241451	simple.pl
200907101600	sor.pl
200908261616	spider.pl
200907140829	split.pl
200907121711	sub1.pl
200908081339	submit.cgi
200907180745	sub.pl
200907150025	test
200908221448	tree.pl
200909011628	tt
200909011630	tt.tar
200907101433	up.pl
200907131140	while_undef.pl
200907101456	xop.pl
gaurav@localhost:~$ 


now check with yours. you have to pipe the list of files(long listing i.e. 'ls -l') of some directory that you want.
Regards.
Gaurav.

It seems that ls -l output of yours and his seems to be differing.

Instead of showing your ls output, show the "ls -l".

Also, ls -l format is not similar always, so trying to parse that will not help. For example:

  1. For the current and previous years, it wont show the Year number,
  2. but for other years it will show,
drwx------ 2 gdm     gdm     4096 Jan  1  1970 orbit-gdm/
drwx------ 2 test      test     4096 Jan 10 13:10 plugtmp/

thegeek,
thanks for pointing out.

gaurav@localhost:~$ ls -l perl_code/
total 256
-rw-r--r-- 1 gaurav gaurav   109 2009-07-23 13:48 1a.pl
-rw-r--r-- 1 gaurav gaurav    47 2009-07-23 13:24 1.pl
-rw-r--r-- 1 gaurav gaurav  6616 2009-08-22 13:47 anew
-rw-r--r-- 1 gaurav gaurav   151 2009-07-15 00:24 backup.pl
-rw-r--r-- 1 gaurav gaurav   173 2009-07-23 03:24 base.pl
-rwxr-xr-x 1 gaurav gaurav   314 2009-07-29 12:35 cgi_first.cgi
-rwxr-xr-x 1 gaurav gaurav   144 2009-07-10 14:49 circle.pl
-rw-r--r-- 1 gaurav gaurav   258 2009-07-23 13:45 core.pl
-rwxr-xr-x 1 gaurav gaurav    71 2009-07-10 14:45 count.pl
-rw-r--r-- 1 gaurav gaurav   273 2009-07-26 14:12 dab2.pl
-rw-r--r-- 1 gaurav gaurav   390 2009-07-26 14:13 data.pl
-rw-r--r-- 1 gaurav gaurav   726 2009-07-27 01:35 dbi_main.pl
-rwxr-xr-x 1 gaurav gaurav   179 2009-08-28 17:39 extract.pl
-rw-r--r-- 1 gaurav gaurav    38 2009-07-14 09:19 file.txt
-rw-r--r-- 1 gaurav gaurav   385 2009-07-29 14:25 first.html
-rw-r--r-- 1 gaurav gaurav    76 2009-07-15 00:29 glob.pl
-rwxr-xr-x 1 gaurav gaurav    84 2009-07-10 14:34 hello.pl
-rwxr-xr-x 1 gaurav gaurav   128 2009-08-22 11:49 htmlparse.pl
-rwxr-xr-x 1 gaurav gaurav    62 2009-07-14 10:42 list.pl
-rwxr-xr-x 1 gaurav gaurav    82 2009-07-14 08:22 match.pl
drwxr-xr-x 5 gaurav gaurav  4096 2009-07-23 12:14 Module-CoreList-2.17
-rwxr-xr-x 1 gaurav gaurav 42602 2009-07-23 12:04 Module-CoreList-2.17.tar.gz
-rw-r--r-- 1 gaurav gaurav   525 2009-08-22 14:30 my_first.html
drwxr-xr-x 5 gaurav gaurav  4096 2009-07-23 13:06 mymodule
-rw-r--r-- 1 gaurav gaurav   202 2009-07-27 01:02 new_boy.pl
-rwxr-xr-x 1 gaurav gaurav   632 2009-07-29 14:22 new_cgi.cgi
-rw-r--r-- 1 gaurav gaurav   539 2009-07-26 14:36 newd.pl
-rw-r--r-- 1 gaurav gaurav  6631 2009-08-22 13:41 new_.html
-rwxr-xr-x 1 gaurav gaurav    68 2009-07-18 14:14 odd.pl
-rwxr-xr-x 1 gaurav gaurav   117 2009-08-08 13:53 one.cgi
-rwxr-xr-x 1 gaurav gaurav   359 2009-08-28 17:35 parse.pl
-rw-r--r-- 1 gaurav gaurav   237 2009-08-27 03:47 pdf1.pl
-rw-r--r-- 1 gaurav gaurav   276 2009-07-27 00:51 place_holders.pl
-rw-r--r-- 1 gaurav gaurav  1250 2009-08-08 13:44 radio-cgi.cgi
-rw-r--r-- 1 gaurav gaurav  1060 2009-07-30 08:01 radio.html
-rwxr-xr-x 1 gaurav gaurav   134 2009-07-10 15:57 rev.pl
-rw-r--r-- 1 gaurav gaurav   144 2009-08-24 14:51 simple.pl
-rwxr-xr-x 1 gaurav gaurav    53 2009-07-10 16:00 sor.pl
-rwxr-xr-x 1 gaurav gaurav   373 2009-08-26 16:16 spider.pl
-rw-r--r-- 1 gaurav gaurav    90 2009-07-14 08:29 split.pl
-rwxr-xr-x 1 gaurav gaurav   165 2009-07-12 17:11 sub1.pl
-rw-r--r-- 1 gaurav gaurav   263 2009-08-08 13:39 submit.cgi
-rw-r--r-- 1 gaurav gaurav    83 2009-07-18 07:45 sub.pl
drwxr-xr-x 2 gaurav gaurav  4096 2009-07-15 00:25 test
-rwxr-xr-x 1 gaurav gaurav   320 2009-08-22 14:48 tree.pl
drwxr-xr-x 2 gaurav gaurav  4096 2009-09-01 16:28 tt
-rw-r--r-- 1 gaurav gaurav 10240 2009-09-01 16:30 tt.tar
-rwxr-xr-x 1 gaurav gaurav    96 2009-07-10 14:33 up.pl
-rw-r--r-- 1 gaurav gaurav    53 2009-07-13 11:40 while_undef.pl
-rwxr-xr-x 1 gaurav gaurav   144 2009-07-10 14:56 xop.pl
gaurav@localhost:~$ 

I see that we have different ls -l output.

Mine looks similar to thegeek's example:

drwx------ 2 gdm     gdm     4096 Jan  1  1970 orbit-gdm/
drwx------ 2 test      test     4096 Jan 10 13:10 plugtmp/

do you know how i can display the date and time fields like yours gaurav, so i can parse as you did?

Hello Deanne,
which *nix are you using?
post #uname -a
post ls --version
try ls -al to see if it gives in that format of the date.

Reply.

hi gaurav,

please see below:

pdc44_abc818j>> uname -a
SunOS abc44 5.8 Generic_117350-61 sun4u sparc SUNW,Sun-Blade-1000

abc44_abc818j>> ls --version
ls: illegal option -- -
ls: illegal option -- v
ls: illegal option -- e
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]

abc44_abc818j>> ls -version
ls: illegal option -- v
ls: illegal option -- e
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]

abc44_abc818j>> ls -v
ls: illegal option -- v
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]

ls -la still gives a different format of the date (hh:mm info is only available for newer files, yyyy is given otherwise)

In SUN solaris you can use the -E option of ls as below and you can start from there:-
for more info. about -E option use:- man ls

ls -ltrE
o/p

-rw-rw-r--   1 ahmadd   omc          375 2009-11-15 11:46:32.637413000 +0200 ll
-rwxrwxr-x   1 ahmadd   omc           91 2009-11-17 10:49:33.796841000 +0200 do
-rw-rw-r--   1 ahmadd   omc          103 2009-11-17 18:25:12.361537000 +0200 using.xargs
-rw-rw-r--   1 ahmadd   omc           31 2009-11-22 14:54:01.154850000 +0200 1.html
-rw-rw-r--   1 ahmadd   omc           31 2009-11-22 14:54:01.203357000 +0200 2.htm
-rw-rw-r--   1 ahmadd   omc          137 2009-11-23 17:10:31.149394000 +0200 awk.print.B.Before.A.After.String
-rw-rw-r--   1 ahmadd   omc           48 2009-11-24 17:49:40.241181000 +0200 command.to.write.on.colors
-rw-rw-r--   1 ahmadd   omc           20 2009-12-02 22:19:41.248697000 +0200 3.php
-rw-rw-r--   1 ahmadd   omc           59 2009-12-03 16:36:22.465366000 +0200 awk.grep.paragraph.between.2.strings
-rw-rw-r--   1 ahmadd   omc          337 2009-12-07 14:09:02.357586000 +0200 awk.print.fields.in.table.format.nice.one
-rwxr-xr-x   1 ahmadd   omc           57 2009-12-29 10:39:46.166845000 +0200 runtest
-rw-rw-r--   1 ahmadd   omc          131 2009-12-30 10:34:35.588184000 +0200 awk.to.split.file.to.multiple.files
-rw-rw-r--   1 ahmadd   omc          337 2009-12-31 09:59:21.258534000 +0200 awk.to.handle.web.programing
-rw-rw-r--   1 ahmadd   omc          201 2010-01-03 14:27:56.136898000 +0200 script.to.find.median
-rw-rw-r--   1 ahmadd   omc           19 2010-01-11 19:12:16.541827000 +0200 test2
-rw-rw-r--   1 ahmadd   omc          118 2010-01-12 10:06:06.453932000 +0200 test



 

---------- Post updated at 11:26 ---------- Previous update was at 11:13 ----------

use the below:-:cool::cool::cool:
;);):wink:

ls -trE dirname | nawk '{split($6,a,"-") ; split($7,b,":") ; $6=a[1]a[2]a[3]b[1]b[2] ; $7=$8=""}1
'OFS="\t"
o/p

-rw-rw-r--      1       ahmadd  omc     1091    200911101001                    sed1.cr
-rw-rw-r--      1       ahmadd  omc     246     200911101001                    word.cr
-rw-rw-r--      1       ahmadd  omc     184     200911101001                    grep3.cr
-rw-rw-r--      1       ahmadd  omc     173     200911101001                    grep3v2.cr
-rw-rw-r--      1       ahmadd  omc     121     200911101001                    pebw.cr
-rw-rw-r--      1       ahmadd  omc     274     200911101001                    grep4.cr
-rw-rw-r--      1       ahmadd  omc     211     200911101001                    grep5.cr
-rw-rw-r--      1       ahmadd  omc     115     200911101001                    convert2uc.cr
-rw-rw-r--      1       ahmadd  omc     204     200911101001                    flowcontrol.cr
drwxrwxr-x      3       ahmadd  omc     1536    200911101007                    shells_commands
-rw-rw-r--      1       ahmadd  omc     14      200911101008                    word2.cr
-rw-rw-r--      1       ahmadd  omc     99      200911101008                    5.cr
drwxrwxr-x      3       ahmadd  omc     4096    201001111257                    AWK

Hi ahmad.

The -E option is not available on Solaris 8 (at least not my Solaris 8!) - but it is available on Solaris 10 (and maybe 9, but I don't have that).

Tested on Solaris 8:

# ll -E passwd
ls: illegal option -- E
usage: ls -1RaAdCxmnlogrtucpFbqisfL [files]

# ll passwd
-r--r--r--   1 root     sys          6118 Jun 23  2009 passwd

# perl -e 'foreach(@ARGV){ $time=localtime((stat( $_ ))[9]); printf("%s: %s\n", $_,$time); }' * | grep passwd
passwd: Tue Jun 23 11:59:19 2009

Dear scottn can you find this option on the below paths ? I think it is there in solaris 8

/usr/xpg4/bin/ls -E
or
/usr/xpg6/bin/ls -E

;):wink:

thanks ahmad, but i also dont have -E option. same output as scottn.

but hey, i guess i will just start from the output of the perl code scottn suggested. :ok:

thanks everyone!