用Bash打印修改日期

时间:2019-02-05 20:45:19

标签: bash date

我在获取bash脚本来打印文件的修改日期时遇到了一些麻烦,这是我的代码:

files=$(ls -R)    

for file in ${files}
do
  modification=$(date +%D -r ${file})
    echo ${modification}
done

问题是,当我运行脚本时,与脚本处于同一级别的所有内容都将打印其日期,但是如果文件低于或高于该级别,则会收到错误,如以下输出所示:

File .: was modified on 
File 1.txt was modified on 02/05/19
File 2.txt was modified on 02/05/19
File 3.txt was modified on 02/05/19
File folder was modified on 02/05/19
File GetFiles.sh was modified on 02/05/19
date: ./folder:: No such file or directory
File ./folder: was modified on 
date: folder1.txt: No such file or directory
File folder1.txt was modified on 
date: folder2.txt: No such file or directory
File folder2.txt was modified on 
date: folder3.txt: No such file or directory
File folder3.txt was modified on 

1 个答案:

答案 0 :(得分:2)

shopt -s globstar
for i in **; do date +"$i was modified on %D" -r "$i"; done

来自man bash

  

globstar:                         如果设置,则在路径名扩展上下文中使用的模式**将匹配所有文件,并且零或                         更多目录和子目录。如果模式后跟/,则仅目录和                         子目录匹配。


请参阅:help shopt