在当前目录中查找按修改时间排序的文件,并将结果存储在数组中 [英] Find files in current directory sorted by modified time and store result in an array
问题描述
有人可以告诉我在执行以下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 ofls -t
was not NUL-delimited. - Because
xargs
split results into multiplels -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屋!