在当前目录中查找按修改时间排序的文件,并将结果存储在数组中 [英] Find files in current directory sorted by modified time and store result in an array

查看:84
本文介绍了在当前目录中查找按修改时间排序的文件,并将结果存储在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以告诉我在执行以下shell函数时我在做什么错吗?

Can somebody tell me what am I doing wrong in following shell function?

NOTES_PATH="/home/temp"

function open_note {

    local files=()                                                          
    while IFS=  read -r -d $'\0' filename; do                                        
        files+=("$filename")                                                   
    done < <(find "$NOTES_PATH" -maxdepth 1 -type f -print0 | xargs -0 ls -t)

    echo "There is/are ${#files[@]} file(s) available"
}

即使在/home/temp目录中可以看到2个文本文件,我每次都会得到有0个文件可用".

I get "There is/are 0 file(s) available" everytime even though I can see 2 text files in /home/temp directory.

请注意,我想使用while循环方法来读取文件名,除非没有实现我想做的事情,否则不能以任何其他方式使用. [学分: John1024 回答]

Please note that I want to use while loop approach to read filenames, not any other way unless I cannot achieve what I want to do. [credits: John1024 answer]

P.S.如果我从命令行运行find /home/temp -maxdepth 1 -type f -print0 | xargs -0 ls -t,则会得到预期的结果.

P.S. If I run find /home/temp -maxdepth 1 -type f -print0 | xargs -0 ls -t from command line, I get expected result.

推荐答案

原始代码有几个错误:

  • 由于ls -t的输出不是NUL分隔的,因此丢失了xargs -0之前的NUL分隔内容的优点.
  • 由于xargs将结果分成多个ls -t调用,因此仅在这些批处理中进行排序:如果有足够的文件名需要两次或多次调用,则这些调用将仅进行单独排序,而不是全局排序.
  • ls的解析输出为通常容易出错的.
  • The advantages of NUL-delimiting content prior to xargs -0 were lost, because the output of ls -t was not NUL-delimited.
  • Because xargs split results into multiple ls -t invocations, sorting took place only within these batches: Given enough filenames to require two or more invocations, those invocations would only be individually sorted, not globally sorted.
  • Parsing output of ls is generally error-prone.

更干净的实现(假设使用GNU find和GNU排序)如下所示:

A cleaner implementation (assuming GNU find and GNU sort) would look like the following:

open_note() {
  local filename mtime
  local -a files=( )

  while IFS= read -r -d' ' mtime && IFS= read -r -d '' filename; do
    files+=( "$filename" )
  done < <(find "$NOTES_PATH" -maxdepth 1 -type f -printf '%T@ %P\0' | sort -z -n)
}

给出的find动作发出以下格式的流:

The find action given emits a stream of the format:

<epoch time> <filename><NUL>

...然后对sort进行排序,然后while read循环将丢弃每个字段的第一部分.

...which sort then sorts on, after which the while read loop discards the first part of each field.

其他一些注意事项:

  • 请勿使用function关键字;它使您的代码与POSIX sh完全不兼容,但是(与其他更有用的bashisms不同)与合规语法相比没有任何优势.
  • 如果您要限制本地人的范围,请确保您了解全部 ,其中包括循环使用的名称.
  • Don't use the function keyword; it makes your code gratuitously incompatible with POSIX sh, but (unlike other, more useful bashisms) adds no advantage whatsoever over the compliant syntax.
  • If you're trying to keep your locals scoped, be sure that you get all of them -- that includes names being looped over.

这篇关于在当前目录中查找按修改时间排序的文件,并将结果存储在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆