How to Know the Creation Time of a File in Ubuntu

Is there a way to know the creation time of a file in ubuntu?

Unfortunately Unix does not store the creation time of a file.

All you are able to get using stat is

  1. time of last access
  2. time of last modification
  3. time of last status change

Note: When using filesystem type ext4 crtime is available!

How to get file creation date/time in Bash/Debian?

Unfortunately your quest won't be possible in general, as there are only 3 distinct time values stored for each of your files as defined by the POSIX standard (see Base Definitions section 4.8 File Times Update)

Each file has three distinct associated timestamps: the time of last
data access, the time of last data modification, and the time the file
status last changed. These values are returned in the file
characteristics structure struct stat, as described in <sys/stat.h>.

EDIT: As mentioned in the comments below, depending on the filesystem used metadata may contain file creation date. Note however storage of information like that is non standard. Depending on it may lead to portability problems moving to another filesystem, in case the one actually used somehow stores it anyways.

How to get file creation date in Linux?

fstat works on file descriptors, not FILE structures. The simplest version:

#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>

#ifdef HAVE_ST_BIRTHTIME
#define birthtime(x) x.st_birthtime
#else
#define birthtime(x) x.st_ctime
#endif

int main(int argc, char *argv[])
{
struct stat st;
size_t i;

for( i=1; i<argc; i++ )
{
if( stat(argv[i], &st) != 0 )
perror(argv[i]);
printf("%i\n", birthtime(st));
}

return 0;
}

You will need to figure out if your system has st_birthtime in its stat structure by inspecting sys/stat.h or using some kind of autoconf construct.

Get file creation time with Python on linux

You probably can't.:

3.1)  How do I find the creation time of a file?

You can't - it isn't stored anywhere. Files have a last-modified
time (shown by "ls -l"), a last-accessed time (shown by "ls -lu")
and an inode change time (shown by "ls -lc"). The latter is often
referred to as the "creation time" - even in some man pages -
but that's wrong; it's also set by such operations as mv, ln,
chmod, chown and chgrp.

The man page for "stat(2)" discusses this.

How to find file creation date in linux using java?

You can use stat command in Linux to get various date though creation date isn't available.

Instead you can get these 3 dates about a file:

  • Time of last access
  • Time of last modification (content of the file)
  • Time of last change (metadata of file)

EDIT:

For getting creation/modification times of a file in Java (if using JDK 1.7) see: http://docs.oracle.com/javase/tutorial/essential/io/fileAttr.html

As per this document:

A word about time stamps: The set of basic attributes includes three
time stamps: creationTime, lastModifiedTime, and lastAccessTime. Any
of these time stamps might not be supported in a particular
implementation, in which case the corresponding accessor method
returns an implementation-specific value.

Unfortunately Linux/Unix doesn't store file's creation time hence you cannot get it.

PS: If you can use ext4 filesystem then you can get file's creation time in Unix/Linux.

Check if file was created more than 10 minutes from now

You can just use find to delete a file, for example to delete file1 in the current folder:

find . -name file1 -mmin +10 -exec rm {} \;

How do I get file creation and modification date/times?

In Python 3.4 and above, you can use the object oriented pathlib module interface which includes wrappers for much of the os module. Here is an example of getting the file stats.

>>> import pathlib
>>> fname = pathlib.Path('test.py')
>>> assert fname.exists(), f'No such file: {fname}' # check that the file exists
>>> print(fname.stat())
os.stat_result(st_mode=33206, st_ino=5066549581564298, st_dev=573948050, st_nlink=1, st_uid=0, st_gid=0, st_size=413, st_atime=1523480272, st_mtime=1539787740, st_ctime=1523480272)

For more information about what os.stat_result contains, refer to the documentation. For the modification time you want fname.stat().st_mtime:

>>> import datetime
>>> mtime = datetime.datetime.fromtimestamp(fname.stat().st_mtime, tz=datetime.timezone.utc)
>>> print(mtime)
datetime.datetime(2018, 10, 17, 10, 49, 0, 249980)

If you want the creation time on Windows, or the most recent metadata change on Unix, you would use fname.stat().st_ctime:

>>> ctime = datetime.datetime.fromtimestamp(fname.stat().st_ctime, tz=datetime.timezone.utc)
>>> print(ctime)
datetime.datetime(2018, 4, 11, 16, 57, 52, 151953)

This article has more helpful info and examples for the pathlib module.

how do I check in bash whether a file was created more than x time ago?

Only for modification time

if test `find "text.txt" -mmin +120`
then
echo old enough
fi

You can use -cmin for change or -amin for access time. As others pointed I don’t think you can track creation time.

PHP: how can I get file creation date?

Use filectime. For Windows it will return the creation time, and for Unix the change time which is the best you can get because on Unix there is no creation time (in most filesystems).

Note also that in some Unix texts the
ctime of a file is referred to as
being the creation time of the file.
This is wrong. There is no creation
time for Unix files in most Unix
filesystems.



Related Topics



Leave a reply



Submit