简体   繁体   English

从 Bash Shell 脚本中最新的目录中检查文件

[英]Check from files in directory which is the most recent in Bash Shell Script

I am making a bash script to run in a directory with files generated everyday and copy the most recent file to another directory.我正在制作一个 bash 脚本,以便在每天生成文件的目录中运行,并将最新的文件复制到另一个目录。

I am using this now我现在正在使用这个

for [FILE in directory]
do
  if [ls -Art | tail -n 1]
    something...
  else
    something...
  fi
done

I know this is not alright.我知道这不行。 I would like to compare the date modified of the files with the current date and if it was equal, copy that file then.我想将文件的修改日期与当前日期进行比较,如果相等,则复制该文件。

How would that work or is there an easier method to do it?这将如何工作或有更简单的方法来做到这一点?

The newest file is different from file(s) modified today.最新的文件与今天修改的文件不同。

Using ls is actually a pretty simple and portable approach.使用ls实际上是一种非常简单且可移植的方法。 The stdout output format is defined by POSIX (if not printing to a terminal), and ls -A is also in newer POSIX standards. stdout 输出格式由 POSIX 定义(如果不打印到终端),并且ls -A也在较新的 POSIX 标准中。

It should look more like this though:不过它应该看起来更像这样:

newest=$(ls -At | head -n 1)

You could add -1 , but it AFAIK it shouldn't be required, as it's not printing to a terminal.您可以添加-1 ,但它AFAIK 不应该是必需的,因为它不会打印到终端。

If you don't want to use ls , you can use this on linux:如果你不想使用ls ,你可以在 linux 上使用它:

find . -mindepth 1 -maxdepth 1 -type f -exec stat -c ‘%Y:%n’ {} + |
sort -n |
head -n 1 |
cut -d : -f 2-

Note using 2- not 2 with cut , in case a filename contains : .注意使用2-而不是2cut ,以防文件名包含:

Also, the resulting file name will be a relative path ( ./file ), or an empty string if no files exist.此外,生成的文件名将是相对路径 ( ./file ),如果不存在文件, ./file空字符串。

We could use find :我们可以使用find

find . -maxdepth 1 -daystart -type f -mtime -1 -exec cp -f {} dest \;

Explanation:解释:

  • -maxdepth 1 limits the search to the current directory. -maxdepth 1限制搜索到当前目录。
  • -daystart sets the reference time of -mtime to the beginning of today. -daystart-mtime的参考时间设置为今天的开始。
  • -type f limits the search to files. -type f将搜索限制为文件。
  • -mtime -1 limits the search to files that have been modified less than 1 day from reference time. -mtime -1将搜索限制为自参考时间-mtime -1 1 天修改过的文件。
  • -exec cp -f {} dest \\; copies the found files to directory dest .将找到的文件复制到目录dest

Note that -daystart -mtime -1 means anytime after today 00:00 (included), but also tomorrow or any time in the future.请注意, -daystart -mtime -1表示今天 00:00(包括在内)之后的任何时间,但也包括明天或未来的任何时间。 So if you have files with last modification time in year 2042 they will be copied too.因此,如果您有上次修改时间为 2042 年的文件,它们也会被复制。 Use -mtime 0 if you prefer coping files that have been modified between today at 00:00 (excluded) and tomorrow at 00:00 (included).如果您更喜欢在今天 00:00(不包括)和明天 00:00(包括)之间修改的文件,请使用-mtime 0

Note also that all this could be impacted by irregularities like daylight saving time or leap seconds (not tested).另请注意,所有这些都可能受到夏令时或闰秒(未测试)等违规行为的影响。

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

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