我似乎無法找到如何打印出文件的日期。我至今能打印出目錄中的所有文件,但我需要用它打印出日期。在Bash中打印文件的最後修改日期
我知道我需要附加條目的回聲日期格式,但我找不到正確的格式。
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
echo "$entry"
done
我似乎無法找到如何打印出文件的日期。我至今能打印出目錄中的所有文件,但我需要用它打印出日期。在Bash中打印文件的最後修改日期
我知道我需要附加條目的回聲日期格式,但我找不到正確的格式。
echo "Please type in the directory you want all the files to be listed"
read directory
for entry in "$directory"/*
do
echo "$entry"
done
添加到@StevePenny答案,你可能要削減的不那麼人類可讀部分:
stat -c%y Localizable.strings | cut -d'.' -f1
如果文件名沒有空格:
ls -l <dir> | awk '{print $6, " ", $7, " ", $8, " ", $9 }'
這將打印爲以下格式:
Dec 21 20:03 a1.out
Dec 21 20:04 a.cpp
如果文件名具有空間(可以爲文件名使用下面的命令不帶空格太,只是看起來比較複雜/醜比前者):
ls -l <dir> | awk '{printf ("%s %s %s ", $6, $7, $8); for (i=9; i<=NF; i++){ printf ("%s ", $i)}; printf ("\n")}'
編輯:原來,我已經忘記了需要報價爲了正確打印並且不給「沒有這樣的文件或目錄」錯誤。非常感謝你幫助我!
這是我的最終代碼:
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
打印出這樣的:
/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
是不是 '日期' 命令更加簡單?無需AWK,統計等
date -r <filename>
更簡單,然後使用stat/more可用。 – Ing 2014-04-25 13:49:05
它看起來像BSD(或者至少OS X的)'日期'沒有這個。它的'-r'只是用來提供格式化的時間戳。你將不得不使用GNU日期來獲得這個功能。 – 2014-07-01 00:42:57
在OSX上使用:date'stat -f「%Sm」-t「%m%d%H%M%y」「$ {1}」' – Michaelangel007 2016-02-22 01:19:56
在OS X,我喜歡我的日期是在YYYY-MM-DD HH:MM
輸出的文件格式。
所以要指定一個文件,我會用:
stat -f "%Sm" -t "%Y-%m-%d %H:%M" [filename]
如果我想在一定範圍的文件運行它,我可以做這樣的事情:
#!/usr/bin/env bash
for i in /var/log/*.out; do
stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
done
這個例子將打印在我上次運行sudo periodic daily weekly monthly
命令時,因爲它引用了日誌文件。
要添加的文件名,每個日期下,我會跑,而不是執行以下操作:
#!/usr/bin/env bash
for i in /var/log/*.out; do
stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i"
echo "$i"
done
輸出將是以下幾點:
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
不幸的是我不知道怎麼樣以防止換行並將文件名稱追加到日期的末尾,而無需向腳本添加更多行。
PS - 我用#!/usr/bin/env bash
因爲我白天是一個Python用戶,並安裝在我的系統,而不是#!/bin/bash
對於換行符我編輯你的代碼在不同版本的bash
得到沒有換行符的東西。
#!/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
我想以YYYYMMDDHHMMSS
格式獲取文件的修改日期。以下是我如何做到的:
date -d @$(stat -c %Y myfile.css) +%Y%m%d%H%M%S
說明。正是這些命令的組合:
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)
閱讀-p目錄 – CousinCocaine 2014-07-10 11:08:28