Get Last Modified Date of File in Linux
Jestem nowy w Linuksie. Używam linii poleceń. Próbuję wyświetlić ostatnią zmodyfikowaną datę pliku. Jak mogę to zrobić w Linuksie z linii poleceń?
Jestem nowy w Linuksie. Używam linii poleceń. Próbuję wyświetlić ostatnią zmodyfikowaną datę pliku. Jak mogę to zrobić w Linuksie z linii poleceń?
Jak wspomniał @edvinas.me, stat mówi różne informacje o pliku, w tym o ostatniej zmodyfikowanej dacie.
Na początku byłem zmieszany z Modify i Change, tylko po to, aby wyjaśnić, stat listy wyjściowe:
Na przykład:
~ $ touch foo
~ $ stat foo
File: ‘foo’
Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: fc01h/64513d Inode: 410397 Links: 1
Access: (0644/-rw-r--r--) Uid: (80972/ etomort) Gid: (18429/ eem_tw)
Access: 2015-09-21 12:06:11.343616258 +0200
Modify: 2015-09-21 12:06:11.343616258 +0200
Change: 2015-09-21 12:06:11.343616258 +0200
Birth: -
~ $ echo "Added bar to foo file" >> foo
~ $ stat foo
File: ‘foo’
Size: 42 Blocks: 8 IO Block: 4096 regular file
Device: fc01h/64513d Inode: 410654 Links: 1
Access: (0644/-rw-r--r--) Uid: (80972/ etomort) Gid: (18429/ eem_tw)
Access: 2015-09-21 12:09:31.298712951 +0200
Modify: 2015-09-21 12:09:31.298712951 +0200
Change: 2015-09-21 12:09:31.302713093 +0200
Birth: -
~ $ chmod 444 foo
~ $ stat foo
File: ‘foo’
Size: 42 Blocks: 8 IO Block: 4096 regular file
Device: fc01h/64513d Inode: 410654 Links: 1
Access: (0444/-r--r--r--) Uid: (80972/ etomort) Gid: (18429/ eem_tw)
Access: 2015-09-21 12:09:31.298712951 +0200
Modify: 2015-09-21 12:09:31.298712951 +0200
Change: 2015-09-21 12:10:16.040310543 +0200
Birth: -
ls -l powinien wykonać tę pracę.
Przykład:
#> ls -l /home/TEST/
total 16
-rw-r--r-- 1 rfmas1 nms 949 Nov 16 12:21 create_nd_lists.py
-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 enb_list
-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 nb_list
-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 nodes_ip.txt
-rw-r--r-- 1 rfmas1 nms 0 Nov 16 12:35 rnc_list
@Adam Taylor’s comment in @phoops’s answer and @Sparhawk’s answer.
To specifically just get the date (używając 3 października 2019 na przykład, bo to były moje ostatnie urodziny, oto moje venmo, jeśli czujesz, że doprowadziło mnie to do finansowego błogosławieństwa: @levi_uzodike)
stat -c %y file | cut -d' ' -f1 da ci 2019-10-03 date +%F -r file da ci 2019-10-03 date +%D -r file da ci 10/03/19 date +%x -r file da ci 10/03/2019, lub 10/03/19 jeśli jesteś w USA i albo 03/10/2019, lub 03/10/19 jeśli jesteś w Wielkiej Brytanii, aby wymienić tylko kilka przykładów (oczywiście jest więcej możliwości) Te opcje formatu date są, w moim rozumieniu, kombinacją innych opcji formatu. Oto kilka wyjaśnień z strona man :
%b locale’s skrócona nazwa miesiąca (np. Jan) %B locale’s pełna nazwa miesiąca (np, styczeń) … %d dzień miesiąca (np. 01) %D data; taka sama jak %m/%d/%y %e dzień miesiąca, miejsce wyściełane; taka sama jak %_d %F pełna data; taka sama jak %Y-%m-%d … %m miesiąc (01..12) … %x data lokalizacji (np, 12/31/99) … %y ostatnie dwie cyfry roku (00..99) %Y rok … Domyślnie, pola numeryczne datowników z zerem.
Następujące opcjonalne flagi mogą być następujące `%‘:
- (hyphen) do not pad the field
_ (underscore) pad with spaces
0 (zero) pad with zeros
^ use upper case if possible
# use opposite case if possible
N.B.: These flags don’t work on the “combo formats” like %F, %D and %x. They are for the “singular field formats”.
Apparently this last flag ( # ) does not work as I’d expect (e.g., if date +%b gives Oct, date +%#b gives OCT as opposed to oCT) I guess this would be useless, but I’d think a lower case option would be more useful. date +%#pdoes turn date +%p which might give PM or AM into pm or am, respectively. So I guess it’s not a 'per-character’ case switch but sets the case of all the characters in the string to the opposite case of the majority of the characters? Also date +%P gives pm or am, but neither date +%^P nor date +%#P change its output. My guess for this case is that %P is just an alias for %#p, and it seems that whenever you add more than one flag, the behavior is undefined/unpredictable ( e.g., date +%0- e gives the same as date +%-e: 3 and date +%-0e gives the same as date +%0e: 03, which makes you think that only the flag next to the letter works or that it goes left to right, but both date +%#^p and date +%^#p give pm or am, [depending on the time of course] ) unless there’s some hidden order of operations? Sorry for digressing…
Also, if you run the command locale -k LC_TIME | grep ^d_fmt, you can see the combo for the specific locale of your system (e.g., d_fmt="%m/%d/%Y").
And you can make your own combo. For example,
date +%^b\ %-e\ %Y -r file will give you OCT 3 2019Jeśli plik jest na innym serwerze, podoba mi się httpie docs ).
pip install httpie --user
Komenda -h daje tylko nagłówek. Wzorzec to
http -h [url] | grep 'Last-Modified\|Date'
Przykład:
$ http -h https://martin-thoma.com/author/martin-thoma/ | grep 'Last-Modified\|Date'
Date: Fri, 06 Jan 2017 10:06:43 GMT
Last-Modified: Fri, 06 Jan 2017 07:42:34 GMT
Date jest ważny, ponieważ raportuje czas serwera, a nie czas lokalny. Ponadto, nie każdy serwer wysyła Last-Modified (np. superuser wydaje się tego nie robić).
1) Listę plików z ostatnio zmodyfikowaną datą/czasem
Aby wyświetlić listę plików i pokazać ostatnio zmodyfikowane pliki na górze, użyjemy opcji
-ltza pomocą polecenials.$ ls -lt /run output total 24 -rw-rw-r--. 1 root utmp 2304 Sep 8 14:58 utmp -rw-r--r--. 1 root root 4 Sep 8 12:41 dhclient-eth0.pid drwxr-xr-x. 4 root root 100 Sep 8 03:31 lock drwxr-xr-x. 3 root root 60 Sep 7 23:11 user drwxr-xr-x. 7 root root 160 Aug 26 14:59 udev drwxr-xr-x. 2 root root 60 Aug 21 13:18 tunedhttps://linoxide.com/linux-how-to/how-sort-files-date-using-ls-command-linux/