Linux file modification date

I can’t seem to find how to print out the date of a file. I’m so far able to print out all the files in a directory, but I need to print out the dates with it. I know I need to attach a date format with the echo of the entry, but all I can’t find the correct format.

echo "Please type in the directory you want all the files to be listed" read directory for entry in "$directory"/* do echo "$entry" done 

10 Answers 10

Isn’t the ‘date’ command much simpler? No need for awk, stat, etc.

Also, consider looking at the man page for date formatting; for example with common date and time format:

It looks like BSD (or at least OS X’s) date ‘s doesn’t have this. Its -r is just used to provide a timestamp to format. You’ll have to use GNU date to get this functionality.

In macOS 10.13 date [-r seconds | filename] is fully documented in the man page, and works as expected.

%y time of last modification, human-readable

for entry in «$directory»/* do stat -c%y «$entry» done Doesn’t work. Prints out stat: missing operand in terminal

Note that on BSD the stat command has a different syntax. My case for FreeBSD: stat -f %Sm -t %F» «%R filename .

Alternatively, you may try also :

On OS X, I like my date to be in the format of YYYY-MM-DD HH:MM in the output for the file.

So to specify a file I would use:

stat -f "%Sm" -t "%Y-%m-%d %H:%M" [filename] 

If I want to run it on a range of files, I can do something like this:

#!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i" done 

This example will print out the last time I ran the sudo periodic daily weekly monthly command as it references the log files.

To add the filenames under each date, I would run the following instead:

#!/usr/bin/env bash for i in /var/log/*.out; do stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i" echo "$i" done 

The output would was the following:

2016-40-01 16:40 /var/log/daily.out 2016-40-01 16:40 /var/log/monthly.out 2016-40-01 16:40 /var/log/weekly.out 

Unfortunately I’m not sure how to prevent the line break and keep the file name appended to the end of the date without adding more lines to the script.

PS — I use #!/usr/bin/env bash as I’m a Python user by day, and have different versions of bash installed on my system instead of #!/bin/bash

In case you haven’t figured out the line break part, the -e flag is available for the echo program. Example usage could be: echo -e «$(stat -f %Sm -t %Y%m%d_%H%M%S $AFile)\t$AFile.» . In your case it could be: echo -e «$(stat -f «%Sm» -t «%Y-%m-%d %H:%M» «$i»)\t$i.» `

Читайте также:  Линукс как проверить пинг

Adding to @StevePenny answer, you might want to cut the not-so-human-readable part:

stat -c%y Localizable.strings | cut -d'.' -f1 

For the line breaks i edited your code to get something with no line breaks.

#!/bin/bash for i in /Users/anthonykiggundu/Sites/rku-it/*; do t=$(stat -f "%Sm" -t "%Y-%m-%d %H:%M" "$i") echo $t : "$" # t only contains date last modified, then only filename 'grokked'- else $i alone is abs. path done 

If file name has no spaces:

This prints as the following format:

 Dec 21 20:03 a1.out Dec 21 20:04 a.cpp 

If file names have space (you can use the following command for file names with no spaces too, just it looks complicated/ugly than the former):

Ah, I see! That mostly works, except for the files with the spaces in the names. Is there a solution for that?

Actually, I tried it again, and it doesn’t work. turns out I was trying out a directory that had file names without spaces. =/ Edited my answer below

when you say it does not work, can you say what is the error? it works for me when i executed it on my system.

EDITED: turns out that I had forgotten the quotes needed for $entry in order to print correctly and not give the «no such file or directory» error. Thank you all so much for helping me!

 echo "Please type in the directory you want all the files to be listed with last modified dates" #bash can't find file creation dates read directory for entry in "$directory"/* do modDate=$(stat -c %y "$entry") #%y = last modified. Qoutes are needed otherwise spaces in file name with give error of "no such file" modDate=$ #%% takes off everything off the string after the date to make it look pretty echo $entry:$modDate 
/home/joanne/Dropbox/cheat sheet.docx:2012-03-14 /home/joanne/Dropbox/Comp:2013-05-05 /home/joanne/Dropbox/Comp 150 java.zip:2013-02-11 /home/joanne/Dropbox/Comp 151 Java 2.zip:2013-02-11 /home/joanne/Dropbox/Comp 162 Assembly Language.zip:2013-02-11 /home/joanne/Dropbox/Comp 262 Comp Architecture.zip:2012-12-12 /home/joanne/Dropbox/Comp 345 Image Processing.zip:2013-02-11 /home/joanne/Dropbox/Comp 362 Operating Systems:2013-05-05 /home/joanne/Dropbox/Comp 447 Societal Issues.zip:2013-02-11 

Источник

How to display modification time of a file?

I’d like a method to find and print the modified time of a file, for use within a bash script. I have come up with:

ls -l $filename | cut -d ' ' -f '6-8' 

Though I’d like to avoid parsing ls , also it’d be useful to have the year in there. Ideally I’d like to see an output similar to the default output of the date command.

Tue Jul 26 15:20:59 BST 2016 

2 Answers 2

Don’t use ls , this is a job for stat :

Читайте также:  Переменная home в линукс

-c lets us to get specific output, here %y will get us the last modified time of the file in human readable format. To get time in seconds since Epoch use %Y :

If you want the file name too, use %n :

stat -c '%y : %n' filename stat -c '%Y : %n' filename 

Set the format specifiers to suit your need. Check man stat .

% stat -c '%y' foobar.txt 2016-07-26 12:15:16.897284828 +0600 % stat -c '%Y' foobar.txt 1469513716 % stat -c '%y : %n' foobar.txt 2016-07-26 12:15:16.897284828 +0600 : foobar.txt % stat -c '%Y : %n' foobar.txt 1469513716 : foobar.txt 

If you want the output like Tue Jul 26 15:20:59 BST 2016 , use the Epoch time as input to date :

% date -d "@$(stat -c '%Y' a.out)" '+%a %b %d %T %Z %Y' Tue Jul 26 12:15:21 BDT 2016 % date -d "@$(stat -c '%Y' a.out)" '+%c' Tue 26 Jul 2016 12:15:21 PM BDT % date -d "@$(stat -c '%Y' a.out)" Tue Jul 26 12:15:21 BDT 2016 

Check date ‘s format specifiers to meet your need. See man date too.

Further to this answer, bear in mind that ls is inconsistently implemented across systems, and so should never be used for any sort of automation. Instead, use commands such as stat and find when writing scripts.

excellent. I had not known about this little treasure. So many of them in the *nix/linux environment yet to be discovered.

ls -l $filename | cut -d ' ' -f '6-8' 

but if the date is less than 10 it misses the time. This because of the extra space before the date if less than 10. Try this:

filename="test.txt" ls -l $filename | awk -F ' ' '' 

The awk command prints the fields separated by all spaces (-F ‘ ‘). Hope it works. I know this doesn’t answer the original question but just a clarification on the ls command for just date and time. When you Google «ubuntu get date and time of file» it lists this question at the top, which is what I was looking for, since I don’t need the year also. For year, date and time, you could try one of the commands below. %m prints month number. %b prints month abbreviation: Drop the %H:%M if you don’t need the hour and minute. %-d doesn’t print leading zero for the day of the month.

date -r $filename +"%Y %m %-d %H:%M" date -r $filename +"%y %m %-d %H:%M" date -r $filename +"%Y %b %-d %H:%M" date -r $filename +"%y %b %-d %H:%M" 

Источник

How to Get Last Modified Date of File in Linux

Sometimes, you may be required to check detailed information about a file (timestamp) such as its last modified date. This can come in handy when you want to check when the file was last edited. Additionally, it ensures that you have the latest version of the file.

Читайте также:  Dhcp enable in linux

In this guide, you will learn 4 ways to get the last modified date of file in Linux.

1. Using stat command

The ls -l command is just okay in giving you basic information about a file such as file ownership and permissions, file size, and creation date. The stat command returns detailed information file attributes such as the last time the file was accessed and modified.

The syntax is quite simple. stat is followed by the file name or the full path to the file.

stat command

From the above output, we can clearly see when the file was last accessed ( Access date ), Modify date, Change date among other parameters.

If you wish to view the modified date only and leave out all the other information, run the following command:

Use stat command to only check modified date

The -c option is used to return the date in a custom format, while the ‘%y’ flag displays the last modification time. For directories, the syntax remains the same. Simply replace the file name with that of the directory.

2. Using date command

The date command in its basic syntax displays the current date. However, when used with the -r option, you can display the last modification date of a file as shown.

date command to display last modified date

3. Using ls -l command

The ls -l command is usually used for long listing — display additional information about a file such as file ownership and permissions, size and creation date. To list and display the last modified times, use the lt option as shown.

check last modified file uing ls -lt

4. Using httpie

Another way you can check the last modified date is by using the httpie HTTP command-line client tool. The tool is usually used for interacting with HTTP servers and APIs and can also check when a file residing on a web server was last modified.

But first, you need to install it using the command:

On Ubuntu / Debian / Mint, run the command:

To check when a file on a web server was last modified, use the syntax:

http -h [url] | grep 'Last-Modified'
http -h https://example.com/wp-content/uploads/2020/09/Fedora-32-desktop.png | grep -i 'Last-Modified'
Output Last-Modified: Fri, 18 Sep 2020 22:38:48 GMT

Conclusion

In this guide, we have featured various ways that you can use to list the last modified date of a file on a Linux system, and even a file hosted on a web server using the httpie tool. Hopefully, you won’t have an issue viewing when files were last modified.

If this resource helped you, let us know your care by a Thanks Tweet. Tweet a thanks

Источник

Оцените статью
Adblock
detector