ls

最後更新: 2023-04-12

目錄

 


Extended security information

 

# ls 時會見到 "+", 代表它有 Extended security information

frwxrwxrwx+

# 可以用 getfacl 獲得詳細資料

getfacl <file>

詳見: fs_acl

 


神奇的 Options (-s)

 

ls 的 --size Opts

-s, --size                       # print the allocated size of each file in blocks

i.e

dd if=/dev/zero of=iscsi.img bs=1M count=100      # 100 MB

ls -lh iscsi.img

-rw-r--r-- 1 root root 100M May 16 16:16 iscsi.img

# show in blocks

ls -sl iscsi.img

102400 -rw-r--r-- 1 root root 104857600 Sep 20 14:55 iscsi.img

ls -slh iscsi.img

101M -rw-r--r-- 1 root root 100M May 16 16:16 iscsi.img

 

"ls -l" vs "ls -s"

ls -l

Gives the size of the file (= the amount of data it contains)

ls -s

Tells you the allocated size of the file on file system

Usually a bit more than the file's size, but it can be less if the file has holes.

Sparse File

A "sparse file" contains "holes"

cp centos.img centos.img_cp

dd if=centos.img of=centos.img_dd

ls -ls centos.img*

 

ls 的 --block-size=SIZE Opts

--block-size=SIZE            scale sizes by SIZE before printing them

Units are K,M,G,T,P,E,Z,Y (powers of 1024) or KB,MB,... (powers of 1000)

e.g.

--block-size=M      # 1,04,576 bytes

 

 

 


Listing only directories

 

方法1

# "*/" is a pattern that matches all of the subdirectories in the current directory

# ( "*" would match all files and subdirectories; the "/" restricts it to directories)

ls -d */

方法2

ls -l | grep '^d'

 


UID & GID

 

-n, --numeric-uid-gid              # like -l, but list numeric user and group IDs

ls -n /dev | grep 65534

crw-rw-rw- 1 65534 65534   1, 7 Aug 25 19:11 full
drwxrwxrwt 2 65534 65534     40 Dec 18 17:59 mqueue
crw-rw-rw- 1 65534 65534   1, 3 Aug 25 19:11 null
crw-rw-rw- 1 65534 65534   1, 8 Aug 25 19:11 random
crw-rw-rw- 1 65534 65534   5, 0 Dec 18 16:44 tty
crw-rw-rw- 1 65534 65534   1, 9 Aug 25 19:11 urandom
crw-rw-rw- 1 65534 65534   1, 5 Aug 25 19:11 zero

 


Bash scripting: test for empty directory

 

# ls -A             do not list implied . and ..

if [ -z "$(ls -A /path/to/dir)" ]; then
   echo "Empty"
else
   echo "Not Empty"
fi

 


Show more info.

 

ls -l /etc/systemd/system/getty.target.wants

total 0
lrwxrwxrwx. 1 root root 38 Apr  6 17:37 [email protected] -> /usr/lib/systemd/system/[email protected]

# -o     like -l, but do not list group information

# -g     like -l, but do not list owner

ls -go /etc/systemd/system/getty.target.wants

total 0
lrwxrwxrwx. 1 38 Apr  6 17:37 [email protected] -> /usr/lib/systemd/system/[email protected]

-F, --classify

# append indicator (one of */=>@|) to entries

ls -F /etc/systemd/system/getty.target.wants

[email protected]@

  • * for executables
  • / for directories
  • @ for symbolic links
  • | for FIFOs
  • = for sockets
  • > for doors
  • (nothing) for regular files

Remark: Doors

Doors is an inter-process communication facility for Unix computer systems.

Doors are created by server processes (which must use threads) and called by client processes.
(It is possible for one process to both create and call a door.)

Unlike most remote procedure call systems, each door has only one server procedure.

A server can "attach" a door to a file, enabling clients to connect to that door simply by opening that file.

Clients use door_call() to invoke the door's server procedure,
passing a contiguous region of memory and a list of file descriptors as arguments,
and getting back another contiguous region and list of file descriptors.

 


ls -1 > list.txt

 

ls -1

1.txt
2.txt
3.txt

ls -1 > list.txt

cat list.txt

1.txt
2.txt
3.txt
list.txt

 


Follow symbolic links(-H)

 

Opt:

-H, --dereference-command-line

Usage

ls -l /lxc

lrwxrwxrwx 1 root root 12 Jul  7  2012 /lxc -> /var/lib/lxc

ls -lH /lxc

total 16
drwxr-xr-x 3 root   root   4096 Apr 27 16:52 lamp
drwxrwx--- 3 100000 100000 4096 Oct 26  2020 proxy
drwxrwx--- 3 100000 100000 4096 Jun 15 15:52 sshgw
drwxrwx--- 3 100000 100000 4096 Oct 12  2020 vnc

 


dircolors

 

dircolors = color setup for ls

Help

man dir_colors

A list of codes and colors for lots of filetypes

# -p, --print-database

dircolors -p > ~/.dircolors     # echo $LS_COLORS

.bashrc

# always(default if omitted), auto, never

alias ls='ls --color'

alias ls='ls --color=auto'

Color

  • Green: 可執行 file
  • Sky Blue: Symbolic link file
  • Yellowwith black background: Device
  • Pink: Graphic image file (jpg, png ...)
  • Red: Archive file (gz, zip ...)
  • Uncolored with red background: set-user-ID file
  • Black with yellow background: set-group-ID file
  • Red with black background: Broken link

目錄

Blue: 目錄

Blue with green background: other-writable directory

1) 00=none, 01=bold, 04=underscore, 05=blink, 07=reverse, 08=concealed.

2) colour

3) background color

 

 


Sorting

 

Opts

  • none (-U)                # do not sort; list entries in directory order
  • size (-S)                 # sort by file size
  • time (-t)                  # sort by modification time, newest first
  • version (-v)             # natural sort of numbers within text
  • extension (-X)          # sort alphabetically by entry extension
  • -r, --reverse             # reverse order while sorting
  • --group-directories-first      # can be augmented with a --sort option

Default Sorting

touch {a,z,A,Z,1,9}.txt; ls -1

1.txt
9.txt
a.txt
A.txt
z.txt
Z.txt

Notes

與 ASCII 排序無關

  • 48  0
  • 65  A
  • 95  _
  • 97  a

 

 


Sort "_"

 

touch a _b c 1 _2 3 A _B C

# _X 不是一組地排列

ls -1

1
_2
3
a
A
_b
_B
c
C

ls -1 | sort           # 也無效

原因: In the en_US.UTF-8 locale, underscores (_) sort after letters and numbers

Sort underscore characters in group

[方案1]

LC_COLLATE=C ls -1

1
3
A
C
_2
_B
_b
a
c

[方案2]

# -v, --version-sort         natural sort of (version) numbers within text

ls -v -1

1
3
A
C
a
c
_2
_B
_b

 


Tips

 

~/.bashrc

alias ls='ls --group-directories-first --color'

 

 

Creative Commons license icon Creative Commons license icon