In Linux, I use the following command to show this output:

 find /backup/$INSTANCE/tsm/* -exec echo '"{}" ' \; | xargs stat --printf "chown %U:%G '%n'\nchmod %a '%n'\n" >> /tmp/permissions.txt

the command returns output like this:

[filipe@filipe ~]$ cat /tmp/permissions.txt  
chown filipe:filipe '/backup/filipe/tsm/1347123200748.jpg' 
chmod 766 '/backup/filipe/tsm/1347123200748.jpg'

How can I make the same output using the istat command in AIX?

Simplifying, I need a recursive output that contains the chmod and chown command of the files read by istat.

  • is perl available? – Jeff Schaller Oct 24 '18 at 16:05
  • (perl's installed by default, and is going to be much easier to work with than reversing the rwxr-xr-x-type permissions out of istat) – Jeff Schaller Oct 24 '18 at 16:51

Use find, but pass the filenames directly to a perl script that outputs the desired commands:

find /backup/"$INSTANCE"/tsm/* -exec /path/to/perl-script.pl {} +

Beware of filenames that contain single-quotes! I've modified the printed filename to quote any single-quotes.


#!/usr/bin/env perl -w
use strict;

for (@ARGV) {
  my @s = stat;
  next unless @s; # silently fail on to the next file
  my $filename = $_;
  $filename =~ s/'/'\\''/g;
  printf "chown %s:%s '%s'\nchmod %04o '%s'\n", $s[4], $s[5], $filename, ($s[2] & 07777), $filename;

If you prefer the textual user names and group names to the uids and gids, use the get* lookup functions:

  printf "chown %s:%s '%s'\nchmod %04o '%s'\n", (getpwuid($s[4]))[0], (getgrgid($s[5]))[0], $filename, ($s[2] & 07777), $filename;

Sample output:

chown 1000:1001 '/backup/myinstance/tsm/everyone-file'
chmod 0666 '/backup/myinstance/tsm/everyone-file'
chown 1000:1001 '/backup/myinstance/tsm/file'\''withquote'
chmod 0644 '/backup/myinstance/tsm/file'\''withquote'
chown 1000:1001 '/backup/myinstance/tsm/perl-script.pl'
chmod 0755 '/backup/myinstance/tsm/perl-script.pl'
chown 1000:1001 '/backup/myinstance/tsm/secure-file'
chmod 0600 '/backup/myinstance/tsm/secure-file'
chown 1000:1001 '/backup/myinstance/tsm/setuid-file'
chmod 4755 '/backup/myinstance/tsm/setuid-file'
chown 1000:1001 '/backup/myinstance/tsm/some-dir'
chmod 0755 '/backup/myinstance/tsm/some-dir'
  • using numbers on ID worked, but use names doesn't worked $ cat /tmp/teste.txt chown 57670:50011 '/backup/pddd767/tsm/Nodes' chmod 0774 '/backup/pddd767/tsm/Nodes' $ cat /tmp/teste.txt chown pddd767:* '57670' chmod 141533 '' chown pddd767:* '57670' chmod 141533 '' chown pddd767:* '57670' $ cat perl-script.pl [... cut bcs ltd char] #printf "chown %s:%s '%s'\nchmod %04o '%s'\n", getpwuid($s[4]), getgrgid($s[5]), $filename, ($s[2] & 07777), $filename; printf "chown %s:%s '%s'\nchmod %04o '%s'\n", getpwuid($s[4]), getgrgid($s[5]), $filename, ($s[2] & 07777), $filename; – Filipe Torres Oct 24 '18 at 18:09
  • my apologies, @FilipeTorres, my perl is apparently rusty! I wasn't pulling the appropriate element out of the get* functions; I've updated the code to do the right thing. – Jeff Schaller Oct 24 '18 at 18:23
  • No problem @Jeff Schaller i'm making a simple script to check and change all permiossion/owner of files. you will help me a lot – Filipe Torres Oct 24 '18 at 18:37
  • Thanks, Stéphane; I had run the command on a separate directory, since I didn't actually have a /backup/.../ structure. – Jeff Schaller Oct 24 '18 at 18:51
  • This path is irrelevant, because its change depends of some parameter of the script! But i will try put your codes on the script, thanks everyone. – Filipe Torres Oct 24 '18 at 19:32

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.