0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

サブディレクトリも含め、ls -lの結果を数件ずつ取りたい

0
Posted at

サブディレクトリも含めて再帰的にファイルを取得するのはls -Rfind -maxdepthで指定すればいけますが、ディレクトリの一部だけ(直近のタイムスタンプだけ確認したい)ということがありました。
何か良いオプション無いかと見てみたけど、無さそうだったのでxargsと組み合わせてみます。


find . -type d -maxdepth 5 | xargs -I{} sh -c 'echo {} ================== ; ls -ltr {} | head -n 3 '

maxdepthやlsのオプションは大体適当です。
状況に応じて変えていきます。

結果
. ==================
total 0
drwxr-xr-x  4 user  staff  128 Dec  1 22:55 test
drwxr-xr-x  2 user  staff   64 Dec  1 22:55 moge
./moge ==================
./test ==================
total 0
drwxr-xr-x  3 user  staff  96 Dec  1 22:53 test2
-rw-r--r--  1 user  staff   0 Dec  1 22:53 hoge1
./test/test2 ==================
total 0
-rw-r--r--  1 user  staff  0 Dec  1 22:53 hogeFile
./moga ==================
total 0
drwxr-xr-x  6 user  staff  192 Dec  1 22:56 aaa
./moga/aaa ==================
total 0
drwxr-xr-x  4 user  staff  128 Dec  1 22:55 bbb
-rw-r--r--  1 user  staff    0 Dec  1 22:56 file1
./moga/aaa/bbb ==================
total 0
-rw-r--r--  1 user  staff  0 Dec  1 22:55 aiueo
-rw-r--r--  1 user  staff  0 Dec  1 22:55 kakiku

直接の処理には関係ないですが、無いとパッと見た感じ見づらかったので、見出し代わりにechoを入れています。
このあたりは出力の関係上要らないという場合もありそうなので、お好みで。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?