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"
srv_dir
with a directory in your system. – Themelis May 17 '21 at 15:12touch
, it needs to be done after that last modification. – Stéphane Chazelas May 17 '21 at 15:15