Cómo ordenar todos los archivos por tamaño usando el comando ls en Linux


los ls El comando es el comando más popular y muy útil para listar el contenido de directorios. En este artículo, explicaremos cómo usar ls opción de clasificación para enumerar el contenido del directorio por tamaño.
1) Listar archivos en el directorio por tamaño (clasificación)
Para listar un contenido de un directorio específico con clasificación por tamaño, usaremos -lS opciones con ls mando. Mostrará los archivos con el tamaño más alto en la parte superior.
$ ls -lS /run
output total 24 -rw-rw-r--. 1 root utmp 2304 Sep 8 14:58 utmp drwxr-xr-x. 16 root root 400 Aug 21 13:18 systemd drwxr-xr-x. 7 root root 160 Aug 26 14:59 udev drwxr-xr-x. 4 root root 100 Aug 21 13:18 initramfs drwxr-xr-x. 4 root root 100 Sep 8 03:31 lock drwxr-xr-x. 3 root root 100 Aug 21 13:18 NetworkManager drwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus drwxr-xr-x. 3 root root 60 Aug 21 13:18 log drwxr-xr-x. 2 root root 40 Aug 21 13:18 console drwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock drwxr-x---. 2 root root 40 Aug 21 13:18 firewalld To list with file size but it wont be sorted, we will use-soption withlscommand.
$ ls -s
2) Listar archivos con tamaño (clasificación inversa)
Para enumerar el contenido de un directorio específico con clasificación inversa de tamaño (es decir, los archivos de menor tamaño estarán en la parte superior), usaremos -lSr opciones con ls mando.
$ ls -lSr /run
output
total 24
-rw-------. 1 root root 0 Aug 21 13:18 xtables.lock
-rw-------. 1 root root 3 Aug 21 13:18 syslogd.pid
-rw-r--r--. 1 root root 4 Aug 21 13:18 sshd.pid
-rw-r--r--. 1 root root 4 Aug 21 13:18 auditd.pid
drwxr-xr-x. 2 root root 40 Aug 21 13:18 setrans
3) Clasifique la salida y los tamaños de impresión en formato legible por humanos (por ejemplo, 1K 48M 1G)
Para ordenar la salida y los tamaños de impresión en formato legible por humanos, usaremos -h opción con ls mando.
$ ls -lSh
output total 1.3M -rw-r--r--. 1 root root 1.1M Aug 26 15:45 GeoIP-1.5.0-11.el7.x86_64.rpm -rw-r--r--. 1 root root 177K Aug 26 15:29 index.html drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 apache2 drwxr-xr-x. 2 root root 4.0K Sep 8 13:31 Desktop drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Documents drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Downloads drwxr-xr-x. 2 root root 4.0K Sep 8 13:32 Pictures .......
Además, podemos imprimir tamaños en formato legible por humanos para una extensión específica.
ls -l -S -h *.mp3
ls -l -S -h ~/Downloads/*.mp4 | more
Algunos consejos básicos
4) Lista en orden alfabético
Para listar un contenido de un directorio específico con clasificación alfabética, usaremos ls comando solo sin opción, porque la clasificación alfabética es la predeterminada.
$ ls
output anaconda-ks.cfg Desktop echo.txt index.html Pictures smart.txt apache2 Documents f.txt nano.txt printf.txt vim.txt cat.txt Downloads GeoIP-1.5.0-11.el7.x86_64.rpm original-ks.cfg smart.docx vi.txt
Para enumerar el contenido de un directorio específico con detalles, agregue la ruta del directorio.
$ ls -l /run
output total 24 -rw-r--r--. 1 root root 4 Aug 21 13:18 auditd.pid drwxr-xr-x. 2 root root 40 Aug 21 13:18 console -rw-r--r--. 1 root root 4 Aug 21 13:18 crond.pid ----------. 1 root root 0 Aug 21 13:18 cron.reboot drwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus -rw-r--r--. 1 root root 4 Sep 9 08:17 dhclient-eth0.pid -rw-------. 1 root root 0 Aug 21 13:18 ebtables.lock drwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock drwxr-x---. 2 root root 40 Aug 21 13:18 firewalld ..........
5) Lista en orden alfabético inverso
Para enumerar el contenido de un directorio específico con detalles sobre la ordenación alfabética inversa, usaremos -lr opciones con ls mando.
$ ls -lr /run
output total 24 -rw-------. 1 root root 0 Aug 21 13:18 xtables.lock -rw-rw-r--. 1 root utmp 2304 Sep 8 14:58 utmp 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 drwxr-xr-x. 2 root root 60 Aug 21 13:18 tmpfiles.d drwxr-xr-x. 16 root root 400 Aug 21 13:18 systemd -rw-------. 1 root root 3 Aug 21 13:18 syslogd.pid drwx--x--x. 3 root root 60 Aug 21 13:18 sudo -rw-r--r--. 1 root root 4 Aug 21 13:18 sshd.pid drwxr-xr-x. 2 root root 40 Aug 21 13:18 setrans drwxr-xr-x. 2 root root 40 Aug 21 13:18 sepermit drwxr-xr-x. 2 root root 40 Aug 21 13:18 plymouth .......
6) Enumere el contenido oculto del directorio en orden alfabético
Para enumerar los contenidos ocultos de un directorio específico, usaremos -a o --all opciones con ls mando.
$ ls -a /etc
output . default gss logrotate.d pm rsyslog.conf sysctl.d .. depmod.d host.conf machine-id polkit-1 rsyslog.d systemd adjtime dhcp hostname magic popt.d rwtab system-release aliases DIR_COLORS hosts makedumpfile.conf.sample postfix rwtab.d system-release-cpe ........
7) Listar archivos en orden alfabético
Para enumerar un contenido del directorio especificado con detalles, como los permisos del archivo, el número de enlaces, el nombre del propietario y el propietario del grupo, el tamaño del archivo, la hora de la última modificación y el nombre del archivo / directorio, usaremos -l opción con ls mando.
$ ls -l /run
output total 24 -rw-r--r--. 1 root root 4 Aug 21 13:18 auditd.pid drwxr-xr-x. 2 root root 40 Aug 21 13:18 console -rw-r--r--. 1 root root 4 Aug 21 13:18 crond.pid ----------. 1 root root 0 Aug 21 13:18 cron.reboot drwxr-xr-x. 2 root root 60 Aug 21 13:18 dbus -rw-r--r--. 1 root root 4 Sep 8 12:41 dhclient-eth0.pid -rw-------. 1 root root 0 Aug 21 13:18 ebtables.lock drwxr-xr-x. 2 root root 40 Aug 21 13:18 faillock drwxr-x---. 2 root root 40 Aug 21 13:18 firewalld drwxr-xr-x. 4 root root 100 Aug 21 13:18 initramfs drwxr-xr-x. 4 root root 100 Sep 8 03:31 lock drwxr-xr-x. 3 root root 60 Aug 21 13:18 log ........
También puede utilizar dir comando para listar archivos, dir -S Ordenará por tamaño. Gracias por leer mi artículo y por favor deje sus comentarios.
Leer también: