{"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 id=\"ig-sh-1\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">function getmodif {<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">&nbsp; &nbsp;stat -c &quot;%y&quot; $1 | cut -d&quot; &quot; -f1 | sed -e 's\/..\/\/' -e 's\/-\/\/g'<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">}<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\n<p>Sample output :<\/p>\n<div id=\"ig-sh-2\" class=\"syntax_hilite\">\n\n\t\t<div class=\"toolbar\">\n\n\t\t<div class=\"view-different-container\">\n\t\t\t\t\t\t<a href=\"#\" class=\"view-different\">&lt; View <span>plain text<\/span> &gt;<\/a>\n\t\t\t\t\t<\/div>\n\n\t\t<div class=\"language-name\">code<\/div>\n\n\t\t\n\t\t<br clear=\"both\">\n\n\t<\/div>\n\t\n\t<div class=\"code\">\n\t\t<ol class=\"code\" style=\"font-family:monospace\"><li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">[ozh@SARL ~\/htdocs\/ozh]$ ls -l index.php<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">-rw-r--r-- &nbsp; &nbsp;1 sarl &nbsp; &nbsp; 515 &nbsp; &nbsp; &nbsp; &nbsp; 20728 nov 30 23:13 index.php<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">[sarl@SARL ~\/htdocs\/ozh]$ getmodif index.php<\/div><\/li>\n<li style=\"font-weight: normal;vertical-align:top\"><div style=\"font: normal normal 1em\/1.2em monospace;margin:0;padding:0;background:none;vertical-align:top\">041130<\/div><\/li>\n<\/ol>\t<\/div>\n\n<\/div>\n\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}]}}