
{"id":4732,"date":"2020-05-24T00:12:26","date_gmt":"2020-05-24T04:12:26","guid":{"rendered":"https:\/\/ikriv.com\/blog\/?p=4732"},"modified":"2020-05-24T00:12:43","modified_gmt":"2020-05-24T04:12:43","slug":"bash-is-missing-file-older-than-existing-file","status":"publish","type":"post","link":"https:\/\/ikriv.com\/blog\/?p=4732","title":{"rendered":"Bash: is missing file older than existing file?"},"content":{"rendered":"<p>In bash it is possible to test whether one file is older or newer than the other:<\/p>\n<pre>if [ file1 -nt file2 ]; then\r\n# do something\r\nfi\r\n\r\nif [ file3 -ot file4 ]; then\r\n# do something\r\nfi<\/pre>\n<p>However, <a href=\"http:\/\/linuxcommand.org\/lc3_man_pages\/testh.html\">the man page<\/a> does not say what happens if one of the files does not exist. Experiment shows that at least on my systems (Ubuntu 16.04 and 18.04) non-existing files are consistently treated as created in the infinite past. I.e., any existing file is <b>newer<\/b> than non existing file, and any non existing file is <b>older<\/b> than any existing file. The results are summarized in the table below:<\/p>\n<pre>[ existing -nt missing ] ==> TRUE\r\n[ missing -nt existing ] ==> FALSE\r\n[ existing -ot missing ] ==> FALSE\r\n[ missing -ot existing ] ==> TRUE\r\n[ missing -nt missing ] ==> FALSE\r\n[ missing -ot missing ] ==> FALSE<\/pre>\n<p>BTW, here&#8217;s the script that prints this table:<\/p>\n<pre>#!\/bin\/bash\r\n\r\nfunction compare {\r\n  echo -n \"[ $1 $2 $3 ] ==> \"\r\n  if [ $1 $2 $3 ]; then echo TRUE; else echo FALSE; fi\r\n}\r\n\r\ntouch existing\r\ncompare existing -nt missing\r\ncompare missing -nt existing\r\n\r\ncompare existing -ot missing\r\ncompare missing -ot existing\r\n\r\ncompare missing -nt missing\r\ncompare missing -ot missing<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>In bash it is possible to test whether one file is older or newer than the other: if [ file1 -nt file2 ]; then # do something fi if [ <a href=\"https:\/\/ikriv.com\/blog\/?p=4732\" class=\"more-link\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"Layout":"","footnotes":""},"categories":[4],"tags":[],"class_list":["entry","author-ikriv","post-4732","post","type-post","status-publish","format-standard","category-hack"],"_links":{"self":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=4732"}],"version-history":[{"count":2,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4732\/revisions"}],"predecessor-version":[{"id":4734,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/4732\/revisions\/4734"}],"wp:attachment":[{"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=4732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=4732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ikriv.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=4732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}