I would like to check if my last file is older than 24 hours or not. (project in django)
I have many zip packages in directory so I have to 'filter' the last one with this part of code: ls -1 | sort -n | tail -n1.
My code in .sh file:
#!/bin/bash
file="$HOME/path_directory ls -1 | sort -n | tail -n1"
current=`date +%s`;
last_modified=`stat -c "%Y" $file`;
if [ $(($current-$last_modified)) -gt 86400 ]; then
     echo "File is older that 24 hours" | mailx noreply@address -s "Older than 24 hours" [email protected]
else
     echo "File is up to date.";
fi;
Here is an error, that I got:
stat: invalid option -- '1'
Try 'stat --help' for more information.
/path_directory/imported_file.sh: line 9: 1538734802-: syntax error: operand expected (error token is "-")
If somebody made something similar, please some hint.
I'd advise you to try this:
if test "`find file -mtime +1`"
but if you insist you can fix it by changing it to this:
#!/bin/bash
file="$HOME/path_directory ls -1 | sort -n | tail -n1"
current=$(date +%s);
last_modified=$(stat -c "%Y" $file);
if [ $((current - last_modified)) -gt 86400 ]; then
     echo "File is older that 24 hours" | mailx noreply@address -s "Older than 24 hours" [email protected]
else
     echo "File is up to date.";
fi;
You can obtain the list of files in the directory that have been modified earlier than 1440 minutes (86400 seconds), you can use find for this:
find -maxdepth 1 -mmin +1440
It will thus select all files in this directory (no subdirectories), with a change time in minutes older than 1440.
The + in +1440 is important, since otherwise you will obtain files that are exactly 1440 minutes unmodified.
You can also use -mtime to specify the number in days:
find -maxdepth 1 -mtime +1
If you want all files (in this directory and subdirectories), you can remove the -maxdepth 1.
You can add -type f if you only want to include files, etc. For more flags, and (filtering) options, please read the manpage of find.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With