I'm using bash to build a script where I will get a filename in a variable an then with this variable get the file unix last modification date.
I need to get this modification date value and I can't use stat command.
Do you know any way to get it with the common available *nix commands?
The lastModified() method of the File class returns the last modified time of the file/directory represented by the current File object. You can get the last modified time of a particular file using this method.
The syntax is pretty simple; just run the stat command followed by the file's name whose last modification date you want to know, as shown in the example below. As you can see, the output shows more information than previous commands.
To see the access time for a file with ls , append the -u option in your command. In this case, our access time is the same as the file's modified time, which is normal for files that have not been accessed since they were last saved. Yet another tool we can use is the date command.
$() – the command substitution. ${} – the parameter substitution/variable expansion.
Here we are going to see how to get the last modified date of the file in Linux, sometimes we may require timestamps of the file and apart from this it also ensures that we have the latest version of that file. Using Stat command. Using date command. Using ls -l command. Example 1: Using Stat command.
A shell script to display file date in following format: + Time of last access. + Time of last modification. + Time of last change. Please note that UNIX / Linux filesystem never stores file creation date / time stamp. This script use stat command to find out information about file date and time using custom field format.
A file timestamp change does not really imply you successfully copied. you will probably still see a timestamp change (need to confirm that). I do not get the exact context of your requirement. If you fire a cp, it completes and returns -- does not return while it is working.
The +%s tells date to output a UNIX time (the important bit is that it's an integer in seconds). You can also use stat to get this information - the command stat -c %Y <file> is equivalent. Make sure to use %Y not %y, so that you get a usable time in seconds. Show activity on this post. Show activity on this post.
ls
:Parsing ls
is a bad idea. Not only is the behaviour of certain characters in filenames undefined and platform dependant, for your purposes, it'll mess with dates when they're six months in the past. In short, yes, it'll probably work for you in your limited testing. It will not be platform-independent (so no portability) and the behaviour of your parsing is not guaranteed given the range of 'legal' filenames on various systems. (Ext4, for example, allows spaces and newlines in filenames).
Having said all that, personally, I'd use ls
because it's fast and easy ;)
As pointed out by Hugo in the comments, the OP doesn't want to use stat
. In addition, I should point out that the below section is BSD-stat specific (the %Sm
flag doesn't work when I test on Ubuntu; Linux has a stat
command, if you're interested in it read the man page).
stat
solution: use datedate
, at least on Linux, has a flag: -r
, which according to the man page:
display the last modification time of FILE
So, the scripted solution would be similar to this:
date -r ${MY_FILE_VARIABLE}
which would return you something similar to this:
zsh% date -r MyFile.foo
Thu Feb 23 07:41:27 CST 2012
To address the OP's comment:
If possible with a configurable date format
date
has a rather extensive set of time-format variables; read the man page for more information.
I'm not 100% sure how portable date
is across all 'UNIX-like systems'. For BSD-based (such as OS X), this will not work; the -r
flag for the BSD-date does something completely different. The question doesn't' specify exactly how portable a solution is required to be. For a BSD-based solution, see the below section ;)
Use stat
. You can format the output of stat
with the -f
flag, and you can select to display only the file modification data (which, for this question, is nice).
For example, stat -f "%m%t%Sm %N" ./*
:
1340738054 Jun 26 21:14:14 2012 ./build
1340738921 Jun 26 21:28:41 2012 ./build.xml
1340738140 Jun 26 21:15:40 2012 ./lib
1340657124 Jun 25 22:45:24 2012 ./tests
Where the first bit is the UNIX epoch time, the date is the file modification time, and the rest is the filename.
Breakdown of the example command
stat -f "%m%t%Sm %N" ./*
stat -f
: call stat
, and specify the format (-f
). %m
: The UNIX epoch time. %t
: A tab seperator in the output. %Sm
: S
says to display the output as a string
, m
says to use the file modification data. %N
: Display the name of the file in question. A command in your script along the lines of the following:
stat -f "%Sm" ${FILE_VARIABLE}
will give you output such as:
Jun 26 21:28:41 2012
Read the man page for stat
for further information; timestamp formatting is done by strftime
.
have perl?
perl -MFile::stat -e "print scalar localtime stat('FileName.txt')->mtime"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With