Ottieni l'ultima data modificata del file in Linux
Sono nuovo su Linux. Sto usando la riga di comando. Sto cercando di visualizzare l'ultima data modificata di un file. Come faccio a farlo in Linux dalla riga di comando?
Sono nuovo su Linux. Sto usando la riga di comando. Sto cercando di visualizzare l'ultima data modificata di un file. Come faccio a farlo in Linux dalla riga di comando?
Come menzionato da @edvinas.me, stat fornisce varie informazioni sul file, compresa l'ultima data modificata.
All'inizio ero confuso con Modify e Change, tanto per chiarire, stat mostra l'ora dell'ultimo accesso ai dati (e.
- Modifica mostra l'ora dell'ultima modifica dei dati.
- Cambia mostra l'ora dell'ultima modifica dello stato del file.
Per esempio:
~ $ 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: -
Un altro modo più flessibile è l'utilizzo di date -r. Da man date:
-r, --reference=FILE
display the last modification time of FILE
``` ```
$ date -r foo
Thu Aug 31 10:36:28 AEST 2017
$ date -r foo -R
Thu, 31 Aug 2017 10:36:28 +1000
$ date -r foo -u
Thu Aug 31 00:36:28 UTC 2017
$ date -r foo +%s
1504139788
Questo ha il vantaggio di consentire di specificare il formato di uscita, ad esempio
&001
ls -l dovrebbe fare il lavoro.
Esempio:
#> 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
Per ottenere in particolare la data (usando il 3 ottobre 2019 per esempio perché era il mio ultimo compleanno, ecco il mio venmo se vi sentite indotti a benedirmi finanziariamente: @levi_uzodike)
stat -c %y file | cut -d' ' -f1 vi darà 2019-10-03 date +%F -r file vi darà anche 2019-10-03 date +%D -r file vi darà 10/03/19 date +%x -r file vi darà 10/03/2019 10/03/19 vi darà probabilmente 03/10/2019, o 03/10/19 se siete negli Stati Uniti e date, o `%‘:
>
> - (hyphen) do not pad the fieldN.B.: These flags don’t work on the “combo formats” like ` se siete nel Regno Unito, solo per citare un paio di esempi (naturalmente ci sono più possibilità)
Queste opzioni di formato , sono, per quanto ne so, combinazioni di altre opzioni di formato. Ecco alcune spiegazioni dalla man page :
%b nome del mese abbreviato del locale (ad esempio, Jan) %B nome del mese completo del locale (ad esempio, Gennaio) … %d giorno del mese (es. 01) %D data; come %m/%d/%y %e giorno del mese, spazio imbottito; come %d %F data completa; come %Y-%m-%d … %m mese (01..12) … … %x rappresentazione della data del locale (es, 12/31/99) … %y ultime due cifre dell'anno (00..99) %Y anno … %Y anno … Per impostazione predefinita, i campi numerici dei datari con gli zeri.
Possono seguire i seguenti flag opzionaliand%F`. They are for the “singular field formats_”.
Apparently this last flag ( # ) does not work as I’d expect (e.g., if %D gives %D, %x gives date +%b as opposed to Oct) I guess this would be useless, but I’d think a lower case option would be more useful. date +%#bdoes turn OCT which might give oCT or date +%#p into date +%p or PM, 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 PM gives AM or pm, but neither am nor date +%P change its output. My guess for this case is that pm is just an alias for am, and it seems that whenever you add more than one flag, the behavior is undefined/unpredictable ( e.g., date +%^P gives the same as date +%#P: %P and %#p gives the same as date +%0- e: date +%-e, which makes you think that only the flag next to the letter works or that it goes left to right, but both 3 and date +%-0e give date +%0e or 03, [depending on the time of course] ) unless there’s some hidden order of operations? Sorry for digressing…
Also, if you run the command date +%#^p, you can see the combo for the specific locale of your system (e.g., date +%^#p).
And you can make your own combo. For example,
pm will give you am&007locale -k LC_TIME | grep ^d_fmt&007d_fmt="%m/%d/%Y"&007date +%^b\e\ %-e\ %Y -r file&007OCT 3 2019.Se il file è su un altro webserver, mi piace httpie docs ).
pip install httpie --user
Il comando -h dà solo l'intestazione. Il modello è
http -h [url] | grep 'Last-Modified\|Date'
``` ```
$ 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
Esempio:
&001
Il comando Date è importante in quanto riporta l'ora del server, non l'ora locale. Inoltre, non tutti i server inviano Last-Modified (ad esempio, il superutente sembra non farlo).
1) Elencare la directory dei file con Data e ora dell'ultima modifica
Per elencare i file e mostrare gli ultimi file modificati in alto, useremo le opzioni
-ltcon il comandols.$ 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/