ls命令是列出目錄內容的最流行且非常有用的命令。 在本文中,我們將解釋如何使用ls sort選項按大小列出目錄內容。
1)按大小列出目錄中的文件(排序)
要列出具有大小排序的特定目錄的內容,我們將使用-lS選項和ls命令。 它將在頂部顯示最大的文件。
[linuxidc@localhost www.linuxidc.com]$ ls -lS /run
輸出:
總用量 60
-rw-rw-r--.? 1 root? ? ? ? ? utmp? ? ? ? ? 2304 11月? 7 09:38 utmp
drwxr-xr-x. 16 root? ? ? ? ? root? ? ? ? ? ? 400 11月? 7 08:04 systemd
drwxr-xr-x.? 6 root? ? ? ? ? root? ? ? ? ? ? 220 11月? 7 08:04 libvirt
drwxr-xr-x.? 7 root? ? ? ? ? root? ? ? ? ? ? 180 11月? 7 08:04 lock
drwxr-xr-x.? 7 root? ? ? ? ? root? ? ? ? ? ? 160 11月? 7 09:38 udev
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 140 11月? 7 08:04 proftpd
drwxr-xr-x.? 4 root? ? ? ? ? root? ? ? ? ? ? 120 11月? 7 08:04 initramfs
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 100 11月? 7 08:04 abrt
drwx--x--x.? 4 root? ? ? ? ? gdm? ? ? ? ? ? 100 11月? 7 08:04 gdm
drwxr-xr-x.? 3 root? ? ? ? ? root? ? ? ? ? ? 100 11月? 7 09:35 NetworkManager
drwxr-xr-x.? 2 avahi? ? ? ? ? avahi? ? ? ? ? ? 80 11月? 7 08:04 avahi-daemon
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 80 11月? 7 08:04 console
要列出文件大小但不排序,我們將使用-s選項和ls命令。
[linuxidc@localhost ~]$ ls -s
輸出:
總用量 4856
4808 core.11901? ? ? ? ? 4 linuxidc.txt? ? ? ? ? ? ? ? 0 公共? ? 0 文檔
4 linuxidc.c? ? ? ? ? 0 linuxidc-vagrant-project? ? 0 模板? ? 0 下載
36 linuxidc.com.mp4? ? 0 VirtualBox VMs? ? ? ? ? ? ? 0 視頻? ? 0 音樂
0 linuxidc_movies? ? ? 4 www.linuxidc.com.txt? ? ? ? 0 圖片? ? 0 桌面
2)列出文件大小(反向排序)
要列出具有反向排序大小的特定目錄的內容(即最小文件位于頂部),我們將使用-lSr選項和ls命令。
[linuxidc@localhost ~]$ ls -lSr /run
輸出:
總用量 60
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 xtables.lock
srw-rw-rw-.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 rpcbind.sock
-r--r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 rpcbind.lock
srwxr-xr-x.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 mcelog-client
srw-rw-rw-.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 gssproxy.sock
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 ebtables.lock
prw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 dmeventd-server
prw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 dmeventd-client
----------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 cron.reboot
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 3 11月? 7 08:04 mcelog.pid
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 syslogd.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 lvmetad.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 libvirtd.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 ksmtune.pid
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 gssproxy.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 chronyd.pid
3)以人類可讀格式對輸出和打印尺寸進行排序(例如,1K 48M 1G)
要以人類可讀的格式對輸出和打印大小進行排序,我們將使用-h選項和ls命令。
[linuxidc@localhost ~]$ ls -lSh
總用量 4.8M
-rw-------. 1 linuxidc linuxidc 5.0M 10月 17 18:44 core.11901
-rw-rw-r--. 1 linuxidc linuxidc? 33K 10月 21 19:24 linuxidc.com.mp4
-rw-------. 1 linuxidc linuxidc? 345 11月? 7 08:05 linuxidc.c
drwxr-xr-x. 6 linuxidc linuxidc? 130 11月? 6 14:32 桌面
drwx------. 3 linuxidc linuxidc? 66 11月? 6 15:09 VirtualBox VMs
-rw-------. 1 linuxidc linuxidc? 59 11月? 7 08:05 linuxidc.txt
drwxr-xr-x. 2 linuxidc linuxidc? 49 10月 25 16:01 圖片
drwxrwxr-x. 3 linuxidc linuxidc? 41 11月? 6 14:55 linuxidc-vagrant-project
lrwxrwxrwx. 1 linuxidc linuxidc? 26 11月? 3 16:57 linuxidc_movies -> /mnt/linuxidc_drive/movies
-rw-------. 1 linuxidc linuxidc? 19 11月? 7 08:05 www.linuxidc.com.txt
drwxr-xr-x. 2 linuxidc linuxidc? ? 6 10月 17 18:44 公共
drwxr-xr-x. 2 linuxidc linuxidc? ? 6 10月 17 18:44 模板
drwxr-xr-x. 2 linuxidc linuxidc? ? 6 10月 17 18:44 視頻
drwxr-xr-x. 2 linuxidc linuxidc? ? 6 10月 17 18:44 文檔
drwxr-xr-x. 2 linuxidc linuxidc? ? 6 10月 17 18:44 下載
drwxr-xr-x. 2 linuxidc linuxidc? ? 6 10月 17 18:44 音樂
此外,我們可以以人類可讀的格式打印尺寸以進行特定擴展。
[linuxidc@localhost ~]$ ls -l -S -h ~/*.mp4 | more
-rw-rw-r--. 1 linuxidc linuxidc 33K 10月 21 19:24 /home/linuxidc/linuxidc.com.mp4
[linuxidc@localhost ~]$ ls -l -S -h *.mp3
-rw-------. 1 linuxidc linuxidc 0 11月? 4 20:25 linuxidc.mp3
幾個基本提示
4)按字母順序排序列表
要使用字母排序列出特定目錄的內容,我們將僅使用ls命令而不使用選項,因為字母排序是默認排序。
[linuxidc@localhost ~]$ ls
abc.linuxidc.com? linuxidc.com.mp4? ? ? ? ? 公共? 文檔
b.linuxidc.com? ? linuxidc-vagrant-project? 模板? 下載
c.linuxidc.com? ? VirtualBox VMs? ? ? ? ? ? 視頻? 音樂
e.linuxidc.txt? ? www.linuxidc.com.txt? ? ? 圖片? 桌面
要列出包含詳細信息的特定目錄的內容,請添加目錄的路徑。
[linuxidc@localhost ~]$ ls -l /run
總用量 60
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 100 11月? 7 08:04 abrt
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? 11 11月? 7 08:04 alsactl.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 08:04 atd.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 auditd.pid
drwxr-xr-x.? 2 avahi? ? ? ? ? avahi? ? ? ? ? ? 80 11月? 7 08:04 avahi-daemon
drwxr-x---.? 2 chrony? ? ? ? chrony? ? ? ? ? 60 11月? 7 09:50 chrony
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 chronyd.pid
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 80 11月? 7 08:04 console
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 08:04 crond.pid
----------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 cron.reboot
drwxr-xr-x.? 3 root? ? ? ? ? lp? ? ? ? ? ? ? 80 11月? 7 08:04 cups
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 dbus
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 09:50 dhclient-ens33.pid
prw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 dmeventd-client
prw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 dmeventd-server
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 ebtables.lock
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 08:04 faillock
drwxr-x---.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 09:35 firewalld
drwx--x--x.? 4 root? ? ? ? ? gdm? ? ? ? ? ? 100 11月? 7 08:04 gdm
5)按字母順序反向排序列表
要按字母順序反向排序列出特定目錄的內容和詳細信息,我們將使用-lr選項和ls命令。
[linuxidc@localhost ~]$ ls -lr /run
總用量 60
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 xtables.lock
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 vmware
-rw-rw-r--.? 1 root? ? ? ? ? utmp? ? ? ? ? 2304 11月? 7 09:52 utmp
drwxr-xr-x.? 3 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 09:50 user
drwx------.? 2 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 udisks2
drwxr-xr-x.? 7 root? ? ? ? ? root? ? ? ? ? ? 160 11月? 7 09:50 udev
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 tuned
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 tmpfiles.d
drwxr-xr-x. 16 root? ? ? ? ? root? ? ? ? ? ? 400 11月? 7 08:04 systemd
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 syslogd.pid
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 sysconfig
drwx------.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 08:04 svnserve
drwx--x--x.? 3 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 sudo
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 08:04 sshd.pid
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 08:04 spice-vdagentd
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 08:04 sm-notify.pid
drwx--x--x.? 2 setroubleshoot setroubleshoot? 40 11月? 7 08:04 setroubleshoot
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 08:04 setrans
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 08:04 sepermit
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 08:04 samba
srw-rw-rw-.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 rpcbind.sock
6)按字母順序排序列出目錄的隱藏內容
要列出特定目錄的隱藏內容,我們將使用-a或--all選項和ls命令。
[linuxidc@localhost ~]$ ls -a /etc
.? ? ? ? ? ? ? ? ? ? ? ? ? idmapd.conf? ? ? ? ? ? ? proftpd.conf
..? ? ? ? ? ? ? ? ? ? ? ? ? init.d? ? ? ? ? ? ? ? ? ? protocols
abrt? ? ? ? ? ? ? ? ? ? ? ? inittab? ? ? ? ? ? ? ? ? pulse
adjtime? ? ? ? ? ? ? ? ? ? inputrc? ? ? ? ? ? ? ? ? purple
akonadi? ? ? ? ? ? ? ? ? ? iproute2? ? ? ? ? ? ? ? ? .pwd.lock
aliases? ? ? ? ? ? ? ? ? ? ipsec.conf? ? ? ? ? ? ? ? Python
aliases.db? ? ? ? ? ? ? ? ? ipsec.d? ? ? ? ? ? ? ? ? qemu-ga
alsa? ? ? ? ? ? ? ? ? ? ? ? ipsec.secrets? ? ? ? ? ? qemu-kvm
alternatives? ? ? ? ? ? ? ? iscsi? ? ? ? ? ? ? ? ? ? radvd.conf
anacrontab? ? ? ? ? ? ? ? ? issue? ? ? ? ? ? ? ? ? ? ras
asound.conf? ? ? ? ? ? ? ? issue.net? ? ? ? ? ? ? ? rc0.d
at.deny? ? ? ? ? ? ? ? ? ? Java? ? ? ? ? ? ? ? ? ? ? rc1.d
audisp? ? ? ? ? ? ? ? ? ? ? jvm? ? ? ? ? ? ? ? ? ? ? rc2.d
audit? ? ? ? ? ? ? ? ? ? ? jvm-commmon? ? ? ? ? ? ? rc3.d
avahi? ? ? ? ? ? ? ? ? ? ? kde? ? ? ? ? ? ? ? ? ? ? rc4.d
bash_completion.d? ? ? ? ? kde4rc? ? ? ? ? ? ? ? ? ? rc5.d
bashrc? ? ? ? ? ? ? ? ? ? ? kderc? ? ? ? ? ? ? ? ? ? rc6.d
binfmt.d? ? ? ? ? ? ? ? ? ? kdump.conf? ? ? ? ? ? ? ? rc.d
blacklist.dat? ? ? ? ? ? ? kernel? ? ? ? ? ? ? ? ? ? rc.local
bluetooth? ? ? ? ? ? ? ? ? krb5.conf? ? ? ? ? ? ? ? rdma
7)按字母順序排序列出文件
要列出指定目錄的內容以及詳細信息,例如文件權限,鏈接數,所有者的名稱和組所有者,文件大小,上次修改的時間以及文件/目錄名,我們將使用-l選項和ls 命令。
[linuxidc@localhost ~]$ ls -l /run
總用量 60
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 100 11月? 7 08:04 abrt
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? 11 11月? 7 08:04 alsactl.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 08:04 atd.pid
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 auditd.pid
drwxr-xr-x.? 2 avahi? ? ? ? ? avahi? ? ? ? ? ? 80 11月? 7 08:04 avahi-daemon
drwxr-x---.? 2 chrony? ? ? ? chrony? ? ? ? ? 60 11月? 7 09:50 chrony
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 4 11月? 7 08:04 chronyd.pid
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 80 11月? 7 08:04 console
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 08:04 crond.pid
----------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 cron.reboot
drwxr-xr-x.? 3 root? ? ? ? ? lp? ? ? ? ? ? ? 80 11月? 7 08:04 cups
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 60 11月? 7 08:04 dbus
-rw-r--r--.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 5 11月? 7 09:50 dhclient-ens33.pid
prw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 dmeventd-client
prw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 dmeventd-server
-rw-------.? 1 root? ? ? ? ? root? ? ? ? ? ? ? 0 11月? 7 08:04 ebtables.lock
drwxr-xr-x.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 08:04 faillock
drwxr-x---.? 2 root? ? ? ? ? root? ? ? ? ? ? 40 11月? 7 09:35 firewalld
您也可以使用dir命令列出文件,dir -S將按大小排序。 感謝您閱讀我的文章,請留下您的寶貴意見。