Skip to content

Instantly share code, notes, and snippets.

@teramako
Last active December 16, 2015 12:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save teramako/5432125 to your computer and use it in GitHub Desktop.
Save teramako/5432125 to your computer and use it in GitHub Desktop.
やっつけコード。与えられたファイルリストから chmod, chown をするコードを吐き出す。
#!/usr/bin/perl
=head1 chperm_maker.pl
与えられたファイルリストのパーミッションを
変更するスクリプトを生成する
=head1 例
$ find . -exec ls -d {} + | perl <this script path>
=head1 $flag について
$flag = 1 にすると chown も行うコードを生成。
chown <uid>:<gid> <path> となるため、
対象サーバでUID,GIDが同一であること
=cut
use strict;
use warnings;
use File::stat;
my $flag = 0;
while (<>) {
chomp;
my $path = $_;
my $st = stat $path;
printf "chmod %04o %s; ", $st->mode & 07777, $path;
if ($flag == 1) {
printf "chown %d:%d %s;\n", $st->uid, $st->gid, $path;
} else {
print "\n";
}
}
# vim: sw=4 ts=4 et:
@teramako
Copy link
Author

コレくらいならワンライナーでかけるだろクソがって声が聞こえた気がする

find . -exec ls -d {} + | perl -MFile::stat -ne 'my $f = $_; my $s = stat $f; printf "chmod %04o %s\n", $s->mode & 07777, $f;'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment