1

I am using ubuntu 22.04 LTS and I want to know if the date created on ubuntu can be copied to date modified? I think I've read that the date created on ubuntu isn't actually the date created even though the label 'date created' is in Nautilus, rather it's the ctime (date changed). Is this correct ? How can I copy the date changed or 'date created' into date modified?

Thanks!!

Raffa
  • 32,237

1 Answers1

0

You are right … ctime is the file's last status(i.e. meta data) change time.

However, in recent kernels, the support of the system call statx() is added ... That, when a file is queried with e.g. stat file, should return some extended file attributes including btime which is the birth/creation time of that file (Check first with stat file to see if birth time is reported) ... A quick check on Ubuntu 22.04 shows that stat indeed uses statx() to get the btime ... See for example:

$ strace -e statx -v -s 0 stat filename
statx(AT_FDCWD, "filename", AT_STATX_SYNC_AS_STAT|AT_SYMLINK_NOFOLLOW, STATX_ALL, {stx_mask=STATX_ALL|STATX_MNT_ID, stx_blksize=4096, stx_attributes=0, stx_nlink=1, stx_uid=1000, stx_gid=1000, stx_mode=S_IFREG|0664, stx_ino=22022834, stx_size=0, stx_blocks=0, stx_attributes_mask=STATX_ATTR_COMPRESSED|STATX_ATTR_IMMUTABLE|STATX_ATTR_APPEND|STATX_ATTR_NODUMP|STATX_ATTR_ENCRYPTED|STATX_ATTR_AUTOMOUNT|STATX_ATTR_MOUNT_ROOT|STATX_ATTR_VERITY|STATX_ATTR_DAX, stx_atime={tv_sec=1679481498, tv_nsec=733858313} /* 2023-03-22T13:38:18.733858313+0300 */, stx_btime={tv_sec=1679481214, tv_nsec=228264332} /* 2023-03-22T13:33:34.228264332+0300 */, stx_ctime={tv_sec=1679481498, tv_nsec=733858313} /* 2023-03-22T13:38:18.733858313+0300 */, stx_mtime={tv_sec=1679481498, tv_nsec=733858313} /* 2023-03-22T13:38:18.733858313+0300 */, stx_rdev_major=0, stx_rdev_minor=0, stx_dev_major=8, stx_dev_minor=2}) = 0
  File: filename
  Size: 0           Blocks: 0          IO Block: 4096   regular empty file
Device: 802h/2050d  Inode: 22022834    Links: 1
Access: (0664/-rw-rw-r--)  Uid: ( 1000/  ubuntu)   Gid: ( 1000/  ubuntu)
Access: 2023-03-22 13:38:18.733858313 +0300
Modify: 2023-03-22 13:38:18.733858313 +0300
Change: 2023-03-22 13:38:18.733858313 +0300
 Birth: 2023-03-22 13:33:34.228264332 +0300
+++ exited with 0 +++

Therefore, you should be able to copy the creation time of a file to replace its modification time with something like this:

touch -m -d "$(stat -c '%w' file)" file

Or to replace both access and modification times add the -a option to touch as well … or drop the -m option from touch as it should then default to both.

Or, you can do the same for all the files in the current working directory with something like this:

for f in *
  do
    [ -f "$f" ] && touch -m -d "$(stat -c '%w' "$f")" "$f"
  done
Raffa
  • 32,237
  • Hello, yes upon using stat filename.jpg I can also confirm that it shows the birth time of files. Actually to clarify, I am specifically using Ubuntu 22.04.2 LTS (sorry about not clarifying this earlier). I can also confirm that the birth time does copy perfectly into the date modified.

    If I make a copy of the original file, is there any possible way to preserve the birth time, change time, accessed time, modified time at all ? even with star archive ? (https://linux.die.net/man/1/star) it says it preserves extended timestamps but I dont know how to use it...yet

    – ghostanime2001 Mar 22 '23 at 14:57
  • @anmac1789 AFAIK you can only preserve the modification date with e.g. cp --preserve=timestamps original copy or cp original copy && touch -r original copy or rsync -t original copy ... Theoretically these commands should copy Access date as well but this one is constantly changing e.g. during the file copying process itself ,,, Change date or ctime and creation date or btime require a file system debugger level to change to earlier date/time, so not changeable via normal user tools. – Raffa Mar 22 '23 at 15:28
  • Just out of curiosity, how would one change the date created/birth time on a file system debugger level ? – ghostanime2001 Mar 22 '23 at 18:11
  • @anmac1789 Curiosity killed the cat :-) … See for example https://unix.stackexchange.com/q/36021 and be very careful and backup first. – Raffa Mar 22 '23 at 18:22
  • access system disk using debugfs, or modify the kernel, I see someone write that we have to add attr->ia_ctime = attr->ia_mtime; //Sets the ctime attr->ia_atime = attr->ia_mtime from this link https://stackoverflow.com/questions/16126992/setting-changing-the-ctime-or-change-time-attribute-on-a-file/17066309#17066309 – ghostanime2001 Mar 22 '23 at 18:31
  • @anmac1789 kernel is a totally different beast and modifying its source code requires building that source code then loading the newly built kernel … Take my word if you would and let it slip or if you don’t then your system will not be considered Ubuntu anymore and we can’t support it here … And you will most likely need support then. – Raffa Mar 22 '23 at 18:44
  • alright looks like a dangerous way to modify files, i assumed it would somewhat be easy since linux is supposed to be easy. I'll just work on modifying the date modified and leave the rest alone until a more proven solution comes along – ghostanime2001 Mar 22 '23 at 18:47