-2

acdbaqa@qaca010z:/tmp/joelj> ls -lrt hl.sh -rwxrwxrwx 1 acdbaqa acdba 2210 Jul 13 20:07 hl.sh

how to I convert the date format of any files to YYYYMMDD:HHSS

Mark Reed
  • 91,912
  • 16
  • 138
  • 175
John Joel
  • 15
  • 1
  • 4
  • 4
    I'm voting to close this question as off-topic because it is not about programming, but about how to use a certain program (`ls`). It is more appropriate for unix.stackexchange.com. – chepner Sep 11 '17 at 17:52
  • Looks like OP needs some scripting advise because this `ls` lacks a suitable option. @chepner – yacc Sep 11 '17 at 17:54
  • 1
    [Parsing ls is fraught with danger](http://mywiki.wooledge.org/ParsingLs). Just don't do it. If you want a formatted date of a file within a shell script, use `stat` instead. Read `man stat` to see usage instructions for your platform. – ghoti Sep 11 '17 at 19:14
  • I am trying to use this in part of shell script (or programming , whatever you call it as ) , else I wouldnt have brought this here ... These comments and answers that I got works fine on a Linux Machine but on Unix (SunOS hostname 5.10 Generic_150400-48 sun4u sparc SUNW,SPARC-Enterprise ) – John Joel Sep 11 '17 at 19:30

3 Answers3

1

In your example you seem to think 2210 is part of the date (it isn't, it's the file size) and your desired format leaves out the minutes. If both are unintentional, and you're using GNU ls, the --time-style option should do what you need:

ls -lrt --time-style="+%Y%m%d:%H%M%S"

which for your example will give more or less this result:

-rwxrwxrwx   1 acdbaqa  acdba       2210 20170713:200700 hl.sh
fvu
  • 32,488
  • 6
  • 61
  • 79
  • Its failing acdbaqa@qaca010z:/tmp/joelj> ls -lrt --time-style="+%Y%m%d:%H%M%S" ls: illegal option -- time-style=+%Y%m%d:%H%M%S usage: ls -1RaAdCxmnlhogrtuvVcpFbqisfHLeE@ [files] acdbaqa@qaca010z:/tmp/joelj> ls -lrt hl.sh --time-style="+%Y%m%d:%H%M%S" --time-style=+%Y%m%d:%H%M%S: No such file or directory -rwxrwxrwx 1 acdbaqa acdba 2210 Jul 13 20:07 hl.sh acdbaqa@qaca010z:/tmp/joelj> uname -a SunOS qaca010z 5.10 Generic_150400-48 sun4u sparc SUNW,SPARC-Enterprise – John Joel Sep 11 '17 at 17:51
  • acdbaqa@qaca010z:/tmp/joelj> ls -lrt --time-style="+%Y%m%d:%H%M%S" ls: illegal option -- time-style=+%Y%m%d:%H%M%S usage: ls -1RaAdCxmnlhogrtuvVcpFbqisfHLeE@ [files] acdbaqa@qaca010z:/tmp/joelj> ls -lrt hl.sh --time-style="+%Y%m%d:%H%M%S" --time-style=+%Y%m%d:%H%M%S: No such file or directory -rwxrwxrwx 1 acdbaqa acdba 2210 Jul 13 20:07 hl.sh acdbaqa@qaca010z:/tmp/joelj> uname -a SunOS qaca010z 5.10 Generic_150400-48 sun4u sparc SUNW,SPARC-Enterprise – John Joel Sep 11 '17 at 17:52
  • Don't try to put output in comments. But you probably don't have GNU ls. See my answer for a more portable option. – Mark Reed Sep 11 '17 at 17:55
  • Sunos 5.10, aka Solaris 10, from 2005 indeed does not use a GNU userland, so my solution won't work out of the box. – fvu Sep 11 '17 at 22:57
1

If all you want is the modified time of a file, rather than ls, consider using the stat command:

stat -f %Sm -t %Y%m%d:%H%M%S hl.sh

That's the long form of the date/time format; it can be abbreviated to %F:%T.

If you don't have stat, you can use perl:

perl -MPOSIX -le 'print(strftime "%Y%m%d:%H%M%S", localtime((stat "hl.sh")[9]))'

You can pass the filename in outside the code to avoid quoting/interpolation issues:

perl -MPOSIX -le 'print(strftime "%Y%m%d:%H%M%S", localtime((stat $ARGV[0])[9]))' hl.sh
Mark Reed
  • 91,912
  • 16
  • 138
  • 175
0

I'd favor the stat command but on Ubuntu I couldn't get it right. So here's my answer. It uses perl to do some string manipulation, so it might be considered a work-around somehow.

ls -lrt somefiles |perl -MDate::Manip -pe 's/^([-\w]+ \d+ \w+ \w+ \d+ )(\w+ +\d+ +[:\d]+)/$1.UnixDate(ParseDate($2),"%Y%m%d:%H%M")/e'

This recognizes date strings of the form %month %day %hour:%minute and converts it into the desired format.

Update: Now also works with the second date format which ls uses for files older than a year (%month %day %year).

Output for your file:

-rwxrwxrwx 1 acdbaqa acdba 2210 20170713:2007 hl.sh
yacc
  • 2,915
  • 4
  • 19
  • 33
  • Can't locate Date/Manip.pm in @INC (@INC contains: /app/ac/local/lib/perl5 /app/ac/lib/perl5 /app/localapps/perl/lib/sun4-solaris-64int /app/localapps/perl/lib /app/ac/local/lib /usr/perl5/5.8.4/lib/sun4-solaris-64int /usr/perl5/5.8.4/lib /usr/perl5/site_perl/5.8.4/sun4-solaris-64int /usr/perl5/site_perl/5.8.4 /usr/perl5/site_perl /usr/perl5/vendor_perl/5.8.4/sun4-solaris-64int /usr/perl5/vendor_perl/5.8.4 /usr/perl5/vendor_perl .). BEGIN failed--compilation aborted. – John Joel Sep 11 '17 at 18:32
  • You need to install this package. Run `cpan Date::Manip`. – yacc Sep 11 '17 at 18:35