How might I reliably get the owner of a file in AIX? By reliable, I don't want to parse the output of ls. On Linux, I would just do a stat --printf=%U foo, but I'm working on AIX 6.1 and 7.1. I know I can do istat, but as there is no --printf option on AIX istat, I would still have to munge the output with grep and awk, therefore not as desirable. In other words, how can I emulate Linux's stat --printf=%U foo using only AIX's core utils?

  • 1
    Is perl or tcl available in core? – thrig Jun 21 '16 at 15:43
  • Yes, I have perl, seems like it might be standard to have on AIX. After tinkering around, I was able to get it with this one-liner /usr/bin/perl -e "print ((getpwuid((stat('foo'))[4]))[0]);". It works, just not as pretty to write as Linux stat. I guess I'm really just playing code (script) golf. – Joshua Huber Jun 21 '16 at 16:54
  • I haven't seen a user-level interface to stat() in AIX; I ended up writing a perl script that emulates Linux's stat(1). – Jeff Schaller Jun 21 '16 at 16:54
  • @JeffSchaller I would be interested to see this script. Does it emulate --printf and other nice Linux stat modes? If it's not too huge, could you post as an Answer so I can upvote? – Joshua Huber Jun 21 '16 at 16:56
  • sure; I was just adding %U support to it, so it's a more-direct answer to your Q :) – Jeff Schaller Jun 21 '16 at 16:57

This is a script I wrote a while ago, in order to get a stat(1)-like utility in AIX. Just added %U! I found it more useful to use the -c option, which behaves slightly differently from --printf. Includes a handy-dandy local copy of perl's stat array as a comment block.

#!/usr/bin/env perl -w
# emulate GNU coreutils stat command in a limited way
# -- only implemented a subset of the stat() options

use strict;
use Getopt::Std;
our $opt_c;

getopts('c:') or die "Usage: $0 [ -c (%n %i %u %g %s %U %X %Y %Z) ] file ...";
# default format is empty (not useful, but avoids 'undef' errors later)
$opt_c |= '';

for (@ARGV) {
  my @s = stat;
  next unless @s; # silently fail on to the next file
  my $p = $opt_c; # make a copy of the format string to mangle for each file

  # mangle and print
  $p =~ s/%n/$_/g;
  $p =~ s/%i/$s[1]/g;
  $p =~ s/%u/$s[4]/g;
  $p =~ s/%g/$s[5]/g;
  $p =~ s/%s/$s[7]/g;
  $p =~ s/%U/getpwuid($s[4])/eg;
  $p =~ s/%X/$s[8]/g;
  $p =~ s/%Y/$s[9]/g;
  $p =~ s/%Z/$s[10]/g;
  print "$p\n";

  #                 0 dev      device number of filesystem
  #                 1 ino      inode number
  #                 2 mode     file mode  (type and permissions)
  #                 3 nlink    number of (hard) links to the file
  #                 4 uid      numeric user ID of file's owner
  #                 5 gid      numeric group ID of file's owner
  #                 6 rdev     the device identifier (special files only)
  #                 7 size     total size of file, in bytes
  #                 8 atime    last access time in seconds since the epoch
  #                 9 mtime    last modify time in seconds since the epoch
  #                10 ctime    inode change time in seconds since the epoch (*)
  #                11 blksize  preferred block size for file system I/O
  #                12 blocks   actual number of blocks allocated

  • This seems a lot more involved than just using awk – Bratchley Jun 22 '16 at 1:53
  • It's certainly another script to have to carry around; I would rather have a perl script that's self-sufficient than count on IBM to be consistent with the formatting of its own (istat) command. Tough trade-off, particularly if perl isn't installed. If perl's not installed, then the coreutils RPM probably isn't, either -- but would provide the stat command independently. – Jeff Schaller Jun 22 '16 at 14:50
  • @JeffSchaller: There's istat command as a stat() interface but sadly it doesn't provide any options, so no printf or anything useful. – doktor5000 Jul 27 '16 at 14:07

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.