【Linux】命令 - ls

Posted by 西维蜀黍 on 2020-08-22, Last Modified on 2022-12-10

参数

  • -R:是否递归,即如果列出的目录中仍有子目录或者文件,将所有目录下面的文件都显示出来。
  • -l:显示文件的形态、权限、拥有者、文件大小、最后修改时间、文件的完整名称(文件名+扩展名)。显示格式是每一行显示一个文件或者目录。
  • -a: List all files, including hidden files
  • -r: list subdirectories recursively

Demo

ls -l - 列出文件的详细信息

The file mode printed under the -l option consists of the entry type, owner permissions, and group permissions. The entry type character describes the type of file, as follows:

       b     Block special file.
       c     Character special file.
       d     Directory.
       l     Symbolic link.
       s     Socket link.
       p     FIFO.
       -     Regular file.
$ ls -l
total 72160
drwxr-xr-x  9 ld-wei_shi ld-sgdev     4096 Dec 11 22:21 .
drwxr-xr-x  4 root       root         4096 Oct 22 01:16 ..
-rw-------  1 ld-wei_shi ld-sgdev    12062 Mar  7 23:01 .bash_history
-rw-r--r--  1 ld-wei_shi ld-sgdev      220 Oct 13 10:23 .bash_logout
-rw-r--r--  1 ld-wei_shi ld-sgdev     3771 Oct 13 10:23 .bashrc
drwx------  4 ld-wei_shi ld-sgdev     4096 Oct 28 22:26 .cache
drwx------  4 ld-wei_shi ld-sgdev     4096 Oct 28 22:27 .config
-rw-r--r--  1 ld-wei_shi ld-sgdev       77 Oct 28 22:27 .gitconfig
drwxr-xr-x 12 ld-wei_shi ld-sgdev     4096 Oct 15 14:17 .oh-my-zsh

ls -a - 显示隐藏文件

$ ls -a
.              .gitconfig            .zcompdump                                                  node_exporter-1.0.1.linux-amd64
..             .oh-my-zsh            

ls -lh - Display with Human Readable Units (KB, MB, GB)

ls -lh
total 71M
-rw-r--r-- 1 ld-wei_shi ld-sgdev   18 Dec 11 22:21 dump.rdb
-rw-r--r-- 1 ld-wei_shi ld-sgdev  12K Nov 15 17:37 g | tail -20q
drwxr-xr-x 3 ld-wei_shi ld-sgdev 4.0K Oct 28 22:26 go
drwxr-xr-x 2 ld-wei_shi ld-sgdev 4.0K Oct 13 23:13 node_exporter-1.0.1.linux-amd64
-rw-r--r-- 1 ld-wei_shi ld-sgdev 9.1M Oct 13 23:12 node_exporter-1.0.1.linux-amd64.tar.gz
drwxr-xr-x 5 ld-wei_shi ld-sgdev 4.0K Oct 15 15:23 prometheus-2.19.2.linux-amd64
-rw-r--r-- 1 ld-wei_shi ld-sgdev  62M Oct 14 12:03 prometheus-2.19.2.linux-amd64.tar.gz

排序显示

  • -S: sort by file size, largest first
  • --sort=WORD: sort by WORD instead of name: none (-U), size (-S), time (-t), version (-v), extension (-X)
# Long format list sorted by size (descending)
$ ls -lS

# Long format list of all files, sorted by modification date (oldest first):
$ ls -ltr

# Sort by time
# change the default of using modification times; access time (-u): atime, access, use; change time (-c): ctime, status; birth time: birth, creation;
$ ls --time=WORD

Misc

(不递归地)列出当前目录下所有子目录的名称

$ ls -F | grep /$

# e.g.
$ tree
.
├── s3_deploy.sh
└── static
    ├── 834468-20180406232634472-395289491-20190611155858275.png
    └── img
        ├── 404-bg.jpg
        └── 834468-20180406232634472-395289491-20190611155858275.png

2 directories, 4 files
$ ls -F | grep /$
static/

(不递归地)计算当前目录下的文件数

$ ls -l * |grep "^-"|wc -l

(不递归地)计算当前目录下的文件夹数

$ ls -l * |grep "^d"|wc -l

(不递归地)列出当前文件夹下文件的绝对路径

$ ls | sed "s:^:`pwd`/:"

递归列出当前目录下的所有文件(包括隐藏文件)的绝对路径

$ find $PWD

不递归列出当前目录下的所有文件(包括隐藏文件)的绝对路径

$ find $PWD -maxdepth 1

Reference