简体   繁体   English

在具有空格的目录中查找上次修改的文件

[英]Find last modified file in a directory with white space

I found and tweaked this script to find the most recently modified file in a directory recursively. 我发现并调整了这个脚本,以递归方式查找目录中最近修改过的文件。 It only breaks when there is a space in the directory name. 它只在目录名称中有空格时才会中断。 Can anyone help me adjust the script so it will read directories with space also?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

Perl? Perl的? you don't have bash and you like writing long lines of code? 你没有bash,你喜欢写长行代码? ;-) ;-)

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

Quoting fixes everything. 引用修复了一切。

find "$i" -type f

Also, you don't need tail . 另外,你不需要tail Just swap $a and $b and exit after you print. 只需交换$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 }'

And -l (letter "ell") appends newlines for you when printing. 并且-l (字母“ell”)在打印时为您添加换行符。

Edit: 编辑:

Actually there's no need for a loop at all: 实际上根本不需要循环:

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]'

在Perl中编写所有内容似乎不那么混乱

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

由于您只处理当前目录,因此只能使用一个命令执行此操作:

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

By default the code below searches the subtree beneath the current working directory. 默认情况下,下面的代码搜索当前工作目录下的子树。 You can also name on the command line one more more subtrees to search. 您还可以在命令行上命名另外一个要搜索的子树。

#! /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;
}

As written, the code does not follow symbolic links. 如上所述,代码不遵循符号链接。 If you name a symlink on the command line, you will see output of 如果在命令行中命名符号链接,则会看到输出

$ ./find-newest ~/link-to-directory
./find-newest: no files processed.

With bash , you have to add a trailing slash to force the dereference. 使用bash ,您必须添加一个尾部斜杠以强制取消引用。

$ ./find-newest ~/link-to-directory/
Thu Jan  1 00:00:00 1970        hello-world

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

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