compare file's date bash
You can compare file modification times with test
, using -nt
(newer than) and -ot
(older than) operators:
if [ "$file1" -ot "$file2" ]; then
cp -f "$file2" "$file1"
fi
Bash compare file last modification date with another date
You need to convert both dates to a common format that is amenable to comparison. You may be able to simply use GNU date
to convert both to Unix epoch timestamps:
d1=$(date +%s --date "$file_last_modified_date") # 1478037600
d2=$(date +%s --date "$variable") # 1478099452
Now you can compare them as integers.
if [ "$d1" -gt "$d2" ]; then
Bash Script - Comparing last modified dates
I have rewritten the script without using the date
command of the shell, which is not portable across MacOS and GNU Linux. I use ls
instead, hoping this command is actually portable. Please check:
#!/bin/bash
## variables commented out since we don't have access to theirs values:
#remote_file="www.someurl.com/file"
#remote_last_modified_date="$(curl -sI ${remote_file} | grep -E "Last-Modified:" | awk '{print $3,$4,$5,$6}' )"
## let's suppose we get next result into our variables:
local_file="test"
remote_last_modified_date="16 Apr 2020 08:14:26"
## we don't use the date command, not portable across MacOS and GNU Linux
## we use ls command with the option --time-style instead
## rewrite dates to allow comparison in format +"%Y-%m-%d_%H:%M:%S"
local_sortable_date=$(ls -l --time-style=+"%Y-%m-%d_%H:%M:%S" "$local_file"| cut -d ' ' -f 6)
remote_year=${remote_last_modified_date:7:4}
remote_month_name=${remote_last_modified_date:3:3}
remote_month=$(echo $remote_month_name |
sed 's/Jan/01/;s/Feb/02/;s/Mar/03/;s/Apr/04/;s/May/05/;s/Jun/06/;
s/Aug/08/;s/Sep/09/;s/Oct/10/;s/Nov/11/;s/Dec/12/')
remote_day=${remote_last_modified_date:0:2}
remote_time=${remote_last_modified_date:12:8}
remote_sortable_date=${remote_year}-${remote_month}-${remote_day}_$remote_time
if [[ $remote_sortable_date > $local_sortable_date ]] ; then
echo "A new version is available"
set of commands
else
echo "latest update already installed"
fi
Best way to compare Timestamps in Linux shell/bash script?
Why don't you stick with epoch-time? You can get the current time as seconds since epoch by date +%s
, so you just have to compare
if (( (healthcheck_time + 50*60) < $(date +%s) ))
then
# .... healthcheck older than 50 minutes
fi
Bash - How to compare two files last modified date, when they are remote?
Try this:
remote=$(ssh user@server "stat -c %Y /path/to/remote_file")
[[ -z "$remote" ]] && exit 1 # stop on error ($remote is empty)
local=$(stat -c %Y /path/to/local_file)
if [[ $remote -gt $local ]]; then
echo remote file is newer
else
echo local file is newer
fi
Comparing two dates in Linux bash shell script
Just a couple simple syntax modifications:
#!/bin/bash
#Setting variable
now=$(date +"%m-%d-%Y")
#Testing variable
echo "Current System Date: $now"
#Setting variable
filedate=$(date -r hello.sh "+%m-%d-%Y")
#Testing variable
echo "hello.sh date: $filedate"
if [[ $now == $filedate ]]
then
echo "This statement works"
else
echo "This statement didnt work"
fi
- in the
if
, you must put spaces after[
and before]
- you are comparing text, so
-ge
does not work. the "-" comparisons work for numbers. Here you want to use==
to compare text. - Reflex for me, I put
"
around echo text.
This will work for cases where the dates are the same. If you need "greater than" or "smaller than", put the dates in timestamp format (+%s) and compare the numbers. Then you could use the -ge
or others of the same type.
Date comparison in Bash
You can compare lexicographically with the conditional construct [[ ]]
in this way:
[[ "2014-12-01T21:34:03+02:00" < "2014-12-01T21:35:03+02:00" ]]
From the man:
[[ expression ]]
Return a status of 0 or 1 depending on the evaluation of the conditional expression expression.
New update:
If you need to compare times with different time-zone, you can first convert those times in this way:
get_date() {
date --utc --date="$1" +"%Y-%m-%d %H:%M:%S"
}
$ get_date "2014-12-01T14:00:00+00:00"
2014-12-01 14:00:00
$ get_date "2014-12-01T12:00:00-05:00"
2014-12-01 17:00:00
$ [[ $(get_date "2014-12-01T14:00:00+00:00") < $(get_date "2014-12-01T12:00:00-05:00") ]] && echo it works
it works
Bash script compare two date variables
The GNU date command can convert a date into the number of seconds since 1970. Try this script:
#! /bin/bash
DATE=$(date -d "$3-$2-$1 01" '+%s')
COUNT=0
tr '/' ' ' | {
while read D M Y ; do
THIS=$(date -d "$Y-$M-$D 01" '+%s')
if (( THIS > DATE )) ; then
COUNT=$((COUNT + 1))
fi
done
echo $COUNT
}
It expects three arguments and the raw dates in stdin:
for D in $(seq 19 25) ; do echo $D/08/2007 ; done | ./count.sh 22 08 2007
3
It will work till 2038. ;-)
Related Topics
Finding Process Count in Linux via Command Line
How to Tell Cmake I Want My Project to Link Libraries Statically
Script to Change Password on Linux Servers Over Ssh
Why Can't You Sleep While Holding Spinlock
Register Variables in Loop in an Ansible Playbook
Ld: Using -Rpath,$Origin Inside a Shared Library (Recursive)
Does Ldd Also Show Dependencies of Dependencies
How to Remove All Special Characters in Linux Text
How to Get the Physical Address from the Logical One in a Linux Kernel Module
How to Convert ".." in Path Names to Absolute Name in a Bash Script
What Does a Typical ./Configure Do in Linux
One Command to Create a Directory and File Inside It Linux Command
Glibc: Elf File Os Abi Invalid
How to Correctly Install Wkhtmltopdf on Debian 64 Bit
How to Continue One Thread at a Time When Debugging a Multithreaded Program in Gdb