简体   繁体   English

从 find 命令的目录列表中查找最近的文件

[英]Finding most recent file from a list of directories from find command

I use find . -type d -name "Financials"我使用find . -type d -name "Financials" find . -type d -name "Financials" to find all the directories called "Financials" under the current directory. find . -type d -name "Financials"查找当前目录下所有名为 "Financials" 的目录。 Since I am on Mac, I can use the following (which I found from another stackoverflow question) to find the latest modified file in my current directory: find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "由于我在 Mac 上,我可以使用以下内容(我从另一个 stackoverflow 问题中找到的)在当前目录中find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "最新修改的文​​件: find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" " find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" " find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" " . find . -type f -print0 | xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" " What I would like to do is find a way to pipe the results of the first command into the second command--ie to find the most recently modified file in each "Financials" directory.我想要做的是找到一种方法将第一个命令的结果通过管道传输到第二个命令中——即在每个“Financials”目录中找到最近修改的文件。 Is there a way to do this?有没有办法做到这一点?

I think you could:我认为你可以:

find . -type d -name "Financials" -print0 |
xargs -0 -I{} find {} -type f -print0 |
xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "

But if you want separately for each dir, then... why not just loop it:但是如果你想分别为每个目录,那么......为什么不循环它:

find . -type d -name "Financials" |
while IFS= read -r dir; do
   echo "newest file in $dir is $(
       find "$dir" -type f -print0 |
       xargs -0 stat -f "%m %N" | sort -rn | head -1 | cut -f2- -d" "
   )"
done

Nest the 2nd file+xargs inside a first find+xargs:将第二个文件+xargs 嵌套在第一个 find+xargs 中:

find . -type d -name "Financials" -print0 \
| xargs -0 sh -c '
    for d in "$@"; do
        find "$d" -type f -print0 \
        | xargs -0 stat -f "%m %N" \
        | sort -rn \
        | head -1 \
        | cut -f2- -d" "
    done
' sh

Note the trailing "sh" in sh -c '...' sh -- that word becomes "$0" inside the shell script so the for-loop can iterate over all the directories.请注意sh -c '...' sh的尾随“sh”——该词在 shell 脚本中变为“$0”,因此 for 循环可以遍历所有目录。

A robust way that will also avoid problems with funny filenames that contain special characters is:还可以避免包含特殊字符的有趣文件名问题的一种可靠方法是:

  1. find all files within this particular subdirectory, and extract the inode number and modifcation time查找此特定子目录中的所有文件,并提取 inode 编号和修改时间

    $ find . -type f -ipath '*/Financials/*' -printf "%T@ %i\\n"
  2. extract youngest file's inode number提取最年轻的文件的 inode 号

    $ ... | awk '($1>t){t=$1;i=$2}END{print i}'
  3. search file information by inode number按 inode 编号搜索文件信息

    $ find . -type f -inum "$inode" '*/Financials/*'

So this gives you:所以这给了你:

  $ inode="$(find . -type f -ipath '*/Financials/*' -printf "%T@ %i\n" | awk '($1>t){t=$1;i=$2}END{print i}')"
  $ find . -type f -inum "$inode" '*/Financials/*'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM