2

I would like to automate the creation of some files so I created a script. I want also to specify the creation date of those files.

In a terminal for example, to create a file.txt with creation date of 12 of May 2012 I could touch as seen bellow,

touch -d 20120512 file.txt

Listing that file confirms the date,

-rw-rw-r-- 1 lenovo lenovo 0 May  12  2012 file.txt

If I apply the above in a script the files that I am creating all have the current time as creation time and not what I've specified. What am I doing wrong here?

Script

#!/bin/bash

##################################
#Generate dat and snapshot files.#
##################################
srv_dir="/home/lenovo/Source/bash/srv"
main_dir="${srv_dir}/main"
database_dir="${main_dir}/Database"
dat_file="${main_dir}/remote.dat"

if [[ -e ${main_dir} ]]; then
    echo "${main_dir} allready exists."
    echo "Aborting..."
    exit 0
fi

# Create directories.
mkdir -p ${database_dir}

# Create files.
if [[ $1 == "--dat-newer" ]]; then
    # User wants dat file to be the latest modified file.
    
    # Create dat file with date as 'now'.
    touch ${dat_file}

    # Create snapshots with older dates.
    touch -d 20210511 "${database_dir}/snapshot001"
    touch -d 20210510 "${database_dir}/snapshot002"
    touch -d 20210512 "${database_dir}/snapshot004"
    touch -d 20210514 "${database_dir}/snapshot003"
else
    # Create an old dat file.
    touch -d 20210512 "${dat_file}"

    # Create snapshots with older dates.
    touch -d 20210511 "${database_dir}/snapshot001"
    touch -d 20210510 "${database_dir}/snapshot002"
    touch -d 20210512 "${database_dir}/snapshot004"

    # Create snapshot003 with date as 'now'.
    touch "${database_dir}/snapshot003"
fi

# populate dat and snapshot files with data.
echo "Data of ${dat_file}" > "${database_dir}/snapshot001"
echo "Data of snapshot001" > "${database_dir}/snapshot001"
echo "Data of snapshot002" > "${database_dir}/snapshot002"
echo "Data of snapshot003" > "${database_dir}/snapshot003"
echo "Data of snapshot004" > "${database_dir}/snapshot004"
0
8

The last part of your script, writing to each file, will result in the files’ last modification times all being updated to the current time. Changing times using touch should be the last thing you do to your files.

Note that touch can’t change the creation time (on file systems that track it); see How to change files creation time? (touch changes only modified time) for details.

0
4

"I want also to specify the creation date of those files" You can't do that as it's a creation time ("birth time", or btime). It's also quite hard to access - in particular not for ls or touch - but see stat if you do want to access it. Rather, what you are seeing is the last-modified datetime.

The first question I would be asking is whether $database_dir and $dat_file have the expected values at the point you use them. If either could contain a space in their name you must put them in double-quotes (and this is good practice anyway, regardless). For example,

touch -d 20210511 "${database_dir}/snapshot001"

Having now seen your full script, the reason the files all contain the current date/time is because the value you're seeing is the last-modified date, not the creation date. You last modify the files with the set of five echo statements at the end of the script, so they will all have the current date/time.

4
  • I've tried as well and still the modification date (you're right) is the current date. I've included the whole script. If you would like to test it you could replace srv_dir with a directory in your system. – Themelis May 17 at 15:12
  • It's such a basic script, don't know why I am not getting those modification dates. – Themelis May 17 at 15:15
  • 1
    On the contrary, most native Linux filesystems store that information. However, it's not modifiable. But that birth/creation time is otherwise not very useful as it's the time the inode spawned into existance, so doesn't reflect any useful date. For the creation time of the contents, there's the mtime, but of course if you want to change it arbitrawily to something other the time the file was last modified with touch, it needs to be done after that last modification. – Stéphane Chazelas May 17 at 15:15
  • I am a noob @StéphaneChazelas, the modification date is what I meant. – Themelis May 17 at 15:18

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.