4

我找到并调整了这个脚本,以递归方式在目录中找到最近修改的文件。仅当目录名称中有空格时才会中断。任何人都可以帮我调整脚本,以便它也可以读取带有空格的目录吗?

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
4

5 回答 5

3

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

find . -type f -printf '%T+ %p\n' | sort -r | head -n1
于 2012-09-19T08:53:36.563 回答
2

报价可以解决一切。

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]'
于 2012-05-22T17:41:06.123 回答
1

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

perl -MFile::Find -e 'find(sub{@f=((stat)[9],$File::Find::name) if -f && $f[0]<(stat)[9]},".");print "@f")'
于 2012-05-22T18:18:06.473 回答
0

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

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
于 2012-05-22T17:42:04.800 回答
0

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

#! /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
于 2012-05-22T19:22:00.107 回答