找到最新的修改文件和exec [英] find the latest modified file and exec
问题描述
我想找出目录中最新的内置rpm,然后在其上执行一些操作。类似于下面的东西。
/ bin / ls -1t srcdir / *。rpm |头-1
但是使用find命令
find srcdir / *。rpm<只读最近修改的文件> -exec< do-something>
两种方法 - 一种可移植到非GNU平台,另一个快速的大目录(在文件系统允许的范围内):
$ b $ h1便携式$ / b $
$ b
什么?
BashFAQ#3:如何根据某些元数据属性(最新/最旧修改时间,大小等)对文件进行排序或比较?与此处相关。总结:
latest =
for$ srcdir/ *。rpm;做
[$ f-nt$最新]&&最新= $ f
完成
如何?
[$ a-nt$ b]
检查变量 a
比在ksh派生的shell中的变量 b
更新。
快速
在非常大的目录下更快,而不是在所有情况下更快。也就是说,它也很容易找到(例如)5或10个最新的文件,或者除了那些5或10个最新的文件之外的所有文件,而另一种方法几乎不能有效地完成。如果你有GNU工具(GNU find,GNU sort),请考虑以下内容:
{read -r -d''mtime&& IFS =读取-r -d''文件名; } \
pre>
< <(find / directory -type f -iname* .rpm-printf'%T @%p \ 0'| sort -z -r -n)
这会将最新文件的时间(以秒为单位)放在shell变量
mtime
中,该文件的名称在filename
中。因此,您可以对该文件进行操作:
if [[-e $ filename]];那么
#可以对你要找的文件进行任意的操作
cp - $ filename/ path / to / where / to / copy
fi
如何?
<find ... -printf'%T @%p\0'
...以
< epoch_mtime> < code>< NUL>
,其中epoch_mtime
是自1970年1月1日以来的秒数。
sort -z -r -n
...然后对输出进行排序,期望它被NUL分隔,数字在开头。
{ read -r -d''mtime&& IFS =读取-r -d''文件名; }
...将内容读入
mtime
变量,直到第一行的第一个空格,然后向前读取第一个NUL到filename
变量。I want to find out the latest built rpm in a directory and then exec something on it. Something similar to below.
/bin/ls -1t srcdir/*.rpm | head -1
But with the find command
find srcdir/*.rpm <get-only-the-most-recently-modified-file> -exec "<do-something>"
解决方案Two approaches -- one portable to non-GNU platforms, the other fast with large directories (to the extent allowed by the filesystem):
Portable
What?
BashFAQ #3: How can I sort or compare files based on some metadata attribute (newest / oldest modification time, size, etc)? is relevant here. To summarize:
latest= for f in "$srcdir"/*.rpm; do [ "$f" -nt "$latest" ] && latest=$f done
How?
[ "$a" -nt "$b" ]
checks whether the file named in variablea
is newer than that named in variableb
in ksh-derived shells.
Fast
...to be clear, this is faster with very large directories, as opposed to faster in all cases. That said, it's also easily adapted to find (for instance) the 5 or 10 newest files, or all files except those 5 or 10 newest, which the other approach could not do nearly as effectively.
What?
If you have GNU tools (GNU find, GNU sort), consider the following:
{ read -r -d ' ' mtime && IFS= read -r -d '' filename; } \ < <(find /directory -type f -iname "*.rpm" -printf '%T@ %p\0' | sort -z -r -n)
This will put your latest file's time (in seconds-since-epoch) in the shell variable
mtime
and the name of that file infilename
. Thus, you can then operate on that file:if [[ -e $filename ]]; then # do whatever arbitrary operations you're looking for on that resulting filename cp -- "$filename" /path/to/where/to/copy fi
How?
To explain how this works:
find ... -printf '%T@ %p\0'
...emits contents in the format
<epoch_mtime> <filename><NUL>
, whereepoch_mtime
is the number of seconds since January 1st, 1970.sort -z -r -n
...then sorts that output, expecting it to be NUL-delimited, on the numbers at the beginning.
{ read -r -d ' ' mtime && IFS= read -r -d '' filename; }
...reads content into the
mtime
variable up to the first space in the first line, and then reads forward to the first NUL into thefilename
variable.这篇关于找到最新的修改文件和exec的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!