2014-04-03 12:45:56 +0000 2014-04-03 12:45:56 +0000
166
166

Obtener la última fecha de modificación del archivo en Linux

Soy nuevo en Linux. Estoy usando la línea de comandos. Estoy intentando ver la última fecha de modificación de un archivo. ¿Cómo lo hago en Linux desde la línea de comandos?

Respuestas (7)

147
147
147
2015-09-21 10:22:09 +0000

Como lo menciona @edvinas.me, stat te dice varias informaciones sobre el archivo incluyendo la última fecha de modificación.

Al principio, me confundí con Modificar y Cambiar, sólo para aclarar, stat listas de salida:

  • Acceso muestra la hora del último acceso a los datos (e. g. leer).
  • Modificar muestra la hora de la última modificación de datos.
  • Cambiar muestra la hora de la última modificación del estado del archivo.

Por ejemplo:

~ $ 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: -
69
69
69
2014-04-03 12:47:41 +0000

Use el comando stat para eso:

$ stat file
40
40
40
2017-08-31 01:04:43 +0000

Otra forma más flexible es usar date -r. A partir de man date:

-r, --reference=FILE
       display the last modification time of FILE

Esto tiene la ventaja de permitirle especificar el formato de salida, por ejemplo

$ 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
17
17
17
2015-11-16 05:43:54 +0000

ls -l debería hacer el trabajo.

Ejemplo:

#> 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
3
3
3
2019-08-14 16:24:39 +0000

Construyendo a partir del comentario de Adam Taylor en la respuesta de @phoops y la respuesta de @Sparhawk.

Para obtener específicamente la fecha ** (usando el 3 de octubre de 2019 como ejemplo porque fue mi último cumpleaños, aquí está mi venmo si te sientes llevado a bendecirme financieramente: @levi_uzodike)

  • stat -c %y file | cut -d' ' -f1 te dará 2019-10-03
  • date +%F -r file también te dará 2019-10-03
  • date +%D -r file te dará 10/03/19
  • date +%x -r file te dará probablemente o 10/03/2019 si estás en EEUU y o 10/03/19, o 03/10/2019 si estás en el Reino Unido, sólo para nombrar un par de ejemplos (por supuesto que hay más posibilidades)

Estas opciones de formato de 03/10/19 son, a mi entender, combinaciones de otras opciones de formato. Aquí hay algunas explicaciones de la página man :

%b nombre del mes abreviado de la localidad (por ejemplo, Enero) %B nombre del mes completo de la localidad (por ejemplo, Enero) … %d día del mes (ej., 01) %D fecha; igual que %m/%d/%y %e día del mes, espacio acolchado; igual que %_d %F fecha completa; igual que %Y-%m-%d … %m mes (01..12) … … %x representación de la fecha del locale (ej.., 31/12/99) … %y dos últimos dígitos del año (00..99) %Y año … Por defecto, la fecha rellena los campos numéricos con ceros.
Las siguientes banderas opcionales pueden seguir date%F`%‘:

  • (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 %D, %x and date +%b. They are for the “singular field formats”.

Apparently this last flag ( # ) does not work as I’d expect (e.g., if Oct gives date +%#b, OCT gives oCT as opposed to date +%#p) I guess this would be useless, but I’d think a lower case option would be more useful. fecha +%pdoes turn PM which might give AM or pm into am or fecha +%P, 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 fecha +%^P, but neither fecha +%#P nor %P change its output. My guess for this case is that %#p is just an alias for fecha +%0- e, and it seems that whenever you add more than one flag, the behavior is undefined/unpredictable ( e.g., date +%-e gives the same as 3: date +%-0e and date +%0e gives the same as 03: date +%#^p, 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 pm give am or `locale -k LCTIME | grep ^dfmt`, [depending on the time of course] ) unless there’s some hidden order of operations? Sorry for digressing…

Also, if you run the command d_fmt="%m/%d/%Y", you can see the combo for the specific locale of your system (e.g., date +%^b\ %-e\ %Y -r file).

And you can make your own combo. For example,

  • OCT 3 2019
2
2
2
2017-01-06 10:08:23 +0000

Si el archivo está en otro servidor web, me gusta httpie docs ).

Instalación

pip install httpie --user

Uso

El comando -h sólo da el encabezado. El patrón es

http -h [url] | grep 'Last-Modified\|Date'

Ejemplo:

$ 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

El Date es importante ya que reporta la hora del servidor, no su hora local. Además, no todos los servidores envían Last-Modified (por ejemplo, el superusuario parece no hacerlo).

2
2
2
2018-11-14 04:22:35 +0000

1) Listar el directorio de archivos con la fecha/hora de la última modificación

Para listar los archivos y mostrar los últimos archivos modificados en la parte superior, usaremos las opciones -lt con el comando ls.

$ 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 tuned

https://linoxide.com/linux-how-to/how-sort-files-date-using-ls-command-linux/

Preguntas relacionadas

6
10
7
5
12