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.
Print a file's last modified date in Bash
You can use thestat
command
stat -c %y "$entry"
More info
%y time of last modification, human-readable
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.
Date and time of a file in Unix in a desired format
Unix doesn't have a file creation time. It has these:
- last modified time (content was modified)
- last inode or files status change time (attributes like permissions, number of links etc., were changed)
- last access time (content was read)
ls
is the best option if you are not feeding the output to a program. man ls
is your friend.
ls -lu => show last access time
ls -lc => show last status change time
ls -l => show last modified time (default)
In general, it is not recommended that we feed the output of ls
into a program because of portability concerns. On macOS and Linux systems, we have a stat
command to extract file status information.
Related posts:
Print a file's last modified date in Bash
How to get the modified date and year of file in Unix?
How to get file creation time in Unix with Perl
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.
How to use 'find' to search for files created on a specific date?
As pointed out by Max, you can't, but checking files modified or accessed is not all that hard. I wrote a tutorial about this, as late as today. The essence of which is to use -newerXY
and ! -newerXY
:
Example: To find all files modified on the 7th of June, 2007:
$ find . -type f -newermt 2007-06-07 ! -newermt 2007-06-08
To find all files accessed on the 29th of september, 2008:
$ find . -type f -newerat 2008-09-29 ! -newerat 2008-09-30
Or, files which had their permission changed on the same day:
$ find . -type f -newerct 2008-09-29 ! -newerct 2008-09-30
If you don't change permissions on the file, 'c' would normally correspond to the creation date, though.
Write current date/time to a file using shell script
Use date >> //home/user/Desktop/Scripts/Date Logs/datelog.txt
.
Like i tried in my system :-
date > /tmp/date.txt
.
And file contains Wed Apr 5 09:27:37 IST 2017
.
[Edit] There are difference between >>(appending to the file) and >(Create the new file)
Edit:- As suggested by chepner, you can directly redirect the o/p of date command to file using date >> /tmp/date.txt
.
How to get true file creation date in R, on Unix systems?
Following other's pointers, this should work quite reasonably.
Unfortunately it needs root privileges (dued to debugfs
) and it's not very efficient yet (especially a bit quick'n dirty on regular expressions, but it's 01:00 o clock in the morning here :) ).
BTW, we set up the pager to be cat
(making debugfs
to print on standard output), find in which device the file is stored in order to use debugfs
properly and finally get the stats and elaborate it a bit.
In general, in UNIX
, once you have a bash-command to read its output in R
you have to use pipe
in read mode(that is default) and readLines
.
Test done in a Debian Gnu Linux.
np350v5c:/home/l# R
> my.file <- "/etc/network/interfaces"
>
> setup_pager <- function() {system("export PAGER=cat")}
>
> where_is <- function(file) {
con <- pipe(sprintf("df %s", file))
res <- strsplit(readLines(con)[2], " ")[[1]][1]
close(con)
res
}
>
> where_is(my.file) # could be /dev/sda1 as well, depending on /etc/fstab
[1] "/dev/disk/by-uuid/9ce40c2b-60d8-40b1-890f-1e5da4199c88"
>
> my.command <- sprintf("debugfs -R 'stat %s' %s",
my.file,
where_is(my.file))
>
> ## root privileges especially here ..
> setup_pager()
> con <- pipe(my.command)
> debugfs <- readLines(con)
debugfs 1.42.9 (4-Feb-2014)
> close(con)
>
> my.date <- gsub("^crtime:.+-- ", "", grep("^crtime", debugfs, value = TRUE))
> my.date
[1] "Tue Feb 19 00:07:21 2013"
> strptime(tolower(substr(my.date, 5, nchar(my.date))),
format = "%b %d %H:%M:%S %Y")
[1] "2013-02-19 00:07:21 CET"
HTH, Luca
Related Topics
How to Install Gcc 4.9.2 on Rhel 7.4
What Is the Meaning of So_Reuseaddr (Setsockopt Option) - Linux
What Does the Number in Parentheses Shown After Unix Command Names in Manpages Mean
Exploring Docker Container'S File System
"Failed to Load Platform Plugin "Xcb" " While Launching Qt5 App on Linux Without Qt Installed
How to Recursively Grep All Directories and Subdirectories
Finding Which Process Was Killed by Linux Oom Killer
How to Debug the Linux Kernel With Gdb and Qemu
What Happens If There Is No Exit System Call in an Assembly Program
How to Install Latest Version of Git on Centos 8.X/7.X/6.X
How to Create a Crontab Through a Script
How to Check If a File Is Empty in Bash
How Do Linux Binary Installers (.Bin, .Sh) Work
Linux/Unix Command to Determine If Process Is Running
How to Redirect Stderr and Stdout to Different Files in the Same Line in Script