File Modification Date in Bash

One thing really silly is the way digital cameras name their files : dsc00001.jpg 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'm using a Perl script I've made, datemodif, 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.

After some testing, here is the function I've added to my .bash_profile :

  1. function getmodif {
  2.    stat -c "%y" $1 | cut -d" " -f1 | sed -e 's/..//' -e 's/-//g'
  3. }

Sample output :

  1. [ozh@SARL ~/htdocs/ozh]$ ls -l index.php
  2. -rw-r--r--    1 sarl     515         20728 nov 30 23:13 index.php
  3. [sarl@SARL ~/htdocs/ozh]$ getmodif index.php
  4. 041130

1 comment

  1. Marcelo Magalhaes

    Silly but very useful. Thanks a lot.

Comments are closed.