如何在Linux中使用ls命令按大小对所有文件排序
时间:2020-03-05 15:28:52 来源:igfitidea点击:
“ ls”命令是用于列出目录内容的最流行且非常有用的命令。
在本文中,我们将说明如何使用“ ls”排序选项按大小列出目录内容。
1)按大小列出目录中的文件(排序)
要通过大小排序列出特定目录的内容,我们将在“ ls”命令中使用“ -lS”选项。
它将在顶部显示最大的文件。
$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 -s option with ls command.
$ls -s
2)列出文件的大小(反向排序)
要使用大小反向排序列出特定目录的内容(即,大小最小的文件位于顶部),我们将在“ ls”命令中使用“ -lSr”选项。
$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)以人类可读的格式(例如1K 48M 1G)对输出和打印尺寸进行排序
为了以人类可读的格式对输出和打印尺寸进行排序,我们将在“ ls”命令中使用“ -h”选项。
$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 .......
同样,我们可以按人类可读的格式打印尺寸以进行特定的扩展。
ls -l -S -h *.mp3
ls -l -S -h ~/Downloads/*.mp4 | more
一些基本技巧
4)按字母顺序排序
要通过字母排序列出特定目录的内容,我们将仅使用“ ls”命令,不带选项,因为字母排序是默认设置。
$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
要列出包含详细信息的特定目录的内容,请添加目录的路径。
$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)按字母反向排序列表
要列出特定目录的内容并提供按字母反向排序的详细信息,我们将在“ ls”命令中使用“ -lr”选项。
$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)以字母顺序列出目录的隐藏内容
要列出特定目录的隐藏内容,我们将在“ ls”命令中使用“ -a”或者“ --all”选项。
$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)按字母顺序列出文件
要列出指定目录的内容并提供详细信息,例如文件许可权,链接数,所有者名称和组所有者,文件大小,上次修改时间以及文件/目录名称,我们将使用“ -l”选项使用“ ls”命令。
$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 ........
我们也可以使用“ dir”命令列出文件,“ dir -S”将按大小排序。