filename脚本中的最后修改日期shell

时间:2022-09-25 14:04:06

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命令来获取它吗?

5 个解决方案

#1


16  

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是个坏主意。文件名中某些字符的行为不仅未定义且与平台有关,为了您的目的,它们会在过去六个月时混淆日期。简而言之,是的,它可能会在您的有限测试中为您服务。它不是独立于平台的(因此没有可移植性),并且鉴于各种系统上的“合法”文件名范围,不能保证解析的行为。 (例如,Ext4允许文件名中的空格和换行符)。

Having said all that, personally, I'd use ls because 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 %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).

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

So, a non-stat solution: use date

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

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

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

date有一套相当广泛的时间格式变量;阅读手册页以获取更多信息。

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 ;)

我不是100%确定所有'类UNIX系统'的可移植日期。对于基于BSD(例如OS X),这将不起作用; BSD-date的-r标志完全不同。问题并没有“详细说明解决方案的可移植性。”对于基于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).

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标志格式化stat的输出,并且您可以选择仅显示文件修改数据(对于此问题,这是很好的)。

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. stat -f:调用stat,并指定格式(-f)。
  3. %m: The UNIX epoch time.
  4. %m:UNIX纪元时间。
  5. %t: A tab seperator in the output.
  6. %t:输出中的制表符分隔符。
  7. %Sm: S says to display the output as a string, m says to use the file modification data.
  8. %Sm:S表示将输出显示为字符串,m表示使用文件修改数据。
  9. %N: Display the name of the file in question.
  10. %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 page for stat for further information; timestamp formatting is done by strftime.

阅读stat的手册页以获取更多信息;时间戳格式化由strftime完成。

#2


4  

have perl?

有perl吗?

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

#3


3  

How about:

怎么样:

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

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

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

#4


1  

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年5月27日

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

23:02

23:02

#5


-2  

You can use ls -l which lists the last modification time, and then use cut to cut out the modification date:

您可以使用列出最后修改时间的ls -l,然后使用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
  • 嗯嗯......:mm
  • Mmm dd yyyy
  • 嗯dy 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

#1


16  

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是个坏主意。文件名中某些字符的行为不仅未定义且与平台有关,为了您的目的,它们会在过去六个月时混淆日期。简而言之,是的,它可能会在您的有限测试中为您服务。它不是独立于平台的(因此没有可移植性),并且鉴于各种系统上的“合法”文件名范围,不能保证解析的行为。 (例如,Ext4允许文件名中的空格和换行符)。

Having said all that, personally, I'd use ls because 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 %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).

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

So, a non-stat solution: use date

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

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

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

date有一套相当广泛的时间格式变量;阅读手册页以获取更多信息。

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 ;)

我不是100%确定所有'类UNIX系统'的可移植日期。对于基于BSD(例如OS X),这将不起作用; BSD-date的-r标志完全不同。问题并没有“详细说明解决方案的可移植性。”对于基于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).

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标志格式化stat的输出,并且您可以选择仅显示文件修改数据(对于此问题,这是很好的)。

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. stat -f:调用stat,并指定格式(-f)。
  3. %m: The UNIX epoch time.
  4. %m:UNIX纪元时间。
  5. %t: A tab seperator in the output.
  6. %t:输出中的制表符分隔符。
  7. %Sm: S says to display the output as a string, m says to use the file modification data.
  8. %Sm:S表示将输出显示为字符串,m表示使用文件修改数据。
  9. %N: Display the name of the file in question.
  10. %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 page for stat for further information; timestamp formatting is done by strftime.

阅读stat的手册页以获取更多信息;时间戳格式化由strftime完成。

#2


4  

have perl?

有perl吗?

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

#3


3  

How about:

怎么样:

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

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

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

#4


1  

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年5月27日

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

23:02

23:02

#5


-2  

You can use ls -l which lists the last modification time, and then use cut to cut out the modification date:

您可以使用列出最后修改时间的ls -l,然后使用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
  • 嗯嗯......:mm
  • Mmm dd yyyy
  • 嗯dy 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