【问题标题】:Find last modified file in a directory with white space在有空格的目录中查找最后修改的文件
【发布时间】:2012-05-29 05:54:07
【问题描述】:

我找到并调整了这个脚本,以递归方式在目录中查找最近修改的文件。仅当目录名称中有空格时才会中断。谁能帮我调整脚本,让它也能读取有空格的目录吗?s

for i in *; do

find $i -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail -1

done

【问题讨论】:

    标签: macos perl bash find


    【解决方案1】:

    引用可以解决一切问题。

    find "$i" -type f
    

    另外,您不需要tail。只需交换 $a$b 并在打印后退出。

    find $i -type f | perl -lne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($b))[$p] <=> (stat($a))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f; exit }'
    

    -l(字母“ell”)在打印时会为您附加换行符。

    编辑:

    其实根本不需要循环:

    find  -type f | perl -lne 'chomp(@files = <>); my $p = 9; @files = sort { (stat($b))[$p] <=> (stat($a))[$p] } @files; print scalar localtime((stat($files[0]))[$p]), "\t", $files[0]'
    

    【讨论】:

    • 感谢您的快速回复。我肯定会尝试这种方式而没有循环。我实际上设法通过在 for 循环之前添加 IFS='\n' 来解决这个问题。
    • 实际上我认为我自己的解决方案不起作用。再次感谢您的意见,我会尽力让您的建议发挥作用。
    • 好的,再次感谢您!添加引号给了我我需要的结果。
    【解决方案2】:

    由于您只处理当前目录,因此只需一个命令即可:

    find . -type f | perl -ne 'chomp(@files = <>); my $p = 9; foreach my $f (sort { (stat($a))[$p] <=> (stat($b))[$p] } @files) { print scalar localtime((stat($f))[$p]), "\t", $f, "\n" }' | tail -1
    

    【讨论】:

      【解决方案3】:

      全部用 Perl 编写似乎不那么混乱

      perl -MFile::Find -e 'find(sub{@f=((stat)[9],$File::Find::name) if -f && $f[0]<(stat)[9]},".");print "@f")'
      

      【讨论】:

        【解决方案4】:

        默认情况下,下面的代码搜索当前工作目录下的子树。您还可以在命令行中命名更多要搜索的子树。

        #! /usr/bin/env perl
        
        use strict;
        use warnings;
        
        use File::Find;
        
        my($newest_mtime,$path);
        sub remember_newest {
          return if -l || !-f _;
          my $mtime = (stat _)[9];
          ($newest_mtime,$path) = ($mtime,$File::Find::name)
            if !defined $newest_mtime || $mtime > $newest_mtime;
        }
        
        @ARGV = (".") unless @ARGV;
        for (@ARGV) {
          if (-d) {
            find \&remember_newest, @ARGV;
          }
          else {
            warn "$0: $_ is not a directory.\n";
          }
        }
        
        if (defined $path) {
          print scalar(localtime $newest_mtime), "\t", $path, "\n";
        }
        else {
          warn "$0: no files processed.\n";
          exit 1;
        }
        

        正如所写,代码不遵循符号链接。如果您在命令行上命名符号链接,您将看到输出

        $ ./find-newest ~/link-to-directory
        ./find-newest: 未处理任何文件。

        使用 bash,您必须添加尾部斜杠来强制取消引用。

        $ ./find-newest ~/link-to-directory/
        1970 年 1 月 1 日星期四 00:00:00 hello-world

        【讨论】:

          【解决方案5】:

          Perl?您没有 bash 并且喜欢编写长行代码? ;-)

          find . -type f -printf '%T+ %p\n' | sort -r | head -n1
          

          【讨论】:

          • OS X 的 find 没有 -printf 选项。不过,您可以使用 find . -exec stat -f "%m %N" {} \; | sort -n | tail -1 之类的东西。
          • 啊。是的,我原来的 exec 也有类似的用法。相信sort -r -n | head -1 还会更有效率吗? (现在分头发!)
          猜你喜欢
          • 2016-08-01
          • 2011-01-05
          • 1970-01-01
          • 2020-11-24
          • 2016-04-03
          • 1970-01-01
          • 2011-06-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多