简体   繁体   English

查找最后修改的文件,并在变量中获取结果

[英]Find Last Modified File and get the result in a variable

I need to search for a specific .iso file in a Directory. 我需要在目录中搜索特定的.iso文件。 This one is the last one modified, and I will have only one result returned. 这是最后一个修改的内容,我将只返回一个结果。 I want this result to be stored in a Variable. 我希望将此结果存储在变量中。

My script then contains : 然后,我的脚本包含:

$MYFILE = find . -type f -name '*.iso' -printf '%p\\n' | sort -r | head -n 1

But this gives a file which is not up to date (22 of August, instead of 14 of October for instance) ... 但这给出的文件不是最新的(例如8月22日,而不是10月14日)...

Is there anything wrong with that way to go ? 这种方式有什么问题吗? I don't quite understand why I got a file that is neither the last one nor the first. 我不太明白为什么我得到的文件既不是最后一个也不是第一个。

"Find" does not sort the files and "sort -r" will only do a reverse alphabetical sort. “查找”不对文件进行排序,“排序-r”仅对字母进行反向排序。

If you print the modification time with find, you will be able to sort on modification time and extract the file name (even if the files are in different sub directories): 如果使用find打印修改时间,则可以对修改时间进行排序并提取文件名(即使文件位于不同的子目录中):

MYFILE=$(find -name "*.iso" -type f -printf "%Ts %p\n" | sort -n | tail -1 | sed -r -e 's/^[0-9]+ //')

I guess your sort -r does alphabetical sort on file names and not based on modified time. 我想您的sort -r会按字母顺序对文件名进行排序,而不是基于修改后的时间。

Try this: 尝试这个:

find . -name "*.iso" -type f | xargs ls -tr | tail -1

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

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