I'm trying to achieve this:
for i in {01..31}
do
touch -t 202207$i0000 $i-07-2022.md
done
I have 31 .md files named 01-07-2022.md, 02-07-2022.md ... I basically want to change their creation date respectivelly to the date on their name. Is there someone able to tell me what's wrong with my code?
Thank's.
Solution
for i in {01..31}
do
SetFile -d "$i/01/2022 00:00:00" $i-07-2022.md
done
CodePudding user response:
Using touch
can only alter the modification and last access timestamps.
If you are creating a file with touch
, then its creation (birth) timestamp and its last modification timestamp are set to the -t
argument provided.
One way to see this behavior is to compare the output of ls
with and without the -U
option which reports the creation time instead of the modification value:
touch -t 202207010000 myfile
ls -lT myfile; ls -UlT myfile
... Jul 1 00:00:00 2022 myfile
... Jul 1 00:00:00 2022 myfile
touch -t 202208010000 myfile
ls -lT myfile; ls -UlT myfile
... Aug 1 00:00:00 2022 myfile
... Jul 1 00:00:00 2022 myfile
If you truly mean that you want to change the creation (birthtime) timestamp, independent of the last modification timestamp, and you are on macOS, and you have the XCode Command Line Tools installed, then you can use SetFile
. For example:
SetFile -d "04/01/2011 01:02:03" myfile
changes the creation timestamp of myfile
to the value requested. The modification timestamp remains unaltered, but can be changed independently with the -m
option of SetFile
.
SetFile -m "04/01/2011 01:02:03" myfile
As for scripting a quick solution to iterating over your files, use something like this:
#!/usr/bin/env bash
for file in *.md
do
mm=${file:0:2}
dd=${file:3:2}
yy=${file:6:4}
touch -t ${yy}${mm}${dd}0000 ${file}
done
This uses shell parameter expansion features to snip the components you need from the file names found.
As first noted, if you want to truly distinguish creation and modification timestamps, replace touch
with SetFile
directives.