Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get mtime of specific file using Bash?

Tags:

linux

bash

shell

I am well aware of being able to do find myfile.txt -mtime +5 to check if my file is older than 5 days or not. However I would like to fetch mtime in days of myfile.txt and store it into a variable for further usage. How would I do that?

like image 304
Industrial Avatar asked Jan 23 '11 14:01

Industrial


People also ask

How do you get Mtime of a file in Linux?

Modified timestamp (mtime) indicates the last time the contents of a file were modified. For example, if new contents were added, deleted, or replaced in a file, the modified timestamp is changed. To view the modified timestamp, we can simple use the ls command with -l option.

What is Mtime in find command?

find command has a great operator for narrowing down the list of results: mtime. as you probably know from the atime, ctime and mtime post, the mtime is a file property confirming the last time the file was modified. find uses mtime option to identify files based on when they were modified.

How do I find the date a file was created?

The easiest way to get the file creation date is with the stat command. As we can see, the creation date is shown in the “Birth” field.

How can I tell who created a file in Linux?

A. You can use ls -l command (list information about the FILEs) to find our the file / directory owner and group names. The -l option is known as long format which displays Unix / Linux / BSD file types, permissions, number of hard links, owner, group, size, date, and filename.


4 Answers

stat can give you that info:

filemtime=$(stat -c %Y myfile.txt)

%Y gives you the last modification as "seconds since The Epoch", but there are lots of other options; more info. So if the file was modified on 2011-01-22 at 15:30 GMT, the above would return a number in the region of 1295710237.

Edit: Ah, you want the time in days since it was modified. That's going to be more complicated, not least because a "day" is not a fixed period of time (some "days" have only 23 hours, others 25 — thanks to daylight savings time).

The naive version might look like this:

filemtime=$(stat -c %Y "$1")
currtime=$(date +%s)
diff=$(( (currtime - filemtime) / 86400 ))
echo $diff

...but again, that's assuming a day is always exactly 86,400 second long.

More about arithmetic in bash here.

like image 132
T.J. Crowder Avatar answered Oct 03 '22 23:10

T.J. Crowder


The date utility has a convenient switch for extracting the mtime from a file, which you can then display or store using a format string.

date -r file "+%F"
# 2021-01-12
file_mtime=$(date -r file "+%F")

See man date, the output of date is controlled by a format string beginning with "+"

Useful format strings for comparing many dates might include:

"+%j": day of year
"+%s": unix epoch time

Arithmetic with dates is a bit of a pain in bash, so if you need relative time that will work in all corner cases, you may be better off with another language.

like image 42
De Novo Avatar answered Oct 04 '22 00:10

De Novo


AGE=$(perl -e 'print -M $ARGV[0]' $file)

will set $AGE to the age of $file in days, as Perl's -M operator handles the stat call and the conversion to days for you.

The return value is a floating-point value (e.g., 6.62849537 days). Add an int to the expression if you need to have an integer result

AGE=$(perl -e 'print int -M $ARGV[0]' $file)

Ruby and Python also have their one-liners to stat a file and return some data, but I believe Perl has the most concise way.

like image 28
mob Avatar answered Oct 04 '22 01:10

mob


I this the answer?

A=$(stat -c "%y" myfile.txt)

look at stat-help

stat --help
Usage: stat [OPTION]... FILE...
Display file or file system status.
[...]
-c  --format=FORMAT   use the specified FORMAT instead of the default;
                      output a newline after each use of FORMAT
[...]
The valid format sequences for files
[...]
  %y   Time of last modification, human-readable
  %Y   Time of last modification, seconds since Epoch
[...]
like image 43
silvio Avatar answered Oct 04 '22 00:10

silvio