connollyc4 connollyc4 - 2 months ago 8x
Bash Question

how to add a timestamp to a file using the last modified date instead of current date

Is there a way to add the last modified timestamp to a file name? I'm trying to backup files older than 10 days and I'm trying to smack the last modified date to the files the script finds:

find /users/tnea01/logfile* -mtime +10 -type f -exec sh -c \ 'tar -czvPf /users/tnea01/logfile_archive/$(basename $0)_$(date +%F).tar.gz $0' {} \;

instead of using date +%F I wanted to use the modified date and not the current.

Thanks you


What you can do in this case is use the "-r" for date. So you can:

date -r <filename>

will return a date like : Tue Jul 26 14:02:26 PDT 2016

If you can also use the %F or %D after the filename as you were doing:

date -r <filename> +%F

will return just the yyy-mm-dd like 2016-07-26