最後に変更された内容に基づいてディレクトリを並べ替える(再帰的)

最後に変更された内容に基づいてディレクトリを並べ替える(再帰的)

同じレベルに複数のディレクトリがあり、その中の内容の最後の変更日に基づいて(再帰的に)ソートしたいと思います。ただし、Nautilusでは、ディレクトリの「最後の変更日」は、内部的に新しいファイルが作成されたときにのみ更新されるように見えます。

これらのディレクトリの再帰的な「最後の変更日」を表示する方法はありますか?


編集:日付を最も近い分まで知ることができます。それで私はStéphane Chazelasの解決策を使って混乱を減らすためにいくつかの小さな修正をしました。

find . -mindepth 2 -type f -printf '%TF %TH:%TM/%P\0' |
LC_ALL=C sort -zt/ -k2,2 -k1,1r |
LC_ALL=C sort -t/ -zmsuk2,2 |
LC_ALL=C sort -z |
cut -zd/ -f1,2 | tr '\0/' '\n\t'

答え1

ディレクトリの最後の変更時刻(例:電話帳、いいえフォルダ)は、ディレクトリ内のエントリが削除、追加、または編集された時刻など、最後に変更された時刻です。

その中の最新の一般ファイルを再帰的に見つけるには、そのディレクトリとその中のすべてのファイルの内容を読み、ファイルの修正時間を確認する必要があります。これは高価な作業であり、どのファイルマネージャアプリでもこれを行うことは期待できません。

しかし、スクリプトを書くことはできます。

findsortBourneに似たシェルのGNU実装では、次のことができます。

TZ=UTC0 find . -mindepth 2 -type f -printf '%TFZ%TT/%P\0' |
  LC_ALL=C sort -zt/ -k2,2 -k1,1r |
  LC_ALL=C sort -t/ -zmsuk2,2 |
  LC_ALL=C sort -z |
  tr '\0' '\n'

これにより、次の内容が提供されます。

2020-02-08Z19:17:22.3588966190/Scripts/.distfiles
2020-02-09Z09:25:37.5336986350/StartupFiles/zshrc
2020-07-26Z20:33:17.7263164070/Misc/vcs_info-examples
2020-07-26Z20:33:17.7463157170/Util/ztst-syntax.vim
2020-08-22Z18:06:17.9773156630/Functions/VCS_Info
2020-08-30Z11:11:00.5701005930/autom4te.cache/requests
2020-08-30Z11:11:31.5245491550/Config/defs.mk
2020-08-30Z11:11:31.6085449480/Etc/Makefile
2020-08-30Z11:12:10.9305773600/INSTALL.d/share/zsh/5.8.0.2-dev/help
2020-10-22Z05:17:15.3808945480/Completion/Base/Utility
2020-10-22Z05:17:15.3928938520/Doc/Zsh/zle.yo
2020-10-22Z05:17:15.3968936190/Src/zsh.h
2020-10-22Z05:17:15.3968936190/Test/D02glob.ztst
2020-10-22Z05:17:15.4168924590/.git/logs/refs/heads/master

つまり、各ディレクトリの最新の一般ファイルと対応するタイムスタンプが提供されます。一般ファイルのないディレクトリは表示されません。

ディレクトリリストのみを表示するには、cut -zd/ -f2 |コマンドの前にを挿入しますtr

zsh メソッドのようなよりクールな出力のために、trコマンドを次に置き換えることができます。

LC_ALL=C gawk -v RS='\0' -F / '{
  dir = $2; mtime = $1
  sub("[^/]*/[^/]*/", "")
  printf "%-20s %s (%s)\n", dir, mtime, $0}'

以下を使用すると、タイムスタンプを10進Unix epoch時間で印刷し、現地時間に再フォーマットするgawkこともできます。findgawk

find . -mindepth 2 -type f -printf '%T@/%P\0' |
  LC_ALL=C sort -zt/ -k2,2 -k1,1rn |
  LC_ALL=C sort -t/ -zmsuk2,2 |
  LC_ALL=C sort -zn |
  LC_ALL=C gawk -v RS='\0' -F / '{
    dir = $2; split($1, mtime, ".")
    sub("[^/]*/", "")
    printf "%-20s %s (%s)\n", dir, strftime("%FT%T." mtime[2] "%z", mtime[1]), $0}'

これにより、次の出力が提供されます。

cross-build          2019-12-02T13:48:33.0505299150+0000 (cross-build/x86-beos.cache)
m4                   2019-12-02T13:48:33.4615093990+0000 (m4/xsize.m4)
autom4te.cache       2019-12-02T13:50:48.8897482560+0000 (autom4te.cache/requests)
CWRU                 2020-08-09T17:17:21.4712835520+0100 (CWRU/CWRU.chlog)
include              2020-08-09T17:17:21.5872807740+0100 (include/posixtime.h)
tests                2020-08-09T17:17:21.8392747400+0100 (tests/type.right)
.git                 2020-08-09T17:17:21.8472745490+0100 (.git/index)
doc                  2020-08-09T17:35:35.1638603570+0100 (doc/Makefile)
po                   2020-08-09T17:35:35.3758514290+0100 (po/Makefile)
support              2020-08-09T17:35:36.7037954930+0100 (support/man2html)
lib                  2020-08-09T17:35:42.3755564970+0100 (lib/readline/libhistory.a)
builtins             2020-08-09T17:35:42.5035511020+0100 (builtins/libbuiltins.a)
examples             2020-08-09T17:35:47.1513551370+0100 (examples/loadables/cut)
INSTALL.d            2020-08-09T17:35:47.3993446790+0100 (INSTALL.d/lib/bash/cut)

答え2

次のソート順序関数をzsh定義できますby_age_of_newest_file

zmodload zsh/stat
typeset -A newest_mtime newest_file
by_age_of_newest_file() {
  local dir=${1-$REPLY}
  local newest=($dir/**/*(ND.om[1]))

  if (($#newest)); then
    stat -gLA REPLY -F %FZ%T.%N +mtime -- $newest
    newest_mtime[$dir]=$REPLY newest_file[$dir]=$newest
  else
    REPLY= newest_mtime[$dir]= newest_file[$dir]=
  fi
}

次のように使用できます。

print -rC1 -- *(ND/o+by_age_of_newest_file)

最も古いものから最新のものの順にリストを印刷します(ファイルのないディレクトリが最初にリストされます)。順序を変更するo+にはに置き換えます。O+

または、最新のファイルとそのタイムスタンプでディレクトリを印刷します。

data=()
for dir (*(ND/o+by_age_of_newest_file))
  data+=("$dir" "$newest_mtime[$dir]" "($newest_file[$dir])")
print -raC3 -- "$data[@]"

これは次のようなものを提供します。

Scripts                        2020-02-08Z19:17:22.358896619  (Scripts/.distfiles)
StartupFiles                   2020-02-09Z09:25:37.533698635  (StartupFiles/zshrc)
Misc                           2020-07-26Z20:33:17.726316407  (Misc/vcs_info-examples)
Util                           2020-07-26Z20:33:17.746315717  (Util/ztst-syntax.vim)
Functions                      2020-08-22Z18:06:17.977315663  (Functions/VCS_Info/Backends/VCS_INFO_get_data_hg)
autom4te.cache                 2020-08-30Z11:11:00.570100593  (autom4te.cache/requests)
Config                         2020-08-30Z11:11:31.524549155  (Config/defs.mk)
Etc                            2020-08-30Z11:11:31.608544948  (Etc/Makefile)
INSTALL.d                      2020-08-30Z11:12:10.870580360  (INSTALL.d/share/zsh/5.8.0.2-dev/help/zstyle)
Completion                     2020-10-22Z05:17:15.380894548  (Completion/Base/Utility/_store_cache)
Doc                            2020-10-22Z05:17:15.392893852  (Doc/Zsh/zle.yo)
Src                            2020-10-22Z05:17:15.396893619  (Src/zsh.h)
Test                           2020-10-22Z05:17:15.396893619  (Test/D02glob.ztst)
.git                           2020-10-22Z05:17:15.416892459  (.git/logs/refs/heads/master)

(タイムスタンプはZulu / UTC時間であることを参照してください)

関連情報