ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
However, my suggestion was not:
stat -c %Y vs.exe | awk '{printf $1 "\n"}'
but:
stat -c %y vs.exe | awk '{printf $1 "\n"}'
Please note that letter "y" is lower case in my code. It makes a great difference!
As for the code: it uses awk, which I found one of the most useful tools (almost like grep or sed).
awk treats the input line as a (database) record containing fields, using (by default) the space character as the field separator (but a different field separator can be specified in the command).
Then you can refer to any field of the line using its index ($1, $2, $3, etc.)
Examples:
stat -c %y vs.exe | awk '{printf $1 ":" $3 "\n"}'
will print the first and the third fields on each line, putting ":" between the fields.
stat -c %y vs.exe | awk '{printf $1 ":" $3 }'
will print the same, but on one single output line, because there is no newline "\n" specified.
Apart from just printing specific parts (fields) of each line, you can perform calculations or sums based on them, and all in one command line.
I browsed google for a way to use a bash script for printing the date of files onto an html page I have going that indexes files in my directory. http://relik.ath.cx/unused/ should show you what Im talking about. Anways I wanted to know if there was a way I could make
MODDATE=$(stat -c %y test.txt)
MODDATE=${MODDATE%% *}
print out the time and maybe the permissions of the file. http://relik.ath.cx/unused/xhtml_html for anyone that wants a directory index just run this script in a place that doesn't have an index.html file you plan on keeping and contains html files you want listed.
Please note that this is a resurrected necro-thread.
@yech, please don't re-open old threads unless you have something substantial to add to that discussion. I think your comment (even though I agree with it totally) is borderline in that respect.
OTOH, it does allow me to make a substantial addition of my own :
Code:
stat --printf='%Y' file
The --printf option lets you control the output format you want to use more precisely. Add '\n' if you need a trailing newline.
$ stat -c %y File.jpg
2014-10-26 19:42:56.353367145 -0700
----------------------
I realize this is a very old thread, but the original question had "displays the date the photo was taken" and all the other answers would only work in optimal situations (never modified or copied the file) and would otherwise reflect a incorrect time.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.