Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

compare file's date bash

Tags:

linux

bash

centos

I'm working on a tiny dropbox-like bash script, how can I compare the dates of 2 files and replace the old one(s) with the new one without using rsync is there any simple way to process this? can the SHA1 help me with knowing the newer?

like image 878
Wael hamada Avatar asked Feb 10 '13 21:02

Wael hamada


People also ask

How do I compare two dates in Shell?

You can use date +%s -d your_date to get the number of seconds since a fixed instance (1970-01-01, 00:00 UTC) called "epoch". Once you get that it's really easy to do almost anything with dates.

How do I compare two timestamps in Unix?

If so, you can lexicographically compare your timestamp with the output of date -u -d '-10 minutes' +%Y%m%d%H%M%S (the -u assumes that your database timestamp is in UTC, which I'd advise).

What is $s in bash?

From man bash : -s If the -s option is present, or if no arguments remain after option processing, then commands are read from the standard input. This option allows the positional parameters to be set when invoking an interactive shell.


1 Answers

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 
like image 170
Anton Kovalenko Avatar answered Nov 10 '22 07:11

Anton Kovalenko