En GNU/Linux se utiliza el comando ls para listar el contenido de directorios. En este artículo muestro algunos ejemplos para obtener información útil acerca de los directorios utilizando ls.
La sintaxis de ls es simple:
ls [opciones] [archivo]
Es posible listar archivos o directorios. Si no se especifica un archivo, se muestra el contenido del directorio actual.
Para comenzar, un ejemplo de salida de ls sin opciones. Listar el directorio "/usr":
$ ls /usr bin etc games include lib lib64 libexec local lost+found sbin share src tmp
Listar directorio utilizando una línea por cada ítem: opción -1
$ ls -1 /usr bin etc games include lib lib64 libexec local lost+found sbin share src tmp
Listar directorio utilizando una línea por cada ítem junto con los permisos, usuario, grupo, tamaño, fecha de modificación: opción -l
$ ls -l /usr total 244 dr-xr-xr-x. 2 root root 69632 Sep 27 13:44 bin drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 127 root root 12288 Sep 26 13:21 include dr-xr-xr-x. 28 root root 4096 Jul 12 12:08 lib dr-xr-xr-x. 146 root root 81920 Sep 27 08:43 lib64 drwxr-xr-x. 34 root root 12288 Sep 27 08:43 libexec drwxr-xr-x. 12 root root 4096 Jun 21 04:23 local drwx------. 2 root root 16384 Jun 21 03:52 lost+found dr-xr-xr-x. 2 root root 16384 Sep 27 08:44 sbin drwxr-xr-x. 299 root root 12288 Aug 22 11:59 share drwxr-xr-x. 4 root root 4096 Jun 21 04:23 src lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp
Ordenar listado por fecha de modificación (se muestran primero los archivos más recientes): opción -t
$ ls -lt /usr total 244 dr-xr-xr-x. 2 root root 69632 Sep 27 13:44 bin dr-xr-xr-x. 2 root root 16384 Sep 27 08:44 sbin drwxr-xr-x. 34 root root 12288 Sep 27 08:43 libexec dr-xr-xr-x. 146 root root 81920 Sep 27 08:43 lib64 drwxr-xr-x. 127 root root 12288 Sep 26 13:21 include drwxr-xr-x. 299 root root 12288 Aug 22 11:59 share dr-xr-xr-x. 28 root root 4096 Jul 12 12:08 lib drwxr-xr-x. 4 root root 4096 Jun 21 04:23 src lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp drwxr-xr-x. 12 root root 4096 Jun 21 04:23 local drwx------. 2 root root 16384 Jun 21 03:52 lost+found drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games
Es posible combinar la opción "-t" con la opción "-r" (orden reverso) para mostrar primero los más antiguos: opción -r
$ ls -ltr /usr/ total 244 drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwx------. 2 root root 16384 Jun 21 03:52 lost+found drwxr-xr-x. 12 root root 4096 Jun 21 04:23 local lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp drwxr-xr-x. 4 root root 4096 Jun 21 04:23 src dr-xr-xr-x. 28 root root 4096 Jul 12 12:08 lib drwxr-xr-x. 34 root root 12288 Sep 27 08:43 libexec dr-xr-xr-x. 2 root root 16384 Sep 27 08:44 sbin dr-xr-xr-x. 147 root root 81920 Sep 28 08:47 lib64 drwxr-xr-x. 127 root root 12288 Sep 28 08:47 include drwxr-xr-x. 299 root root 12288 Sep 28 08:47 share dr-xr-xr-x. 2 root root 69632 Sep 28 09:06 bin
Mostrar tamaños en unidades amigables (KB, MB, GB): opción -h
$ ls -lth /usr total 244K dr-xr-xr-x. 2 root root 68K Sep 27 13:44 bin dr-xr-xr-x. 2 root root 16K Sep 27 08:44 sbin drwxr-xr-x. 34 root root 12K Sep 27 08:43 libexec dr-xr-xr-x. 146 root root 80K Sep 27 08:43 lib64 drwxr-xr-x. 127 root root 12K Sep 26 13:21 include drwxr-xr-x. 299 root root 12K Aug 22 11:59 share dr-xr-xr-x. 28 root root 4.0K Jul 12 12:08 lib drwxr-xr-x. 4 root root 4.0K Jun 21 04:23 src lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp drwxr-xr-x. 12 root root 4.0K Jun 21 04:23 local drwx------. 2 root root 16K Jun 21 03:52 lost+found drwxr-xr-x. 2 root root 4.0K Sep 23 2011 etc drwxr-xr-x. 2 root root 4.0K Sep 23 2011 games
No ignorar las entradas que comienzan con "." y "..": opción -a
$ ls -ltha /usr total 252K dr-xr-xr-x. 2 root root 68K Sep 27 13:44 bin dr-xr-xr-x. 2 root root 16K Sep 27 08:44 sbin drwxr-xr-x. 34 root root 12K Sep 27 08:43 libexec dr-xr-xr-x. 146 root root 80K Sep 27 08:43 lib64 dr-xr-xr-x. 27 root root 4.0K Sep 27 07:36 .. drwxr-xr-x. 127 root root 12K Sep 26 13:21 include drwxr-xr-x. 299 root root 12K Aug 22 11:59 share dr-xr-xr-x. 28 root root 4.0K Jul 12 12:08 lib drwxr-xr-x. 14 root root 4.0K Jun 21 04:23 . drwxr-xr-x. 4 root root 4.0K Jun 21 04:23 src lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp drwxr-xr-x. 12 root root 4.0K Jun 21 04:23 local drwx------. 2 root root 16K Jun 21 03:52 lost+found drwxr-xr-x. 2 root root 4.0K Sep 23 2011 etc drwxr-xr-x. 2 root root 4.0K Sep 23 2011 games
Mostrar "casi todas" las entradas (todos los archivos, incluyendo los que comienzan por ".", excepto "." y ".."). Al igual que la opción "-a" se muestran los archivos cuyo nombre comienza por punto, por ejemplo si en el directorio "/usr" hubiera un archivo llamado ".config" sería listado, aunque no lista las entradas "." (directorio actual) ni ".." (directorio padre): opción -A
$ ls -lA /usr total 244 dr-xr-xr-x. 2 root root 69632 Sep 28 09:06 bin drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 127 root root 12288 Sep 28 08:47 include dr-xr-xr-x. 28 root root 4096 Jul 12 12:08 lib dr-xr-xr-x. 147 root root 81920 Sep 28 08:47 lib64 drwxr-xr-x. 34 root root 12288 Sep 27 08:43 libexec drwxr-xr-x. 12 root root 4096 Jun 21 04:23 local drwx------. 2 root root 16384 Jun 21 03:52 lost+found dr-xr-xr-x. 2 root root 16384 Sep 27 08:44 sbin drwxr-xr-x. 299 root root 12288 Sep 28 08:47 share drwxr-xr-x. 4 root root 4096 Jun 21 04:23 src lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp
Listar primero los directorios: opción --group-directories-first
$ ls -l --group-directories-first /usr/ total 244 dr-xr-xr-x. 2 root root 69632 Sep 28 09:06 bin drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 127 root root 12288 Sep 28 08:47 include dr-xr-xr-x. 28 root root 4096 Jul 12 12:08 lib dr-xr-xr-x. 147 root root 81920 Sep 28 08:47 lib64 drwxr-xr-x. 34 root root 12288 Sep 27 08:43 libexec drwxr-xr-x. 12 root root 4096 Jun 21 04:23 local drwx------. 2 root root 16384 Jun 21 03:52 lost+found dr-xr-xr-x. 2 root root 16384 Sep 27 08:44 sbin drwxr-xr-x. 299 root root 12288 Sep 28 08:47 share drwxr-xr-x. 4 root root 4096 Jun 21 04:23 src lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp
Ordenar por tamaño (de mayor a menor, también es posible combinar con "-r"): opción -S
$ ls -lS /usr/ total 244 dr-xr-xr-x. 147 root root 81920 Sep 28 08:47 lib64 dr-xr-xr-x. 2 root root 69632 Sep 28 09:06 bin drwx------. 2 root root 16384 Jun 21 03:52 lost+found dr-xr-xr-x. 2 root root 16384 Sep 27 08:44 sbin drwxr-xr-x. 127 root root 12288 Sep 28 08:47 include drwxr-xr-x. 34 root root 12288 Sep 27 08:43 libexec drwxr-xr-x. 299 root root 12288 Sep 28 08:47 share drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games dr-xr-xr-x. 28 root root 4096 Jul 12 12:08 lib drwxr-xr-x. 12 root root 4096 Jun 21 04:23 local drwxr-xr-x. 4 root root 4096 Jun 21 04:23 src lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp
Como último ejemplo, es posible obtener un listado del directorio con las entradas separadas por coma: opción -m
$ ls -m /usr/ bin, etc, games, include, lib, lib64, libexec, local, lost+found, sbin, share, src, tmp
Finalmente podemos agregar un alias en el archivo ".bashrc" de nuestro profile para combinar las opciones que más nos gusten, por ejemplo:
echo alias l=\"ls -Alhp --group-directories-first\" >> $HOME/.bashrc
$ l /usr/ total 244K dr-xr-xr-x. 2 root root 68K Sep 28 09:06 bin/ drwxr-xr-x. 2 root root 4.0K Sep 23 2011 etc/ drwxr-xr-x. 2 root root 4.0K Sep 23 2011 games/ drwxr-xr-x. 127 root root 12K Sep 28 08:47 include/ dr-xr-xr-x. 28 root root 4.0K Jul 12 12:08 lib/ dr-xr-xr-x. 147 root root 80K Sep 28 08:47 lib64/ drwxr-xr-x. 34 root root 12K Sep 27 08:43 libexec/ drwxr-xr-x. 12 root root 4.0K Jun 21 04:23 local/ drwx------. 2 root root 16K Jun 21 03:52 lost+found/ dr-xr-xr-x. 2 root root 16K Sep 27 08:44 sbin/ drwxr-xr-x. 299 root root 12K Sep 28 08:47 share/ drwxr-xr-x. 4 root root 4.0K Jun 21 04:23 src/ lrwxrwxrwx. 1 root root 10 Jun 21 04:23 tmp -> ../var/tmp/