Linux 脚本中的文件名最后修改日期外壳

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11212663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-06 07:04:50  来源:igfitidea点击:

filename last modification date shell in script

linuxbashshellunixhp-ux

提问by odew

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.

我正在使用 bash 构建一个脚本,我将在其中获取一个变量中的文件名,然后使用此变量获取文件 unix 上次修改日期。

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

我需要获取此修改日期值,但无法使用 stat 命令。

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

你知道有什么方法可以用常用的 *nix 命令来获得它吗?

回答by Alex Howansky

How about:

怎么样:

find $PATH -maxdepth 1 -name $FILE -printf %Tc

See the find manpage for other values you can use with %T.

有关可以与 %T 一起使用的其他值,请参阅查找联机帮助页。

回答by David W.

You can use ls -lwhich lists the last modification time, and then use cutto cut out the modification date:

可以使用ls -lwhich列出上次修改时间,然后使用cut剪出修改日期:

mod_date=$(ls -l $file_name | cut -c35-46)

This works on my system because the date appears between columns 35 to 46. You might have to play with it on your system.

这适用于我的系统,因为日期出现在第 35 到 46 列之间。您可能必须在您的系统上使用它。

The date is in two different formats:

日期有两种不同的格式:

  • Mmm dd hh:mm
  • Mmm dd yyyy
  • Mmm dd hh:mm
  • Mmm dd yyyy

Files modified more than a year ago will have the later format. Files modified less than a year ago will have to first format. You could search for a ":" and know which format the file is in:

一年多以前修改过的文件将采用较新的格式。不到一年前修改的文件必须先格式化。您可以搜索“:”并知道文件的格式:

if echo "$mod_date" | grep -q ":"
then
    echo "File was modified within the year"
else
    echo "File was modified more than a year ago"
fi

回答by simont

Why you shouldn't use ls:

为什么不应该使用ls

Parsing lsis 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 notbe platform-independent (so no portability) and the behaviour of your parsing is notguaranteed given the range of 'legal' filenames on various systems. (Ext4, for example, allows spaces and newlines in filenames).

解析ls是一个坏主意。不仅文件名中某些字符的行为未定义且取决于平台,而且出于您的目的,它会弄乱过去六个月的日期。简而言之,是的,它可能会在您有限的测试中为您工作。它不会与平台无关(因此没有可移植性),并且鉴于各种系统上的“合法”文件名的范围,无法保证您的解析行为。(例如,Ext4允许文件名中有空格和换行符)。

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

说了这么多,就个人而言,我会使用ls它,因为它既快速又简单;)

Edit

编辑

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 %Smflag doesn't work when I test on Ubuntu; Linux has a statcommand, if you're interested in it read the man page).

正如 Hugo 在评论中指出的那样,OP 不想使用stat. 此外,我应该指出以下部分是特定于 BSD-stat 的(%Sm当我在 Ubuntu 上测试时该标志不起作用;Linux 有一个stat命令,如果您对它感兴趣,请阅读手册页)。

So, a non-statsolution: use date

所以,一个非stat解决方案:使用日期

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

date,至少在 Linux 上,有一个标志:-r,根据手册页

display the last modification time of FILE

显示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

To address the OP's comment:

要解决 OP 的评论:

If possible with a configurable date format

如果可能,使用可配置的日期格式

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

date有一组相当广泛的时间格式变量;阅读手册页了解更多信息。

I'm not 100% sure how portable dateis across all 'UNIX-like systems'. For BSD-based (such as OS X), this will notwork; the -rflag 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 ;)

我不是 100% 确定date所有“类 UNIX 系统”的可移植性如何。对于基于BSD(如OS X),这将工作; -rBSD-date的标志做了一些完全不同的事情。这个问题并没有具体说明解决方案需要有多便携。对于基于 BSD 的解决方案,请参阅以下部分;)

A better solution, BSD systems (tested on OS X, using BSD-stat; GNU stat is slightly different but could be made to work in the same way).

更好的解决方案,BSD 系统(在 OS X 上测试,使用 BSD-stat;GNU stat 略有不同,但可以以相同的方式工作)。

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

使用stat. 您可以stat使用-f标志格式化输出,并且您可以选择仅显示文件修改数据(对于这个问题,这很好)。

For example, stat -f "%m%t%Sm %N" ./*:

例如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

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

其中第一位是UNIX纪元时间,日期是文件修改时间,其余的是文件名。

Breakdown of the example command

示例命令分解

stat -f "%m%t%Sm %N" ./*

stat -f "%m%t%Sm %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: Ssays to display the output as a string, msays to use the file modification data.
  5. %N: Display the name of the file in question.
  1. stat -f: 调用stat,并指定格式 ( -f)。
  2. %m: UNIX 时代。
  3. %t:输出中的制表符分隔符。
  4. %Sm:S表示将输出显示为stringm表示使用文件修改数据。
  5. %N:显示相关文件的名称。

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

Read the man pagefor statfor further information; timestamp formatting is done by strftime.

阅读手册页stat获取更多信息;时间戳格式化由strftime.

回答by jm666

have perl?

有perl吗?

perl -MFile::stat -e "print scalar localtime stat('FileName.txt')->mtime"

回答by user5287944

You can use the "date" command adding the desired format option the format:

您可以使用“date”命令添加所需的格式选项格式:

 date +%Y-%m-%d -r /root/foo.txt

2013-05-27

2013-05-27

 date +%H:%M -r /root/foo.txt

23:02

23:02