bashshelldateunixcreation

Save Creation Date of File into Variable - BASH Script - UNIX


In my bash script I am doing the following:

shopt -s nullglob 

for file in $LOCATION/EXF_TESTFORSTACKOVERFLOW_*.xml;
 do
touch "$file.FTC"
 done

But Before creating the .FTC File I would like to do an check:

IF creation Date of the xml File is more than 30 Minutes from sysdate - create FTC

IF its not older than 30 Minutes - do not create .FTC for this File.

Any suggestions how i can implement this check before doing the touch "$file.FTC" I am not sure on how to check on the date for the files. When using command ls -l I see that date i want to do this check on.

Thank you all!


Solution

  • You may not have a "create (or birth)" time -- it depends on what filesystem you have (check your /etc/fstab) -- you should probably use "mtime" instead, the last modified time, or "ctime", the last inode change (file rename, moved, etc)

    Here's an example of the stat command to get time data for your files:

    stat -c $'%n\nbirth\t%W = %w\naccess\t%X = %x\nmod\t%Y = %y\nchange\t%Z = %z\n' *
    

    The output might look something like (this is an XFS file system):

    file.txt
    birth   0 = -
    access  1595249263 = 2020-07-20 08:47:43.194000000 -0400
    mod     1595249255 = 2020-07-20 08:47:35.453000000 -0400
    change  1595249255 = 2020-07-20 08:47:35.453000000 -0400
    
    test.dat
    birth   0 = -
    access  1595364240 = 2020-07-21 16:44:00.675000000 -0400
    mod     1595364184 = 2020-07-21 16:43:04.786000000 -0400
    change  1595364184 = 2020-07-21 16:43:04.786000000 -0400
    
    menu.sh
    birth   0 = -
    access  1600025501 = 2020-09-13 15:31:41.977000000 -0400
    mod     1600025496 = 2020-09-13 15:31:36.664000000 -0400
    change  1600025496 = 2020-09-13 15:31:36.664000000 -0400
    

    On my Mac with APFS, the birth time is a known value.