ls命令常见用法

485 阅读2分钟

1. 查看最近编辑的文件

ls -t | head -1

ls -t按修改时间排序,先显示最近编辑的文件。head -1取第一行。同理,可以按最近修改时间排序,显示目录下的文件。

$ ls -lt
total 91676
-rwxr-xr-x 1 song song 93138000 Feb 11 15:51 VSCodeUserSetup-x64-1.75.0.exe
-rw-r--r-- 1 song song   718288 Feb 11 15:51 b
-rw-r--r-- 1 song song      489 Feb 11 15:50 a
-rw-r--r-- 1 song song       10 Feb 11 15:46 e
-rw-r--r-- 1 song song        8 Feb 11 15:43 d
-rw-r--r-- 1 song song        6 Feb 11 15:42 c

也可以逆序显示,最近修改的文件最后展示。

$ ls -ltr
total 91676
-rw-r--r-- 1 song song        6 Feb 11 15:42 c
-rw-r--r-- 1 song song        8 Feb 11 15:43 d
-rw-r--r-- 1 song song       10 Feb 11 15:46 e
-rw-r--r-- 1 song song      489 Feb 11 15:50 a
-rw-r--r-- 1 song song   718288 Feb 11 15:51 b
-rwxr-xr-x 1 song song 93138000 Feb 11 15:51 VSCodeUserSetup-x64-1.75.0.exe

2. 每行显示一个文件

$ ls
a  b  c  d  e
$ ls -1
a
b
c
d
e

ls -1每行显示一个文件,注意是数字1。

3. 更易读的文件大小

(base) song@songxxpc:/tmp/test$ ls -hl
total 90M
-rwxr-xr-x 1 song song  89M Feb 11 15:51 VSCodeUserSetup-x64-1.75.0.exe
-rw-r--r-- 1 song song  489 Feb 11 15:50 a
-rw-r--r-- 1 song song 702K Feb 11 15:51 b
-rw-r--r-- 1 song song    6 Feb 11 15:42 c
-rw-r--r-- 1 song song    8 Feb 11 15:43 d
-rw-r--r-- 1 song song   10 Feb 11 15:46 e

选项-h使用更易读的方式显示文件大小,M代表MB,K代表KB,G代表GB。

4. 显示目录本身信息

$ ls -ld /etc
drwxr-xr-x 104 root root 4096 Feb 11 15:15 /etc

选项-d表示显示目录本身信息,而不是目录下的文件。

5. 递归显示子目录

$ ls -R /etc/apt
/etc/apt:
apt.conf.d  auth.conf.d  preferences.d  sources.list  sources.list.bak  sources.list.d  trusted.gpg.d

/etc/apt/apt.conf.d:
01-vendor-ubuntu  15update-stamp       20auto-upgrades  50command-not-found    99update-notifier
01autoremove      20apt-esm-hook.conf  20packagekit     50unattended-upgrades
10periodic        20archive            20snapd.conf     70debconf

/etc/apt/auth.conf.d:

/etc/apt/preferences.d:

/etc/apt/sources.list.d:

/etc/apt/trusted.gpg.d:
ubuntu-keyring-2012-archive.gpg  ubuntu-keyring-2012-cdimage.gpg  ubuntu-keyring-2018-archive.gpg

选项-R表示递归显示子目录。

6. 显示uid和gid

(base) song@songxxpc:/tmp/test$ ls -n
total 91676
-rwxr-xr-x 1 1000 1000 93138000 Feb 11 15:51 VSCodeUserSetup-x64-1.75.0.exe
-rw-r--r-- 1 1000 1000      489 Feb 11 15:50 a
-rw-r--r-- 1 1000 1000   718288 Feb 11 15:51 b
-rw-r--r-- 1 1000 1000        6 Feb 11 15:42 c
-rw-r--r-- 1 1000 1000        8 Feb 11 15:43 d
-rw-r--r-- 1 1000 1000       71 Feb 11 16:27 e

默认显示的是所属用户和所属组的名称,选项-n表示显示uid和gid。

7. 按文件大小排序

选项-S表示从大到小排序输出。

$ ls -lS
total 91676
-rwxr-xr-x 1 song song 93138000 Feb 11 15:51 VSCodeUserSetup-x64-1.75.0.exe
-rw-r--r-- 1 song song   718288 Feb 11 15:51 b
-rw-r--r-- 1 song song      489 Feb 11 15:50 a
-rw-r--r-- 1 song song       71 Feb 11 16:27 e
-rw-r--r-- 1 song song        8 Feb 11 15:43 d
-rw-r--r-- 1 song song        6 Feb 11 15:42 c

8. 显示隐藏的文件和目录

选项-a显示隐藏的文件和目录。

$ ls
anaconda3  backup  clion  pycharm  shellscript
$ ls -a
.              .bashrc  .dotnet   .landscape   .parallel        .slocdata                  .wget-hsts  pycharm
..             .cache   .gnupg    .lesshst     .profile         .sudo_as_admin_successful  anaconda3   shellscript
.bash_history  .conda   .ipython  .local       .pyenv           .viminfo                   backup
.bash_logout   .config  .jupyter  .motd_shown  .python_history  .vscode-server             clion

9. 逗号分隔文件和目录

$ ls -m
VSCodeUserSetup-x64-1.75.0.exe, a, b, c, d, e

10. 只显示子目录

$ ls -d */
anaconda3/  backup/  clion/  pycharm/  shellscript/

参考

Practical applications of 'ls' command in Linux - GeeksforGeeks