このエントリーをはてなブックマークに追加


findやgrepで検索した結果をファイル日付でソートする方法

findやgrepで該当するファイルを検索し出力された内容をファイルのタイムスタンプでソートしたい場合の例を以下に記します。


関連記事

findの場合

findで検索した内容をファイルのタイムスタンプでソートしたい場合は、xargsとlsを使えば簡単に実現できます。
以下に実行例を記します。

フィアルサイズが4M以上のファイルをfindで検索し、lsコマンドでソートした例です。

ls -lt

tオプションでタイムスタンプでソートしています。

sakura@hpmini:~$ find /usr/bin/ -size +4M | xargs ls -lt
-rwxr-xr-x 1 root root 9612924 12月 14 22:33 /usr/bin/php5
-rwxr-xr-x 1 root root 6575456  7月  2  2016 /usr/bin/gimp-2.8
-rwxr-xr-x 1 root root 4340528 11月  8  2014 /usr/bin/aptitude-curses
-rwxr-xr-x 2 root root 4595016 10月  8  2014 /usr/bin/python3.4
-rwxr-xr-x 2 root root 4595016 10月  8  2014 /usr/bin/python3.4m

ls -ltr

rオプションを追加しリバース(r)で表示した例です。

sakura@hpmini:~$ find /usr/bin/ -size +4M | xargs ls -ltr
-rwxr-xr-x 2 root root 4595016 10月  8  2014 /usr/bin/python3.4m
-rwxr-xr-x 2 root root 4595016 10月  8  2014 /usr/bin/python3.4
-rwxr-xr-x 1 root root 4340528 11月  8  2014 /usr/bin/aptitude-curses
-rwxr-xr-x 1 root root 6575456  7月  2  2016 /usr/bin/gimp-2.8
-rwxr-xr-x 1 root root 9612924 12月 14 22:33 /usr/bin/php5

grepで検索したファイル一覧をタイムスタンプでソートする

grepも同様の方法で実現できます。

以下、grepでdebianが含まれるファイル一覧を取得しています。

sakura@hpmini:~$ grep -ilr debian /etc/X11/
/etc/X11/Xsession.d/70im-config_launch
/etc/X11/app-defaults/KOI8RXTerm-color
/etc/X11/app-defaults/XTerm-color
/etc/X11/app-defaults/UXTerm-color
/etc/X11/app-defaults/XTerm
/etc/X11/Xsession
/etc/X11/Xwrapper.config
/etc/X11/rgb.txt

xargsとlsを使いタイムスタンプでソートしてみます。

ls -lt

tオプションでタイムスタンプでソートしています。

sakura@hpmini:~$ grep -ilr debian /etc/X11/ | xargs ls -lt
-rw-r--r-- 1 root root   601 11月 25 20:59 /etc/X11/Xwrapper.config
-rw-r--r-- 1 root root  6217  3月  1  2015 /etc/X11/app-defaults/KOI8RXTerm-color
-rw-r--r-- 1 root root  6209  3月  1  2015 /etc/X11/app-defaults/UXTerm-color
-rw-r--r-- 1 root root 10201  3月  1  2015 /etc/X11/app-defaults/XTerm
-rw-r--r-- 1 root root  6207  3月  1  2015 /etc/X11/app-defaults/XTerm-color
-rw-r--r-- 1 root root  1497  3月 30  2014 /etc/X11/Xsession.d/70im-config_launch
-rwxr-xr-x 1 root root  3517  9月 26  2013 /etc/X11/Xsession
-rw-r--r-- 1 root root 17394  9月 26  2013 /etc/X11/rgb.txt

ls -ltr

rオプションを追加しリバース(r)で表示した例です。

sakura@hpmini:~$ grep -ilr debian /etc/X11/ | xargs ls -ltr
-rw-r--r-- 1 root root 17394  9月 26  2013 /etc/X11/rgb.txt
-rwxr-xr-x 1 root root  3517  9月 26  2013 /etc/X11/Xsession
-rw-r--r-- 1 root root  1497  3月 30  2014 /etc/X11/Xsession.d/70im-config_launch
-rw-r--r-- 1 root root  6207  3月  1  2015 /etc/X11/app-defaults/XTerm-color
-rw-r--r-- 1 root root 10201  3月  1  2015 /etc/X11/app-defaults/XTerm
-rw-r--r-- 1 root root  6209  3月  1  2015 /etc/X11/app-defaults/UXTerm-color
-rw-r--r-- 1 root root  6217  3月  1  2015 /etc/X11/app-defaults/KOI8RXTerm-color
-rw-r--r-- 1 root root   601 11月 25 20:59 /etc/X11/Xwrapper.config

以上、findとgrepのファイル一覧をファイルのタイムスタンプでソートして表示する方法でした。



トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 検索 最終更新   ヘルプ   最終更新のRSS
Last-modified: 2017-02-07 (火) 12:52:25