Files

如何在不修改 ls 輸出的情況下在 AIX 中獲取文件所有者?

  • June 22, 2016

在 AIX 中如何可靠地獲取文件的所有者?通過可靠,我不想解析ls. 在 Linux 上,我只會做 a stat --printf=%U foo,但我正在使用 AIX 6.1 和 7.1。我知道我可以做到,但由於在 AIX 上istat沒有選項,我仍然必須使用and來調整輸出,因此不理想。換句話說,我如何僅使用 AIX 的核心工具來模擬 Linux?--printf``istat``grep``awk``stat --printf=%U foo

這是我不久前編寫的一個腳本,目的是在 AIX 中獲得類似 stat(1) 的實用程序。剛剛添加了 %U!我發現使用 -c 選項更有用,它的行為與 –printf 略有不同。包括一個方便的 perl stat 數組的本地副本作為註釋塊。

#!/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

}

引用自:https://unix.stackexchange.com/questions/291143