Linux

Linux 以 ls 指令依據名稱、日期、大小排序檔案教學與範例

介紹如何在 Linux 系統下使用 ls 指令根據檔案的名稱、日期、大小資訊排序檔案。

按照檔案名稱排序

ls 的輸出預設會按照檔案名稱來排序:

# 按照檔案名稱排序(預設)
ls -l
total 1.5M
drwxr-xr-x  3 root root      4.0K  6月 11  2020 acpi
-rw-r--r--  1 root root      3.0K  6月 26  2019 adduser.conf
drwxr-xr-x  2 root root       20K  4月 30 06:30 alternatives
-rw-r--r--  1 root root       401  5月 30  2017 anacrontab
drwxr-xr-x  3 root root      4.0K  6月 11  2020 apache2
-rw-r--r--  1 root root       433 10月  2  2017 apg.conf
drwxr-xr-x  6 root root      4.0K  6月 11  2020 apm
drwxr-xr-x  3 root root      4.0K  6月 26  2019 apparmor
drwxr-xr-x  9 root root      4.0K  5月 28 06:02 apparmor.d

按照檔案大小排序

若要按照檔案的大小排序,可以加上 -S 參數:

# 按照檔案大小排序
ls -lS
total 1516
-rw-r--r--  1 root root      243873  5月 27 14:12 ld.so.cache
-rw-r--r--  1 root root       45582  5月 27 06:00 mailcap
-rw-r--r--  1 root root       31974  4月  9  2018 matplotlibrc
-rw-r--r--  1 root root       25341  8月 29  2018 brltty.conf
-rw-r--r--  1 root root       24301  7月 15  2016 mime.types
drwxr-xr-x  2 root root       20480  4月 30 06:30 alternatives
-rw-r--r--  1 root root       19183 12月 26  2016 services
-rw-r--r--  1 root root       14867 10月 13  2016 ltrace.conf
-rw-r--r--  1 root root       10550  1月 25  2018 login.defs

加上 -h 參數可以上檔案的大小資訊以更容易被閱讀的形式表示:

# 按照檔案大小排序(易讀格式)
ls -lSh
total 1.5M
-rw-r--r--  1 root root      239K  5月 27 14:12 ld.so.cache
-rw-r--r--  1 root root       45K  5月 27 06:00 mailcap
-rw-r--r--  1 root root       32K  4月  9  2018 matplotlibrc
-rw-r--r--  1 root root       25K  8月 29  2018 brltty.conf
-rw-r--r--  1 root root       24K  7月 15  2016 mime.types
drwxr-xr-x  2 root root       20K  4月 30 06:30 alternatives
-rw-r--r--  1 root root       19K 12月 26  2016 services
-rw-r--r--  1 root root       15K 10月 13  2016 ltrace.conf
-rw-r--r--  1 root root       11K  1月 25  2018 login.defs

按照檔案修改時間排序

若要按照檔案的改時間排序,可以加上 -t 參數:

# 按照檔案修改時間排序
ls -lt
total 1516
drwxr-xr-x  5 root lp          4096  5月 30 00:06 cups
drwxr-xr-x  4 root root        4096  5月 28 06:02 dhcp
drwxr-xr-x  9 root root        4096  5月 28 06:02 apparmor.d
drwxr-xr-x  2 root root        4096  5月 27 14:21 ssh
-rw-r--r--  1 root root        1082  5月 27 14:20 group
-rw-r--r--  1 root root        2851  5月 27 14:20 passwd
-rw-r-----  1 root shadow      1595  5月 27 14:20 shadow
-rw-r--r--  1 root root          97  5月 27 14:19 subgid
-rw-r--r--  1 root root          97  5月 27 14:19 subuid

反向排序

若要讓 ls 反向排序檔案,可以加上 -r 參數:

# 按照檔案名稱排序(反向排序)
ls -lr
total 1516
-rw-r--r--  1 root root         477  3月 16  2018 zsh_command_not_found
drwxr-xr-x  2 root root        4096  7月 10  2019 zsh
drwxr-xr-x  2 root root        4096  6月 11  2020 xml
drwxr-xr-x  5 root root        4096  6月 11  2020 xdg
drwxr-xr-x 11 root root        4096  6月 11  2020 X11
drwxr-xr-x  2 root root        4096  3月  5 06:49 wpa_supplicant
-rw-r--r--  1 root root        4942  4月  9  2019 wgetrc
lrwxrwxrwx  1 root root          23  6月 26  2019 vtrgb -> /etc/alternatives/vtrgb
drwxr-xr-x  4 root root        4096  4月 22  2020 vmware-tools

-r 參數可搭配 -S-t 參數一起使用:

# 按照檔案大小排序(反向排序)
ls -lSr
total 1516
-rw-r--r--  1 root root           0  6月 26  2019 popularity-contest.conf
-rw-r--r--  1 root root           0  6月 15  2020 odbcinst.ini
-rw-r--r--  1 root root           0  3月 26  2018 odbc.ini
-rw-r--r--  1 root root           3  4月 14  2020 papersize
-rw-r--r--  1 root root          11  6月 26  2017 debian_version
-rw-r--r--  1 root root          12  1月 30 06:41 timezone
lrwxrwxrwx  1 root root          19  6月 26  2019 mtab -> ../proc/self/mounts
-rw-r--r--  1 root root          19  8月  5  2020 issue.net
lrwxrwxrwx  1 root root          21  8月 14  2020 os-release -> ../usr/lib/os-release
# 按照檔案修改時間排序(反向排序)
ls -ltr
total 1516
-rw-r--r--  1 root root         624  8月  8  2007 mtools.conf
-rw-r--r--  1 root root         280  6月 20  2014 fuse.conf
-rw-r--r--  1 root root        3663  6月  9  2015 screenrc
-rw-r--r--  1 root root          34  1月 27  2016 ld.so.conf
-rw-r--r--  1 root root         367  1月 27  2016 bindresvport.blacklist
-rw-r--r--  1 root root          51  2月 19  2016 vdpau_wrapper.cfg
-rw-r--r--  1 root root       24301  7月 15  2016 mime.types
-rw-r--r--  1 root root         449  7月 15  2016 mailcap.order
-rw-r--r--  1 root root       14867 10月 13  2016 ltrace.conf

參考資料

Share
Published by
Office Guide

Recent Posts

Python 使用 PyAutoGUI 自動操作滑鼠與鍵盤

本篇介紹如何在 Python ...

9 個月 ago

Ubuntu Linux 以 WireGuard 架設 VPN 伺服器教學與範例

本篇介紹如何在 Ubuntu ...

9 個月 ago

Linux 網路設定 ip 指令用法教學與範例

本篇介紹如何在 Linux 系...

9 個月 ago

Windows 使用 TPM 虛擬智慧卡保護 SSH 金鑰教學與範例

本篇介紹如何在 Windows...

10 個月 ago

Linux 以 Shamir’s Secret Sharing 分割保存金鑰教學與範例

介紹如何在 Linux 中使用...

10 個月 ago

Linux 以 Cryptsetup、LUKS 加密 USB 隨身碟教學與範例

介紹如何在 Linux 系統中...

10 個月 ago