ターミナル上でファイルの更新日付きのリストを作成する(Linux or 商用UNIX(AIX/HP-UX))

Twitterでそういった処理について見かけたので、備忘として残しておく事にする。 GNU拡張されたlsやfindのあるLinuxであれば処理は結構簡単なのだけど、POSIXのコマンドしか無い場合だとperlとかを使うしか無い。

Linux等の場合なら、lsを使う場合は以下のようにすれば取得が可能だ。

ls -lad $PWD/{.,}* --time-style="+%F %T"|eval awk "'{print" \${6..8}\, "_ }'"
ls -lad $PWD/{.,}* --time-style="+%F %T"|tr -s ' '|cut -d ' ' -f6-
[root@BS-PUB-CENT7-01 ~]# ls -lad $PWD/{.,}* --time-style="+%F %T"|eval awk "'{print" \${6..8}\, "_ }'"
2017-10-04 02:00:41 /root/.
2017-01-20 00:01:25 /root/..
2017-10-06 01:21:37 /root/.bash_history
2013-12-29 11:26:31 /root/.bash_logout
2013-12-29 11:26:31 /root/.bash_profile
2013-12-29 11:26:31 /root/.bashrc
2013-12-29 11:26:31 /root/.cshrc
2017-10-04 02:05:55 /root/.lesshst
2017-10-03 08:48:18 /root/.pki
2017-10-03 08:42:10 /root/.rnd
2013-12-29 11:26:31 /root/.tcshrc
2017-10-03 08:55:58 /root/.viminfo
2016-01-01 23:07:06 /root/anaconda-ks.cfg
2017-10-04 01:56:31 /root/test_dir
[root@BS-PUB-CENT7-01 ~]# ls -lad $PWD/{.,}* --time-style="+%F %T"|tr -s ' '|cut -d ' ' -f6-
2017-10-04 02:00:41 /root/.
2017-01-20 00:01:25 /root/..
2017-10-06 01:21:37 /root/.bash_history
2013-12-29 11:26:31 /root/.bash_logout
2013-12-29 11:26:31 /root/.bash_profile
2013-12-29 11:26:31 /root/.bashrc
2013-12-29 11:26:31 /root/.cshrc
2017-10-04 02:05:55 /root/.lesshst
2017-10-03 08:48:18 /root/.pki
2017-10-03 08:42:10 /root/.rnd
2013-12-29 11:26:31 /root/.tcshrc
2017-10-03 08:55:58 /root/.viminfo
2016-01-01 23:07:06 /root/anaconda-ks.cfg
2017-10-04 01:56:31 /root/test_dir
[root@BS-PUB-CENT7-01 ~]# ls -ladh $PWD/{.,}* --time-style="+%F %T"|tr -s ' '|cut -d ' ' -f5- # ついでにファイルサイズも取得しておく場合
4.0K 2017-10-04 02:00:41 /root/.
4.0K 2017-01-20 00:01:25 /root/..
12K 2017-10-06 01:21:37 /root/.bash_history
18 2013-12-29 11:26:31 /root/.bash_logout
176 2013-12-29 11:26:31 /root/.bash_profile
176 2013-12-29 11:26:31 /root/.bashrc
100 2013-12-29 11:26:31 /root/.cshrc
43 2017-10-04 02:05:55 /root/.lesshst
18 2017-10-03 08:48:18 /root/.pki
1.0K 2017-10-03 08:42:10 /root/.rnd
129 2013-12-29 11:26:31 /root/.tcshrc
3.6K 2017-10-03 08:55:58 /root/.viminfo
986 2016-01-01 23:07:06 /root/anaconda-ks.cfg
4.0K 2017-10-04 01:56:31 /root/test_dir

再帰的に取得するのであれば、findのprintfで出力項目を指定できるのでそれを使えばいい。

find ./ -type f -printf '%AF %AT %p\n'
find ./ -type f -printf '%AF %AT %s %p\n' # ついでにファイルサイズも取得する場合
find ./ -type f -printf '%AF %AT %s %p\n' | numfmt --to=iec --field=3 # さらについでにファイルサイズを人間が読みやすいよう加工(numfmtは比較的新し目のOSにしか無いので注意)
[root@BS-PUB-CENT7-01 ~]# find ./ -type f -printf '%AF %AT %p\n'
2016-01-11 16:09:15.9680000000 ./.bash_logout
2017-10-03 08:41:04.4160000000 ./.bash_profile
2017-10-03 08:41:04.4200000000 ./.bashrc
2013-12-29 11:26:31.0000000000 ./.cshrc
2013-12-29 11:26:31.0000000000 ./.tcshrc
2016-01-01 23:07:06.4890000000 ./anaconda-ks.cfg
2017-10-06 01:21:37.0940000000 ./.bash_history
2017-10-03 08:42:10.5770000000 ./.rnd
2017-10-03 08:55:58.1960000000 ./.viminfo
2017-10-04 01:55:59.8910000000 ./test_dir/test001/test.log
2017-10-04 01:55:59.8910000000 ./test_dir/test002/test.log
2017-10-04 01:55:59.8910000000 ./test_dir/test003/test.log
2017-10-04 01:55:59.8910000000 ./test_dir/test004/test.log
2017-10-04 01:55:59.8910000000 ./test_dir/test006/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test007/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test008/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test009/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test010/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test011/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test012/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test013/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test014/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test015/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test016/test.log
2017-10-05 09:23:22.3100000000 ./test_dir/test016/test.txt
2017-10-04 01:55:59.8920000000 ./test_dir/test017/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test018/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test019/test.log
2017-10-04 01:55:59.8920000000 ./test_dir/test020/test.log
2017-10-07 09:29:26.4900000000 ./.lesshst

で、Linuxの場合はこれでいいのだが、商用UNIX(AIXやらHP-UXやら)ではGNU拡張されてるようなコマンドは基本用意されていない。 このため、上記のようなコマンドでは出力ができない。

ではどうするかというと、そんなOSでもとりあえずperlは入っているのでそれらで対応するのがいいだろう。 以下、実行例(なお、環境はPolarhomeのものを使用している)。

ls -1d $PWD/* | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",$_'
ls -1d $PWD/* | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",((stat($_)))[7],"\t",$_' # ファイルサイズも取得する
find $PWD/ -type f | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",((stat($_)))[7],"\t",$_' # 再帰的に調べる

HP-UXの場合

-bash-4.2$ uname -a
HP-UX hpux-ia6 B.11.31 U ia64 0107668277 unlimited-user license
-bash-4.2$
-bash-4.2$ ls -1d $PWD/* | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",$_'
2014-12-07 12:17:25     /home/b/blacknon/20141207
2005-01-27 09:17:29     /home/b/blacknon/DISCLAMER
2015-04-26 16:35:29     /home/b/blacknon/aaa
2014-11-30 02:30:00     /home/b/blacknon/private
2014-11-30 02:30:00     /home/b/blacknon/public_html
2015-01-10 14:18:05     /home/b/blacknon/sar.test
2014-12-28 05:57:46     /home/b/blacknon/test
-bash-4.2$
-bash-4.2$ ls -1d $PWD/* | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",((stat($_)))[7],"\t",$_'
2014-12-07 12:17:25     96      /home/b/blacknon/20141207
2005-01-27 09:17:29     1208    /home/b/blacknon/DISCLAMER
2015-04-26 16:35:29     0       /home/b/blacknon/aaa
2014-11-30 02:30:00     96      /home/b/blacknon/private
2014-11-30 02:30:00     96      /home/b/blacknon/public_html
2015-01-10 14:18:05     290136  /home/b/blacknon/sar.test
2014-12-28 05:57:46     25      /home/b/blacknon/test
-bash-4.2$
-bash-4.2$ find $PWD/ -type f | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",((stat($_)))[7],"\t",$_'
2014-11-30 02:30:00     832     /home/b/blacknon/.cshrc
2014-11-30 02:30:00     334     /home/b/blacknon/.login
2014-11-30 02:30:00     700     /home/b/blacknon/.profile
2014-11-30 02:30:00     347     /home/b/blacknon/.exrc
2014-11-30 02:30:00     446     /home/b/blacknon/public_html/cgi-bin/test.cgi
2014-11-30 02:30:00     1710    /home/b/blacknon/public_html/index.html
2014-11-30 02:30:00     0       /home/b/blacknon/.forward
2014-11-30 02:30:00     2       /home/b/blacknon/.rhosts
2014-11-30 02:30:00     0       /home/b/blacknon/.plan
2014-11-30 02:30:00     265     /home/b/blacknon/.logout
2014-11-30 09:51:52     736     /home/b/blacknon/.ssh/id_dsa
2014-11-30 09:51:52     608     /home/b/blacknon/.ssh/id_dsa.pub
2014-11-30 09:51:52     608     /home/b/blacknon/.ssh/authorized_keys
2015-04-06 09:42:30     1326    /home/b/blacknon/.ssh/known_hosts
2015-04-26 16:35:33     1912    /home/b/blacknon/.sh_history
2017-10-07 01:30:53     1878    /home/b/blacknon/.bash_history
2014-11-30 09:53:28     55      /home/b/blacknon/.Xauthority
2014-12-28 05:57:46     25      /home/b/blacknon/test
2015-01-10 14:18:05     290136  /home/b/blacknon/sar.test
2015-04-26 16:35:29     0       /home/b/blacknon/aaa

AIXの場合

-bash-4.2$ uname -a
AIX aix7 1 7 000ACFDE4C00
-bash-4.2$
-bash-4.2$ ls -1d $PWD/* | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",$_'
2014-11-30 10:56:03     /home/b/blacknon/DISCLAMER
2017-10-03 16:55:32     /home/b/blacknon/aaa
2014-11-30 10:56:03     /home/b/blacknon/private
2014-11-30 10:56:03     /home/b/blacknon/public_html
2014-12-21 08:15:19     /home/b/blacknon/test.file
-bash-4.2$
-bash-4.2$ ls -1d $PWD/* | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",((stat($_)))[7],"\t",$_'
2014-11-30 10:56:03     1209    /home/b/blacknon/DISCLAMER
2017-10-03 16:55:32     0       /home/b/blacknon/aaa
2014-11-30 10:56:03     256     /home/b/blacknon/private
2014-11-30 10:56:03     256     /home/b/blacknon/public_html
2014-12-21 08:15:19     104857600       /home/b/blacknon/test.file
-bash-4.2$
-bash-4.2$ find $PWD/ -type f | perl -lne 'use POSIX;print POSIX::strftime("%F %T",localtime((stat($_))[9])),"\t",((stat($_)))[7],"\t",$_'
2014-11-30 11:17:38     50      /home/b/blacknon/.Xauthority
2017-10-05 17:54:40     4934    /home/b/blacknon/.bash_history
2014-11-30 10:56:03     1       /home/b/blacknon/.forward
2014-11-30 10:56:03     1       /home/b/blacknon/.plan
2014-11-30 10:56:03     511     /home/b/blacknon/.profile
2014-11-30 10:56:03     1       /home/b/blacknon/.rhosts
2014-11-30 10:56:03     3511    /home/b/blacknon/.screenrc
2014-11-30 10:56:03     3511    /home/b/blacknon/.screenrc.rpmorig
2014-12-21 08:21:40     244     /home/b/blacknon/.sh_history
2017-10-03 16:52:03     1403    /home/b/blacknon/.ssh/authorized_keys
2014-11-30 11:14:51     771     /home/b/blacknon/.ssh/id_dsa
2014-11-30 11:14:51     603     /home/b/blacknon/.ssh/id_dsa.pub
2017-10-03 16:52:51     1702    /home/b/blacknon/.ssh/id_rsa
2017-10-03 16:52:51     380     /home/b/blacknon/.ssh/id_rsa.pub
2014-11-30 10:56:03     1209    /home/b/blacknon/DISCLAMER
2017-10-03 16:55:32     0       /home/b/blacknon/aaa
2014-11-30 10:56:04     392     /home/b/blacknon/public_html/cgi-bin/test.cgi
2014-11-30 10:56:03     2683    /home/b/blacknon/public_html/index.html
2014-12-21 08:15:19     104857600       /home/b/blacknon/test.file

やっぱ商用UNIXは使いにくいなー…