简体   繁体   English

找到最新的修改文件并执行

[英]find the latest modified file and exec

I want to find out the latest built rpm in a directory and then exec something on it. 我想在目录中找到最新的内置rpm,然后在其上执行某些操作。 Something similar to below. 类似于以下内容。

/bin/ls -1t srcdir/*.rpm | head -1 

But with the find command 但是用find命令

find srcdir/*.rpm <get-only-the-most-recently-modified-file> -exec "<do-something>"

If you just wish to see the latest rpm : 如果您只想查看最新的rpm:

ls -1tr /pat/to/directory/*.rpm | tail -n 1

is good. 很好。 But since [ ls output shouldn't be parsed ] , you need to look to find for a remedy if you plan to use the file for some purpose as is in your case. 但由于[LS输出不应该被解析] ,你需要寻找到find了补救措施,如果你打算使用该文件对某些目的,你的情况。

My Solution would be : 我的解决方案是:

#!/bin/bash
declare -i lts # latest time stamp
declare -i cts # current time stamp
declare latestrpmfile="No file available"
lts=0
while read -r -d '' fname
do
  cts="$( stat --printf=%Y "$fname" )"
if [ "$cts" -gt "$lts" ]
then
  lts="$cts"
  latestrpmfile="$fname"
fi
done < <( find /directory/to/rpm/files/ -type f -iname "*.rpm" -print0 )

#At this point $latestrpmfile will contain the full path to the 
#latest rpm file
#Now, say, to copy the latest rpm file to a directory, do
if [ -e "$latestrpmfile" ]
then
  cp "$latestrpmfile" /path/to/where/to/copy/
fi

Problem specific solution 问题特定的解决方案

lbt=0 #latest build time
lb="nosuchfile" #latest build
for i in "$srcdir"/*.rpm # source dir could be an argument to script
do
bt=$(rpm -q --queryformat "%{BUILDTIME}" -p "$i" 2>/dev/null)
if [ "$bt" -gt "$lbt" ]
then
  lbt=$bt
  lb="$i"
fi
done
#Do something with "$lb"

Two approaches -- one portable to non-GNU platforms, the other fast with large directories (to the extent allowed by the filesystem): 两种方法-一种可移植到非GNU平台,另一种可使用大型目录(在文件系统允许的范围内)快速:


Portable 手提

What? 什么?

BashFAQ #3: How can I sort or compare files based on some metadata attribute (newest / oldest modification time, size, etc)? BashFAQ#3:如何基于某些元数据属性(最新/最旧的修改时间,大小等)对文件进行排序或比较? 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 variable a is newer than that named in variable b in ksh-derived shells. [ "$a" -nt "$b" ]在ksh派生的shell中检查以变量a命名的文件是否比以变量b命名的文件新。


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. 也就是说,它还可以轻松地找到(例如)5个或10个最新文件,或者找到 5个或10个最新文件之外的所有文件,而其他方法几乎无法有效地做到这一点。

What? 什么?

If you have GNU tools (GNU find, GNU sort), consider the following: 如果您具有GNU工具(GNU查找,GNU排序),请考虑以下事项:

{ 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 in filename . 这会将最新文件的时间(以秒为单位,以秒为单位)放入shell变量mtime ,并将该文件的名称放入filename 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> , where epoch_mtime is the number of seconds since January 1st, 1970. ...以<epoch_mtime> <filename><NUL>格式<epoch_mtime> <filename><NUL>内容,其中epoch_mtime是自1970年1月1日以来的秒数。

sort -z -r -n

...then sorts that output, expecting it to be NUL-delimited, on the numbers at the beginning. ...然后对输出进行排序,并期望在开头的数字处使用NUL分隔。

{ 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 the filename variable. ...将内容读入mtime变量,直到第一行的第一个空格,然后再将第一个NUL读入filename变量。

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

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