View the date of a set of files


I'm trying to make a script that checks the date of the files of different directories and if they are older than 7 days since the last update send me an email.

To begin with, I would like to know how I can extract the date from the files. I tried with ls -l | cut -f8 -d ' ' but I do not get all the dates.

I would also like to know how I can compare these dates with the current system date.

asked by FastMaster 08.05.2016 в 17:16

2 answers


Processing the content of ls is absolutely inadvisable, because its format is not completely standard. You can read Why you should not check the output of ls to see all the details about it.

As Elenasys says, with stat you can get information about the last modification, the last access, etc, of the files.

However, once you get that result you will have to process that data, compare it with the current date, etc. And that turns out that find already does it.

With this command you can find the files within a structure that were modified more than 7 days ago:

find /dir/dir2 -mtime +7

From man find :


-mtime n


File's data was last modified n * 24 hours ago.

If what you want to find is only files, add -type to indicate it:

find /dir/dir2 -type f -mtime +7
answered by 09.05.2016 / 10:55

The date you can get is the "last modified" date, you can get a list of files with stat and% and to print the date:

stat -c %y "$entry"

to read the date of last modification within a directory:

for entry in "$directory"/* 
   stat -c %y "$entry" 
answered by 09.05.2016 в 03:54