在 Bash 中打印文件的上次修改日期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16391208/
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-09-09 23:40:15  来源:igfitidea点击:

Print a file's last modified date in Bash

bashshellfiledateunix

提问by Hokerie

I can't seem to find how to print out the date of a file. I'm so far able to print out all the files in a directory, but I need to print out the dates with it.

我似乎无法找到如何打印出文件的日期。到目前为止,我能够打印出目录中的所有文件,但我需要用它打印出日期。

I know I need to attach a date format with the echo of the entry, but all I can't find the correct format.

我知道我需要在条目的回显中附加日期格式,但我找不到正确的格式。

echo "Please type in the directory you want all the files to be listed"

read directory 

for entry in "$directory"/*
do
  echo "$entry"
done

采纳答案by Steven Penny

You can use the statcommand

你可以使用 stat命令

stat -c %y "$entry"

More info

更多信息

%y   time of last modification, human-readable

回答by mmond

Isn't the 'date' command much simpler? No need for awk, stat, etc.

'date' 命令不是更简单吗?不需要 awk、stat 等。

date -r <filename>

Also, consider looking at the man pagefor date formatting; for example with common date and time format:

另外,请考虑查看日期格式的手册页;例如使用常见的日期和时间格式:

date -r <filename> "+%m-%d-%Y %H:%M:%S"

回答by Danijel-James W

On OS X, I like my date to be in the format of YYYY-MM-DD HH:MMin the output for the file.

在 OS X 上,我喜欢我的日期YYYY-MM-DD HH:MM在文件输出中的格式。

So to specify a file I would use:

因此,要指定我将使用的文件:

stat -f "%Sm" -t "%Y-%m-%d %H:%M" [filename]

If I want to run it on a range of files, I can do something like this:

如果我想在一系列文件上运行它,我可以这样做:

#!/usr/bin/env bash
for i in /var/log/*.out; do
  stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
done

This example will print out the last time I ran the sudo periodic daily weekly monthlycommand as it references the log files.

此示例将打印出我上次运行该sudo periodic daily weekly monthly命令时引用的日志文件。



To add the filenames under each date, I would run the following instead:

要在每个日期下添加文件名,我将运行以下命令:

#!/usr/bin/env bash
for i in /var/log/*.out; do
  stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
  echo "$i"
done

The output would was the following:

输出如下:

2016-40-01 16:40
/var/log/daily.out
2016-40-01 16:40
/var/log/monthly.out
2016-40-01 16:40
/var/log/weekly.out

Unfortunately I'm not sure how to prevent the line break and keep the file name appended to the end of the date without adding more lines to the script.

不幸的是,我不确定如何防止换行并将文件名附加到日期末尾而不向脚本添加更多行。



PS - I use #!/usr/bin/env bashas I'm a Python user by day, and have different versions of bashinstalled on my system instead of #!/bin/bash

PS - 我#!/usr/bin/env bash白天使用的是 Python 用户,并且bash在我的系统上安装了不同版本的 Python,而不是#!/bin/bash

回答by rishi

Best is

最好的是

date -r filename +"%Y-%m-%d %H:%M:%S"

回答by fotanus

Adding to @StevePenny answer, you might want to cut the not-so-human-readable part:

添加到@StevePenny 的答案中,您可能想要删除不太人类可读的部分:

stat -c%y Localizable.strings | cut -d'.' -f1

回答by Fabien Snauwaert

I wanted to get a file's modification date in YYYYMMDDHHMMSSformat. Here is how I did it:

我想以YYYYMMDDHHMMSS格式获取文件的修改日期。这是我如何做到的:

date -d @$( stat -c %Y myfile.css ) +%Y%m%d%H%M%S

Explanation.It's the combination of these commands:

解释。这是这些命令的组合:

stat -c %Y myfile.css # Get the modification date as a timestamp
date -d @1503989421 +%Y%m%d%H%M%S # Convert the date (from timestamp)

回答by Hokerie

EDITED: turns out that I had forgotten the quotes needed for $entryin order to print correctly and not give the "no such file or directory" error. Thank you all so much for helping me!

编辑:结果我忘记了$entry正确打印所需的引号,而不是给出“没有这样的文件或目录”错误。非常感谢大家对我的帮助!

Here is my final code:

这是我的最终代码:

    echo "Please type in the directory you want all the files to be listed with last modified dates" #bash can't find file creation dates

read directory

for entry in "$directory"/*

do
modDate=$(stat -c %y "$entry") #%y = last modified. Qoutes are needed otherwise spaces in file name with give error of "no such file"
modDate=${modDate%% *} #%% takes off everything off the string after the date to make it look pretty
echo $entry:$modDate

Prints out like this:

打印出来是这样的:

/home/joanne/Dropbox/cheat sheet.docx:2012-03-14
/home/joanne/Dropbox/Comp:2013-05-05
/home/joanne/Dropbox/Comp 150 java.zip:2013-02-11
/home/joanne/Dropbox/Comp 151 Java 2.zip:2013-02-11
/home/joanne/Dropbox/Comp 162 Assembly Language.zip:2013-02-11
/home/joanne/Dropbox/Comp 262 Comp Architecture.zip:2012-12-12
/home/joanne/Dropbox/Comp 345 Image Processing.zip:2013-02-11
/home/joanne/Dropbox/Comp 362 Operating Systems:2013-05-05
/home/joanne/Dropbox/Comp 447 Societal Issues.zip:2013-02-11

回答by Anton Kiggundu

For the line breaks i edited your code to get something with no line breaks.

对于换行符,我编辑了您的代码以获得没有换行符的内容。

#!/bin/bash
for i in /Users/anthonykiggundu/Sites/rku-it/*; do
   t=$(stat -f "%Sm"  -t "%Y-%m-%d %H:%M" "$i")
   echo $t : "${i##*/}"  # t only contains date last modified, then only filename 'grokked'- else $i alone is abs. path           
done

回答by Bill

If file name has no spaces:

如果文件名没有空格:

ls -l <dir> | awk '{print , " ", , " ", , " ",  }'

This prints as the following format:

这打印为以下格式:

 Dec   21   20:03   a1.out
 Dec   21   20:04   a.cpp

If file names have space (you can use the following command for file names with no spaces too, just it looks complicated/ugly than the former):

如果文件名有空格(您也可以对没有空格的文件名使用以下命令,只是它看起来比前者复杂/丑陋):

 ls -l <dir> | awk '{printf ("%s %s %s ",  ,  , ); for (i=9;   i<=NF; i++){ printf ("%s ", $i)}; printf ("\n")}'

回答by Yoga Nathan

You can use:

您可以使用:

ls -lrt filename |awk '{print "%02d",}'

This will display the date in 2 digits.

这将以 2 位数字显示日期。

If between 1to 9it adds "0" prefix to it and converts to 01 - 09.

如果在19之间,它会为其添加“0”前缀并转换为 01 - 09。

Hope this meets the expectation.

希望这符合预期。