如何查看上次创建/修改文件的最后 5 行?

Ash*_*ish 6 shell wildcards timestamps files

每天我都必须在特定时间进行备份和更新状态以支持团队,为此我需要检查*.aff目录中最后创建/修改的文件的最后 5 行并更新它们。

任何人都可以让我知道如何在 linux 中查看最后修改的文件(特定扩展名)的最后 5 行,就像*.aff文件一样?还创建了其他文件,例如*.log,等等。

Sté*_*las 7

随着zsh壳:

tail -n 5 ./*.aff(D.om[1])
Run Code Online (Sandbox Code Playgroud)

对于其他 shell,如果您不想对文件名可能包含的内容进行假设,则很难想出可靠的东西。

例如bash,如果您使用的是最新的 GNU 系统,则等效项为:

find . -maxdepth 1 -name '*.aff' -type f -printf '%T@:%p\0' |
  sort -rzn |
  sed -zn 's/[^:]*://p;q' |
  xargs -r0 tail -n 5
Run Code Online (Sandbox Code Playgroud)

或者:

find . -maxdepth 1 -name '*.aff' -type f -printf '%T@/%p\0' |
  sort -rzn | (IFS=/ read -rd '' mtime file && tail -n 5 "$file")
Run Code Online (Sandbox Code Playgroud)


Cos*_*tas 5

假设文件名不包含换行符并且所有*.aff文件都是常规文件:

ls -t1d -- *.aff | head -n 1
Run Code Online (Sandbox Code Playgroud)

为您提供最近修改的.aff文件的名称。如果您想要最后 5 行,请执行以下操作:

tail -n 5 -- "$(ls -t1d -- *.aff | head -n 1)"
Run Code Online (Sandbox Code Playgroud)