I want to change the file's creation date. ...
How to take file's name, format the date and use it to change creation date?
Back up (or actually back down).
In traditional Unix systems, files have three time stamps: the mtime (time the content of the file was last modified), the atime (the time the content of the file was last accessed), and the ctime (the time the metadata of the file was last modified). Some more recent Unix file systems (in particular both ZFS and UFS2 on FreeBSD) also store the birth time of the file; that is an innovation which came from Windows (because the Windows file system has always stored the birth time, it was necessary for Unix file systems to do the same, partially so Unix machines could be used as file servers for Windows networks). Some file systems don't update the atime, or only crudely or sometimes.
This brings up a question: Are you trying to change the ctime or the birthtime of the file?
With the traditional Unix system call
utimes(2), it is only possible to set the atime and the mtime. There are very simple but hacky ways to update the ctime to be "right now", namely change the metadata of the file frivolously. For example, you can change the owner or permissions of the file, and immediately change them back. I don't know of a way to change the ctime of a file to an arbitrary value. The reason this is "hacky" is that it is unsafe: If you program crashes between the two updates, you will leave the file with wrong owner or permissions; to do this right you would have to put transactional logging around it, which is a heck of a lot of work. Because the rules for ctime changes are "bizarre" (make perfect sense to a file system implementor, but not really to users), for the most part ctime is unused, and that's a good thing.
It turns out there is a nasty hacky way of changing the birthtime of a file, but AFAIK it only works in one direction: you can make it earlier, not later. Here's how: There is an invariant that the birthtime has to be no later than the mtime. So just change the mtime backwards to before the current birthtime, and the birthtime will be updated with it. Then immediately change the mtime to the real desired value. First, this is again hacky: if you crash in between, you have screwed up. And second it's dangerous, I'm not sure all file systems actually implement this, much less implement this correctly.
In general, updating file metadata (like atime/ctime/mtime/birthtime) is a bad idea. The file system or backup programs may rely on those being set correctly. And they do track the "truth", namely when this particular file was really created/modified/and so on. For example, what happens when you copy a file to another file system? The ctime, mtime and birthtime are guaranteed to change!
Based on all that, here's my advice: Users should not be storing their own metadata in file attributes. That's playing with fire, and hacky. If you have a certain idea of what the creation time of the logical content of the file should be, then store it somewhere other than the ctime or birthtime. There are many ways to do it:
- One is storing it in the file itself (if the file format is structured such that you can add information to it).
- Another way is to store it in the file name, and use a convention. For example, at home I use my computer(s) to store many paper document, and I have a convention that all such documents have names that start with YYYYMMDD, for example 20190725_electrical_bill.pdf. The date in the file name is the "logical" date of the document, in the case of an electrical bill for example the day the statement date. It is not the date on which I got a paper copy in my mailbox, and even less the day that I scanned the paper document, and copied it to my server.
- And if neither the file name nor the content are convenient places for you to store this information, consider using extended attributes. Both UFS and ZFS supported reading and writing extended attributes to files. The only real problem with those is that if you copy the files to other file systems, you have to be careful to only use copy programs that handle extended attributes, and never copy the files to file systems (such as FAT) that don't allow storing extended attributes.