{"id":168,"date":"2004-12-01T23:29:53","date_gmt":"2004-12-01T22:29:53","guid":{"rendered":"http:\/\/frenchfragfactory.net\/ozh\/archives\/2004\/12\/01\/file-modification-date-in-bash\/"},"modified":"2007-05-08T14:00:39","modified_gmt":"2007-05-08T12:00:39","slug":"file-modification-date-in-bash","status":"publish","type":"post","link":"https:\/\/planetozh.com\/blog\/2004\/12\/file-modification-date-in-bash\/","title":{"rendered":"File Modification Date in Bash"},"content":{"rendered":"<p>One thing really silly is the way digital cameras name their files : <em>dsc00001.jpg<\/em> or something similar, which is quite annoying when you try to sort old pictures. So I made a small bash script that renames every picture with a more useful filename : 041131-001.jpg for example. To do so, I&#39;m using a Perl script I&#39;ve made, <a href=\"\/blog\/archives\/2003\/12\/07\/datemodif-perl-script\/\">datemodif<\/a>, which outputs a modification date in various formats, but I wanted to do a more portable script, using only Bash and default GNU utils, but not Perl.<\/p>\n<p>After some testing, here is the function I&#39;ve added to my .bash_profile :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-1\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">function getmodif {\r\n   stat -c &quot;%y&quot; $1 | cut -d&quot; &quot; -f1 | sed -e &#039;s\/..\/\/&#039; -e &#039;s\/-\/\/g&#039;\r\n}<\/code><\/pre><\/div>\n<p>Sample output :<\/p>\n<div class=\"igsh-code-box\" id=\"ig-sh-2\"><pre class=\"language-none line-numbers\" data-no-optimize=\"1\" data-cfasync=\"false\"><code class=\"language-none\">[ozh@SARL ~\/htdocs\/ozh]$ ls -l index.php\r\n-rw-r--r--    1 sarl     515         20728 nov 30 23:13 index.php\r\n[sarl@SARL ~\/htdocs\/ozh]$ getmodif index.php\r\n041130<\/code><\/pre><\/div>\n","protected":false},"excerpt":{"rendered":"<p>Bash alias to get a nice modification date<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[5,2,3,76],"class_list":["post-168","post","type-post","status-publish","format-standard","hentry","tag-bash","tag-code","tag-linux","tag-tips"],"_links":{"self":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/168","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/comments?post=168"}],"version-history":[{"count":0,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/posts\/168\/revisions"}],"wp:attachment":[{"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/media?parent=168"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/categories?post=168"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/planetozh.com\/blog\/wp-json\/wp\/v2\/tags?post=168"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}