Search code examples
linuxbashrhel

Proper way to format timestamp in bash


So I need to create an output file for an outside contractor containing a list of filenames from a directory and creation dates in a specific format like this:

FileName     YYYYmmDDHHMMSS

So far I've come up with:

find ./ -type f -printf " %f %a\n"

which returns:

FIleName Fri Apr 21 18:21:15.0458585800 2017

Or:

ls -l | awk {'print  $9" "$6" "$7" "$8'}

which returns:

FileNAme Apr 21 18:21

But neither is quite the output i need as it needs to be purely numerical and include seconds.

Keeping in mind that the list of files could be very large so efficiency is a priority, how can i get this output?


Solution

  • Something like this

    find ./ -type f -printf " %f %AY%Am%Ad%AH%AM%AS\n" |sed -e 's/\.[0-9]*$//'
    

    (sed is needed to remove fractional part after seconds)

    (Edit) with ls it will be:

    ls -l --time-style=+%Y%m%d%H%M%S |awk {'print $7" "$6'}