Linux ls 命令示例

在本文中,我们将学习如何在 Linux 中使用 ls 命令。
基本上,ls 命令是 Linux 中用于列出文件和目录的基本命令。
ls 命令带有许多参数和功能,例如我们可以按日期、大小对文件和目录进行排序,能够检查隐藏的文件和目录、权限、inode 信息等。

因此,让我们通过示例来看看有用且非常重要的 Linux ls 命令。

一些有用的 Linux ls 命令参数:

  • l – 以长列表格式列出文件和目录。
  • a – 检查隐藏的文件和目录。
  • i - 检查 inode 信息。
  • n – 列出文件和目录所属的所有者和组的 UID 和 GID 编号。
  • g – 列出它们所属的文件和目录组。
  • G – 列出用户而不是他们所属的文件和目录组。
  • h – 以人类可读格式列出文件和目录的大小。
更多: zhilu jiaocheng

带有示例的 Linux ls 命令

[root@localhost ~]# ls       ### List Files and Directories
anaconda-ks.cfg  Documents    install.log.syslog  Public
data             Downloads    Music               Templates
Desktop          install.log  Pictures            Videos

我们可以使用带参数 -l 的 ls 命令来获取更多与文件和目录相关的高级信息,例如权限、所有者、组、大小、创建和修改的月份、日期和时间、文件名...等...

[root@localhost ~]# ls -l   # Long Listing of Files and Directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

要列出特定文件的详细信息,请参阅以下命令。

[root@localhost ~]# ls -l file.txt   # List a Particular File
-rw-r--r--. 1 root root 0 Apr 26 10:52 file.txt

要列出特定目录的内容,请参阅以下命令。

[root@localhost ~]# ls -l /mydata/   # List a Particular Directory
total 0
-rw-r--r--. 1 root root 0 Apr 26 10:53 file1.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file2.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file3.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file4.txt
-rw-r--r--. 1 root root 0 Apr 26 10:53 file5.txt

我们可以使用带有参数 -a 的 Linux ls 命令来检查隐藏的文件和目录。

[root@localhost ~]# ls -a   # Checking hidden files and directories
.                .config    .gconf           .gvfs               Pictures
..               .cshrc     .gconfd          .ICEauthority       Public
anaconda-ks.cfg  data       .gnome2          install.log         .pulse
.bash_history    .dbus      .gnome2_private  install.log.syslog  .pulse-cookie
.bash_logout     Desktop    .gnote           .local              .ssh
.bash_profile    Documents  .gnupg           .mozilla            .tcshrc
.bashrc          Downloads  .gstreamer-0.10  Music               Templates
.cache           .esd_auth  .gtk-bookmarks   .nautilus           Videos

要检查文件和目录的 inode 数量,请使用 ls 命令和选项 -i 。

[root@localhost ~]# ls -i  # Checking Inode numbers of files and directories
401494 anaconda-ks.cfg  785089 Downloads           785094 Pictures
917077 data             392451 install.log         785091 Public
785088 Desktop          392452 install.log.syslog  785090 Templates
785092 Documents        785093 Music               785095 Videos

或者我们可以将 -i 参数与 -l 一起使用,如下面的命令所示。

[root@localhost ~]# ls -li
total 100
401494 -rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
917077 drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
785088 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
785092 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
785089 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
392451 -rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
392452 -rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
785093 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
785094 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
785091 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
785090 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
785095 drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos

带有参数 -o 的 ls 命令将显示文件和目录的所有者,而不会显示组。

[root@localhost ~]# ls -o   # Listing only owner of files and directories
total 100
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

带有选项 -m 的 Linux ls 命令将列出以逗号分隔的文件和目录。

[root@localhost /]# ls -m   # List seperated by comma
bin, boot, dev, etc, home, lib, lib64, lost+found, media, mnt, mydata, opt,
proc, root, sbin, selinux, srv, sys, tmp, users, usr, var

带有选项 -Q 的 ls 命令将列出文件和目录,其中文件和目录的名称用引号括起来。
请参阅下面的示例输出。

[root@localhost /]# ls -lQ   # Labels of Files and directories enclosed by Quotes
total 106
dr-xr-xr-x.   2 root root  4096 Apr 16 09:59 "bin"
dr-xr-xr-x.   5 root root  1024 Apr  9 02:45 "boot"
drwxr-xr-x.  19 root root  4120 Apr 26 08:51 "dev"
drwxr-xr-x. 102 root root 12288 Apr 26 10:34 "etc"
drwxr-xr-x.   9 root root  4096 Apr 20 09:39 "home"
dr-xr-xr-x.  10 root root  4096 Apr  9 02:39 "lib"
dr-xr-xr-x.   9 root root 12288 Apr 16 09:59 "lib64"
drwx------.   2 root root 16384 Apr  9 02:35 "lost+found"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "media"
drwxr-xr-x.   3 root root  4096 Apr  9 02:46 "mnt"
drwxr-xr-x.   2 root root  4096 Apr 26 10:53 "mydata"
drwxr-xr-x.   3 root root  4096 Apr  8 21:17 "opt"
dr-xr-xr-x. 162 root root     0 Apr 26 08:49 "proc"
dr-xr-x---.  27 root root  4096 Apr 26 10:52 "root"
dr-xr-xr-x.   2 root root 12288 Apr 16 09:59 "sbin"
drwxr-xr-x.   7 root root     0 Apr 26 08:49 "selinux"
drwxr-xr-x.   2 root root  4096 Sep 23  2011 "srv"
drwxr-xr-x.  13 root root     0 Apr 26 08:49 "sys"
drwxrwxrwt.  15 root root  4096 Apr 26 10:56 "tmp"
drwxrwxrwx.   3 root root  4096 Apr 20 09:22 "users"
drwxr-xr-x.  13 root root  4096 Apr  9 02:35 "usr"
drwxr-xr-x.  22 root root  4096 Apr 16 09:24 "var"

我们可以使用带有参数 -R 的 Linux ls 命令递归列出目录。
递归意味着它将以树格式列出所有子目录的所有目录。

[root@localhost ~]# ls -R   # Listing Directories Recursively
.:
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates
./data:
a.txt  b.txt  c.txt
./Desktop:
data1  data2  data3
./Desktop/data1:
./Desktop/data2:
./Desktop/data3:
./Documents:
./Downloads:
./Music:
./Pictures:
./Public:
./Templates:
./Videos:

带有 ~ 的 ls 命令会将我们切换到主目录。

[root@localhost Downloads]# pwd 
/root/Downloads
[root@localhost Downloads]# ls ~   # Switch to Home Directory
anaconda-ks.cfg  Documents  install.log         Pictures   Videos
data             Downloads  install.log.syslog  Public
Desktop          file.txt   Music               Templates

带有 -t 的 Linux ls 命令将按照文件和目录的修改日期升序列出文件和目录,意味着较高的先到。

[root@localhost ~]# ls -lt   # Sort by Modified by date
total 100
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates

我们可以使用带参数 -S 的 ls 命令按大小对文件和目录进行排序。

[root@localhost ~]# ls -lS   # Sort by Size in ascending order
total 100
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt

带有选项 -h 的 ls 命令将以人类可读格式列出文件和目录的大小,例如 20K (20 KB)、15M (15 MB)。

[root@localhost ~]# ls -lh   # List the size in Human Readable format
total 100K
-rw-------. 1 root root 2.7K Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root 4.0K Apr 24 09:10 data
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Desktop
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Documents
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Downloads
-rw-r--r--. 1 root root    0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root  41K Apr  9 02:44 install.log
-rw-r--r--. 1 root root 9.0K Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Music
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Pictures
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Public
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Templates
drwxr-xr-x. 2 root root 4.0K Apr  8 21:18 Videos

带有选项 -n 的 ls 命令将显示文件和目录的 UID(用户 ID)和 GID(组 ID)编号。

[root@localhost ~]# ls -ln   # List the UID and GID Numbers
total 108
-rw-------. 1 0   0  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 0   0  4096 Apr 24 09:10 data
drwxr-xr-x. 5 0   0  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 0   0     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 0   0 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 0   0  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 0 501  4096 Apr 28 09:48 test
drwxr-xr-x. 2 0 506  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 0   0  4096 Apr  8 21:18 Videos

如果我们只想列出它们所属的文件和目录的所有者而不是组,请使用带有参数 -G 的 Linux ls 命令。

[root@localhost ~]# ls -lG   # Disable the Group Listing and only lists owners
total 108
-rw-------. 1 root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root  4096 Apr 24 09:10 data
drwxr-xr-x. 5 root  4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 root  4096 Apr 28 09:48 test
drwxr-xr-x. 2 root  4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root  4096 Apr  8 21:18 Videos

带有 -g 的 ls 命令将列出它们所属的文件和目录的组。

[root@localhost ~]# ls -g   # Lists groups to which they belongs
total 108
-rw-------. 1 root        2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root        4096 Apr 24 09:10 data
drwxr-xr-x. 5 root        4096 Apr 28 09:44 Desktop
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Documents
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Downloads
-rw-r--r--. 1 root           0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root       41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root        9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Music
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Pictures
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Public
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Templates
drwxr-xr-x. 2 sales       4096 Apr 28 09:49 test2
drwxr-xr-x. 2 root        4096 Apr  8 21:18 Videos

带有 -F 的 ls 命令将列出带有 /at 结尾的目录。
请参阅下面的示例输出。

[root@localhost ~]# ls -lF   # Add's / at the end of the directories
total 100
-rw-------. 1 root root  2689 Apr  9 02:44 anaconda-ks.cfg
drwxr-xr-x. 2 root root  4096 Apr 24 09:10 data/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Desktop/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Documents/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Downloads/
-rw-r--r--. 1 root root     0 Apr 26 10:52 file.txt
-rw-r--r--. 1 root root 41364 Apr  9 02:44 install.log
-rw-r--r--. 1 root root  9154 Apr  9 02:41 install.log.syslog
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Music/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Pictures/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Public/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Templates/
drwxr-xr-x. 2 root root  4096 Apr  8 21:18 Videos/

有关 Linux ls 相关命令及其参数的更多参考,我们可以在 linux 系统中运行以下命令。

[root@localhost ~]# man help
[root@localhost ~]# ls --help
日期:2020-09-17 00:14:57 来源:oir作者:oir