2012-02-07 14:59:56 +0000 2012-02-07 14:59:56 +0000
54
54
Advertisement

Come controllare tutti i timestamp di un file?

Advertisement

C'è un comando in Linux per controllare tutti i timestamp di un file?

Sto cercando di vedere le date di ultima modifica, creazione e tocco del file.

Advertisement
Advertisement

Risposte (2)

75
75
75
2012-02-07 15:03:39 +0000

Il comando si chiama stat .

$ stat test
234881026 41570368 -rw-r--r-- 1 werner staff 0 0 "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" "Feb 7 16:03:06 2012" 4096 0 0 test

Se volete regolare il formato, fate riferimento alle pagine man, poiché l'output è specifico del sistema operativo e varia sotto Linux/Unix.

Generalmente, potete ottenere i tempi anche attraverso un normale elenco di directory:

  • ls -l emette l'ultima volta che il contenuto del file è stato modificato, lo mtime
  • ls -lc emette l'ultimo tempo di modifica dello stato del file, lo ctime Qual è la differenza? )
  • ls -lu emette l'ultimo tempo di accesso, lo atime (sebbene l'utilità di questo concetto sia soggetto a discussione )

E naturalmente, ctime non registra quando un file è stato “creato”. La specifica POSIX definisce solo tre timestamp, ma alcuni filesystem Linux memorizzano l'ora di nascita/di creazione. Come trovare la data di creazione di un file? Su una tale configurazione supportata, si potrebbe usare

stat --printf '%n\nmtime: %y\nctime: %z\natime: %x\ncrtime:%w\n'
21
21
21
2014-03-15 09:24:38 +0000

Ci sono solo TRE valori di tempo distinti memorizzati per ciascuno dei vostri file, come definito dallo Standard POSIX: http://pubs.opengroup.org/onlinepubs/9699919799/ (vedere la sezione Definizioni di base -> 4.

Ogni file ha tre distinti timestamp associati: l'ora dell'ultimo accesso ai dati , l'ora dell'ultima modifica dei dati , e l'ora dell'ultima modifica dello stato del file. Questi valori sono restituiti nella struttura struct stat delle caratteristiche del file, come descritto in <sys/stat.h> _.

E da <sys/stat.h> :

atime is for Last data access timestamp.
mtime is for Last data modification timestamp.
ctime is for Last file status change timestamp.
  • *

I seguenti esempi mostrano la differenza tra atime , mtime e ctime , questi esempi sono in GNU/Linux BASH. Puoi usare stat -x in Mac OS X o altre Dist. BSD per vedere un formato di output simile.

$ stat --version
stat (GNU coreutils) 8.4
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Michael Meskes.
$
$ touch test
$ stat test
  File: `test'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:58:28.609223953 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800

Quando il file appena creato, tre timestamp sono uguali.


1. atime

Per prima cosa, accediamo ai dati del file leggendolo (less o vim), stampandolo (cat) o copiandolo in un altro file (cp).

$ cat test #Nothing will be printed out, since the file is empty
$ stat test
  File: `test'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800 <-- atime Changed!
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 10:58:28.609223953 +0800

2. ctime

Ora cambiamo lo stato del file, cambiando il permesso (chmod) o rinominandolo (mv)

$ chmod u+x test
$ stat stet
  File: `test'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:04:10.178285430 +0800 <-- ctime Changed!
$    
$ mv test testing
$ stat testing
  File: `testing'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 10:58:28.609223953 +0800
Change: 2014-03-16 11:06:33.342207679 +0800 <-- ctime Changed again!

Nota che fino ad ora, il contenuto ( dati ) del file è ancora lo stesso di quando è stato creato.


3. mtime

Infine, modifichiamo il contenuto del file modificandolo.

$ echo 'Modify the DATA of the file' > testing
$ echo 'Modify the DATA of the file also change the file status' > testing
$ stat testing
  File: `testing'
  Size: 56 Blocks: 8 IO Block: 4096 regular file
Device: 811h/2065d Inode: 98828525 Links: 1
Access: (0764/-rwxrw-r--) Uid: ( 514/ rank) Gid: ( 514/ rank)
Access: 2014-03-16 10:59:13.182301069 +0800
Modify: 2014-03-16 11:09:48.247345148 +0800 <-- mtime Changed!
Change: 2014-03-16 11:09:48.247345148 +0800 <-- ctime also Changed!

4. birth time

Notate anche che la versione più recente di stat (per esempio stat --version 8.13 in Ubuntu 12.04) ha la quarta informazione di timestamp - il Birth Time (tempo di creazione del file). Anche se potrebbe non mostrare il tempo corretto per ora:

$ stat --version
stat (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Michael Meskes.
$
$ stat birth_time
  File: `birth_time'
  Size: 0 Blocks: 0 IO Block: 4096 regular empty file
Device: 805h/2053d Inode: 4073946 Links: 1
Access: (0664/-rw-rw-r--) Uid: ( 1000/ bingyao) Gid: ( 1000/ bingyao)
Access: 2014-03-16 10:46:48.838718970 +0800
Modify: 2014-03-16 10:46:48.838718970 +0800
Change: 2014-03-16 10:46:48.838718970 +0800
 Birth: -
Advertisement

Questions connexes

6
10
5
37
5
Advertisement