Linuxにある程度慣れていると、現在のファイルのパーミッションは記号表記で、設定の際は数字表記でやることが多いだろう。 で、いつも記号表記でばかり確認しているのだが、ふと数字表記で現在のパーミッションを取得できるのかなと思い調べてみた。 個人的には大体lsで調べるのだが、残念ながらそういったオプションは無いらしい。

1. statコマンドを用いる場合

statコマンドで、以下のように指定することでファイルのパーミッションを数字表記で取得できるようだ。

bash
stat PATH -c '%a'
shell
[root@BS-PUB-CENT7-01 ~]# ls -la ./test.pl -rw-r--r--. 1 root root 99 5月 20 11:26 ./test.pl [root@BS-PUB-CENT7-01 ~]# stat ./test.pl -c '%a' 644

これを利用してlsと組み合わせることで、以下のようにlsの表記とセットで表示させることができる。 (ファイル名にスペースが含まれないことが前提になる)

bash
ls -la | awk 'NR>1{cmd="stat "$NF" -c %a";cmd|getline c;close(cmd);print c,$0}'
shell
[root@BS-PUB-CENT7-01 ~]# ls -la 合計 60 dr-xr-x---. 8 root root 4096 5月 20 16:06 . dr-xr-xr-x. 17 root root 4096 1月 20 00:01 .. -rw-------. 1 root root 1537 5月 16 09:50 .bash_history -rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout -rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile -rw-r--r--. 1 root root 176 12月 29 2013 .bashrc drwxr-xr-x. 4 root root 31 5月 16 10:03 .config drwxr-xr-x. 6 root root 89 5月 20 11:29 .cpan -rw-r--r--. 1 root root 100 12月 29 2013 .cshrc -rw-------. 1 root root 40 5月 20 16:06 .lesshst drwxr-xr-x. 3 root root 18 5月 16 09:59 .local drwxr-----. 3 root root 18 5月 16 09:58 .pki -rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc -rw-------. 1 root root 2539 5月 20 11:26 .viminfo drwxr-xr-x. 15 root root 4096 5月 16 10:09 DevAudit -rw-------. 1 root root 986 1月 1 2016 anaconda-ks.cfg drwxr-xr-x. 5 root root 36 5月 20 11:29 perl5 -rw-r--r--. 1 root root 99 5月 20 11:26 test.pl -rw-r--r--. 1 root root 28 5月 18 08:50 test1.txt -rw-r--r--. 1 root root 86 5月 18 08:40 test2.txt [root@BS-PUB-CENT7-01 ~]# ls -la | awk 'NR>1{cmd="stat "$NF" -c %a";cmd|getline c;close(cmd);print c,$0}' 550 dr-xr-x---. 8 root root 4096 5月 20 16:06 . 555 dr-xr-xr-x. 17 root root 4096 1月 20 00:01 .. 600 -rw-------. 1 root root 1537 5月 16 09:50 .bash_history 644 -rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout 644 -rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile 644 -rw-r--r--. 1 root root 176 12月 29 2013 .bashrc 755 drwxr-xr-x. 4 root root 31 5月 16 10:03 .config 755 drwxr-xr-x. 6 root root 89 5月 20 11:29 .cpan 644 -rw-r--r--. 1 root root 100 12月 29 2013 .cshrc 600 -rw-------. 1 root root 40 5月 20 16:06 .lesshst 755 drwxr-xr-x. 3 root root 18 5月 16 09:59 .local 740 drwxr-----. 3 root root 18 5月 16 09:58 .pki 644 -rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc 600 -rw-------. 1 root root 2539 5月 20 11:26 .viminfo 755 drwxr-xr-x. 15 root root 4096 5月 16 10:09 DevAudit 600 -rw-------. 1 root root 986 1月 1 2016 anaconda-ks.cfg 755 drwxr-xr-x. 5 root root 36 5月 20 11:29 perl5 644 -rw-r--r--. 1 root root 99 5月 20 11:26 test.pl 644 -rw-r--r--. 1 root root 28 5月 18 08:50 test1.txt 644 -rw-r--r--. 1 root root 86 5月 18 08:40 test2.txt

2. findコマンドで出力させる

findコマンドで、printfオプションで%mを用いることで、数字表記でのパーミッションを表示させることも可能だ。 以下の例では、カレントディレクトリ配下のファイル・ディレクトリに対してパーミッションやサイズ、ユーザ名等を取得している。

bash
find . -maxdepth 1 -printf "%m %u:%g %s %p \n"
shell
[root@BS-PUB-CENT7-01 ~]# find . -maxdepth 1 -printf "%m %u:%g %s %p \n" 550 root:root 4096 . 644 root:root 18 ./.bash_logout 644 root:root 176 ./.bash_profile 644 root:root 176 ./.bashrc 644 root:root 100 ./.cshrc 644 root:root 129 ./.tcshrc 600 root:root 986 ./anaconda-ks.cfg 600 root:root 1537 ./.bash_history 755 root:root 4096 ./DevAudit 740 root:root 18 ./.pki 755 root:root 31 ./.config 755 root:root 18 ./.local 644 root:root 28 ./test1.txt 644 root:root 86 ./test2.txt 644 root:root 99 ./test.pl 600 root:root 2539 ./.viminfo 755 root:root 89 ./.cpan 755 root:root 36 ./perl5 600 root:root 47 ./.lesshst

とりあえず2個。 lsとstat組み合わせた方が読みやすいかもしれない。