[英]Find the latest build file in directory
I need to get the latest build file in current directory . 我需要在当前目录中获取最新的构建文件。 The logic is sth like this:
逻辑是这样的:
I got this one so far but its not complete 到目前为止,我已经得到了这个,但是还不完整
find ./build -iregex '.*/build_.*\.tar\.gz' -type f -exec basename {} \;
I am puzzled around sorting it to get latest one 我不知道如何对其进行排序以获得最新的
This find
should work using stat
and sort
: 这个
find
应该使用stat
和sort
起作用:
find ./build -iregex '.*/build_.*\.tar\.gz' -type f -exec stat -c '%Y %n' {} + |
sort -rn -k1,1 | head -1 | cut -d " " -f2-
On OSX try this sed
: 在OSX上尝试使用此
sed
:
find ./build -iregex '.*/build_.*\.tar\.gz' -type f -exec stat -f '%m %N' {} + |
sort -rn -k1,1 | head -1 | cut -d " " -f2-
For a pure Bash possibility: 对于纯粹的Bash可能性:
#!/bin/bash
shopt -s globstar nullglob nocaseglob
latest=
for file in ./build/**/build_*.tar.gz; do
[[ -f $file ]] || continue
[[ $latest ]] || latest=$file
[[ $file -nt $latest ]] && latest=$file
done
if [[ $latest ]]; then
echo "Latest build: ${latest##*/}"
else
echo "No builds found"
fi
Having GNU find
, you can use the following command: 使用GNU
find
,可以使用以下命令:
find ./build -iregex '.*/build_.*\.tar\.gz' -printf '%T@ %f\n' | sort -n | tail -n1 | cut -d' ' -f2
It uses find
's printf action to print the timestamps of the latest modification along with the filename (basename). 它使用
find
的printf操作来打印最新修改的时间戳以及文件名(基本名)。 Then it pipes it to sort
, extracts the last line using tail
and finally separates the name from the timestamp using cut
. 然后通过管道将其
sort
,使用tail
提取最后一行,最后使用cut
将名称与时间戳分开。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.