1

I need to display file meta data from the current directory in the format below.

The first column should be left-aligned, second centered and third right aligned to the right end of the terminal screen.

  • Most Unix filesystems does not store file creation timestamps. – Kusalananda Sep 4 '18 at 12:23
  • @Kusalananda, most Linux and BSD file systems do though. – Stéphane Chazelas Sep 4 '18 at 12:28
  • Do you want those 3 columns separated by a fixed amount of white space, or the first left-aligned, second centered and third right aligned to the right end of the terminal screen? – Stéphane Chazelas Sep 4 '18 at 12:30
  • What format for those "dates"? – Stéphane Chazelas Sep 4 '18 at 12:30
  • 2
    What happened to the pertinent information in the question? "File Name File Creation Date Last File access date" and "Solaris" – Jeff Schaller Sep 6 '18 at 12:41
3

With GNU find (possibly available as gfind on your system):

find . -mindepth 1 -maxdepth 1 -printf '%-58P %TF %AF\n'

Would print file name, last modification date (creation date of the content of the file if you like) and last access date in YYYY-mm-dd format.

To restrict to .txt and .bkp files not accessed within the last 365 days:

find . -mindepth 1 -maxdepth 1 \
  \( -name '*.txt' -o -name '*.bkp' \) \
  -atime +364 -printf '%-58P %TF %AF\n'

Columns should be aligned as long as file names don't contain control or multi-byte or zero-width or double-width characters and are not longer than 58 bytes.

The first column is left-aligned, third right aligned (assuming a 80 column wide terminal), the second is shifted to the right to leave room for the file names. Adjust the filename width and spacing if you really want it centred.

Note that the file list is not sorted.

With ast-open ls (or the ls builtin of ksh93 if built with it):

ls -AZ '%-58(name)Ls %(mtime:%F)s %(atime:%F)s'

Should preserve alignment for multi-byte, zero-width and double-width characters and sort the list.

With zsh:

#! /bin/zsh -
zmodload zsh/stat
for f (*(DN)) {
  stat -LH s -F %F -- $f &&
    printf '%-58s %s %s\n' $f $s[mtime] $s[atime]
}

To restrict to .txt and .bkp files not accessed within the last 365 days, replace *(DN) with *.(txt|bkp)(DNa+364).

Should preserve alignment for multi-byte characters and sort the file names.

Since there's no standard command to retrieve modification and access time in a reliable way, portably, as usual, your best bet may be to use perl:

perl -MPOSIX -Mlocale -Mopen=locale -MEncode::Locale -MEncode -e '
  opendir D, "." || die "open .: $!\n";
  for (
    sort {$a->[1] cmp $b->[1]}
      map {[$_, decode(locale => $_)]}
      grep {!/^\.{1,2}\z/}
      readdir D
  ) {
    if (@s = lstat$_->[0]) {
      printf("%-58s %s %s\n", $_->[1],
        map {strftime("%Y-%m-%d", localtime $_)} ($s[9], $s[8]))
    } else {warn "$_: $!\n"}
  }'

Like with the zsh approach, it should preserve alignment for multi-byte characters, and sort the file names according to locale collation order.

As Solaris 10 comes with an ancient version of perl, you may have to give up support for non-ASCII characters in file names which would simplify it to:

perl -MPOSIX -e '
  opendir D, "." || die "open .: $!\n";
  for (sort grep {!/^\.{1,2}\z/} readdir D) {
    if (@s = lstat$_) {
      printf("%-58s %s %s\n", $_,
        map {strftime("%Y-%m-%d", localtime $_)} ($s[9], $s[8]))
    } else {warn "$_: $!\n"}
  }'

To restrict to .txt and .bkp files not accessed within the last 365 days, replace !/^\.{1,2}\z/ with /\.(txt|bkp)\z/ && -A > 365

  • Thanks for script but unfortunately stat command isn't working. I think it's not installed on Solaris 5.10. – Akki Sep 5 '18 at 4:29
  • @Akki, stat becomes a zsh shell builtin after you run zmodload zsh/stat. That code has to run in zsh. zsh is available on Solaris, though not necessarily installed by default depending on the flavour of Solaris you have. – Stéphane Chazelas Sep 5 '18 at 6:40
  • Thanks, I have 1 script which is showing result at file level. I want to run this script as directory level, So that i can get the details of all files in required format. Please find the script. – Akki Sep 5 '18 at 10:26
  • E_NOARG=65 E_MOREARG=66 if [ $# = 0 ] then echo -e '\E[31m' "Usage: $0 <filename>" tput sgr0 exit $E_NOARG elif [ $# -gt 1 ] then echo -e '\E[31m' "Please give only one file at a time" tput sgr0 exit $E_MOREARG fi LS=which ls MOD_TIME=$LS -lart $1|awk '{print $6" "$7" "$8" "}' ACC_TIME=$LS -laru $1|awk '{print $6" "$7" "$8" "}' echo "The modified time of the file is $MOD_TIME" echo "The accessed time of the file is $ACC_TIME" – Akki Sep 5 '18 at 10:27
  • @Akki, you can't parse the output of ls -l reliably, see the edit where the last perl command should work on a plain Solaris 10. – Stéphane Chazelas Sep 5 '18 at 11:11

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.