在脚本文件的最后修改日期壳 [英] filename last modification date shell in script

查看:137
本文介绍了在脚本文件的最后修改日期壳的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用bash建立一个剧本,我会在一个变量获得的文件名,然后用这个变量获取文件UNIX最后修改日期。

I'm using bash to build a script where I will get a filename in a variable an then with this variable get the file unix last modification date.

我需要得到这个修改日期值,我不能用stat命令。

I need to get this modification date value and I can't use stat command.

你知道任何方式得到它的共同可用* nix的命令?

Do you know any way to get it with the common available *nix commands?

推荐答案

LS 是一个坏主意。不仅是不确定的某些字符文件名中的行为,并依赖于平台,你的目的,它会的乱用时,他们在过去的6个月日期。总之,是的,它可能会为你在有限的测试工作。它将不可以是平台无关的(所以没有可移植性)和您解析的行为的不可以给予保证在各种系统上合法文件名的范围。 (ext4的,例如,允许使用空格和换行的文件名的)。

Why you shouldn't use ls:

Parsing ls is a bad idea. Not only is the behaviour of certain characters in filenames undefined and platform dependant, for your purposes, it'll mess with dates when they're six months in the past. In short, yes, it'll probably work for you in your limited testing. It will not be platform-independent (so no portability) and the behaviour of your parsing is not guaranteed given the range of 'legal' filenames on various systems. (Ext4, for example, allows spaces and newlines in filenames).

说了这么多,就个人而言,我会使用 LS ,因为它的快速和容易;)

Having said all that, personally, I'd use ls because it's fast and easy ;)

由于在评论中指出雨果的OP不希望使用统计。另外,我要指出的是,下面的部分是BSD-STAT特定的(当我在Ubuntu测试%的Sm 标记不起作用; Linux有一个统计命令,如果你感兴趣阅读手册页 )。

As pointed out by Hugo in the comments, the OP doesn't want to use stat. In addition, I should point out that the below section is BSD-stat specific (the %Sm flag doesn't work when I test on Ubuntu; Linux has a stat command, if you're interested in it read the man page).

日期,至少在Linux上,有一个标志: -r ,它根据的手册页

date, at least on Linux, has a flag: -r, which according to the man page:

显示文件的最后修改时间

display the last modification time of FILE

因此​​,照本宣科的解决办法是与此类似:

So, the scripted solution would be similar to this:

date -r ${MY_FILE_VARIABLE}

这将类似的东西返回到这一点:

which would return you something similar to this:

zsh% date -r MyFile.foo
Thu Feb 23 07:41:27 CST 2012

要解决OP的评论:

如果可能的可配置的日期格式

If possible with a configurable date format

日期具有相当广泛的设定时间格式的变量;阅读man页面了解更多信息。

date has a rather extensive set of time-format variables; read the man page for more information.

我不是100%确定如何移动日期在所有'类UNIX系统。对于基于BSD(如OS X),这会的的工作;对于BSD-日期 -r 标志做了完全不同。这个问题并没有指定的解决方案究竟是如何携带要求是。对于基于BSD的解决方案,请参见下文)

I'm not 100% sure how portable date is across all 'UNIX-like systems'. For BSD-based (such as OS X), this will not work; the -r flag for the BSD-date does something completely different. The question doesn't' specify exactly how portable a solution is required to be. For a BSD-based solution, see the below section ;)

使用统计。您可以格式化的统计输出与 -f 标记,您可以选择只显示文件的修改数据(对于这个问题,是好的)。

Use stat. You can format the output of stat with the -f flag, and you can select to display only the file modification data (which, for this question, is nice).

例如,与stat -f%M%T%的Sm%N./*


1340738054  Jun 26 21:14:14 2012 ./build
1340738921  Jun 26 21:28:41 2012 ./build.xml
1340738140  Jun 26 21:15:40 2012 ./lib
1340657124  Jun 25 22:45:24 2012 ./tests

当第一位为 UNIX纪元时间,日期是文件的修改时间,而剩下的就是文件名。

Where the first bit is the UNIX epoch time, the date is the file modification time, and the rest is the filename.

的例子命令击穿

与stat -f%M%T%的Sm%N./*


  1. 与stat -f :呼叫统计,并指定格式( -f )。

  2. %M :Unix纪元的时间。

  3. %T :在输出选项卡分隔符。

  4. %的Sm 取值表示,显示输出为字符串 M 表示使用文件修改数据。

  5. %N :显示有问题的文件的名称。

  1. stat -f: call stat, and specify the format (-f).
  2. %m: The UNIX epoch time.
  3. %t: A tab seperator in the output.
  4. %Sm: S says to display the output as a string, m says to use the file modification data.
  5. %N: Display the name of the file in question.

在你的脚本大致如下的行的命令:

A command in your script along the lines of the following:

stat -f "%Sm" ${FILE_VARIABLE}

会给你的输出,例如:

will give you output such as:

Jun 26 21:28:41 2012

阅读 页面与stat 了解更多信息;时间戳格式由 的strftime 完成。

Read the man page for stat for further information; timestamp formatting is done by strftime.

这篇关于在脚本文件的最后修改日期壳的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆