How to Check in Bash Whether a File Was Created More Than X Time Ago

how do I check in bash whether a file was created more than x time ago?

Only for modification time

if test `find "text.txt" -mmin +120`
then
echo old enough
fi

You can use -cmin for change or -amin for access time. As others pointed I don’t think you can track creation time.

Bash script find if file is created less then X time ago

You can use find command with -mmin option.

if [ `find path/to/file -mmin -60 | wc -c` -gt 0 ]; then
#found
fi

Check if file was created more than 10 minutes from now

You can just use find to delete a file, for example to delete file1 in the current folder:

find . -name file1 -mmin +10 -exec rm {} \;

If file modification date is older than N days

Several approaches are available. One is just to ask find to do the filtering for you:

if [[ $(find "$filename" -mtime +100 -print) ]]; then
echo "File $filename exists and is older than 100 days"
fi

Another is to use GNU date to do the math:

# collect both times in seconds-since-the-epoch
hundred_days_ago=$(date -d 'now - 100 days' +%s)
file_time=$(date -r "$filename" +%s)

# ...and then just use integer math:
if (( file_time <= hundred_days_ago )); then
echo "$filename is older than 100 days"
fi

If you have GNU stat, you can ask for a file's timestamp in seconds-since-epoch, and do some math yourself (though this will potentially be a bit off on the boundary cases, since it's counting seconds -- and not taking into account leap days and such -- and not rounding to the beginning of a day):

file_time=$(stat --format='%Y' "$filename")
current_time=$(( date +%s ))
if (( file_time < ( current_time - ( 60 * 60 * 24 * 100 ) ) )); then
echo "$filename is older than 100 days"
fi

Another option, if you need to support non-GNU platforms, is to shell out to Perl (which I'll leave it to others to demonstrate).

If you're interested more generally in getting timestamp information from files, and portability and robustness constraints surrounding same, see also BashFAQ #87.

Test a file date with bash

Here is the best answer I found at the time being, but it's only for the modification time :

expr `date +%s` - `stat -c %Y /home/user/my_file`

Checking if package is older than 24 hours with bash

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" me@mailmail.com
else
echo "File is up to date.";
fi;

Bash script to check if a new file has been created on a directory after run a command

Thanks to informative comments, I've just realized that I've missed the basics of bash script but finally made that work. I'll leave my solution here as an answer for those who struggle like me.:

WATCH_DIR=./tmp

FILES_BEFORE=$(ls $WATCH_DIR)

echo >$WATCH_DIR/filename

FILES_AFTER=$(ls $WATCH_DIR)

if diff <(echo "$FILES_AFTER") <(echo "$FILES_BEFORE")
then
echo "No changes"
else
echo "Changes"
fi

It outputs "Changes" on the first run and "No Changes" for the other unless you delete the newly added documents.



Related Topics



Leave a reply



Submit