【Linux常用命令】 ls

ls == list,根据不同的选项,列举指定目录或文件的相关信息,是Unix/Linux下最常用的命令之一,cd到某一目录下后执行的第一个命令。

ls命令格式:ls [OPTION]... [FILE]...

列举文件信息(默认为当前目录)。

ls

无参数:显示当前目录下的文件和目录(隐藏文件除外)。
Folder: 显示Folder目录中的文件和子目录。
File:显示File文件信息(默认为文件名)。
1 [loong@localhost ~]$ ls
2 Desktop regex src.tar time_test.c vimcdoc-1.7.0 VMwareTools
3 [loong@localhost ~]$ ls /usr
4 bin games kerberos libexec sbin src X11R6
5 etc include lib local share tmp
6 [loong@localhost ~]$ ls time_test.c
7 time_test.c

 ls -a

-a选项同ls,但会列举当前目录或者指定目录下的所有文件,包括dot文件(.开头的文件)和.目录和..目录。

1 [loong@localhost ~]$ ls -a /usr
2 . bin games kerberos libexec sbin src X11R6
3 .. etc include lib local share tmp

ls -A

-A选项同ls,但会列举当前目录或者指定目录下的所有文件,包括dot文件(.开头的文件),但不包括.目录和..目录。

1 [loong@localhost ~]$ ls -A /usr
2 bin games kerberos libexec sbin src X11R6
3 etc include lib local share tmp

ls -l

-l选项的ls命令将列举当前目录或指定目录中文件或者子目录的详析信息。

1 [loong@localhost ~]$ ls -l
2 total 48
3 drwxr-xr-x 3 loong loong 4096 Mar 27 21:12 Desktop
4 drwxrwxr-x 2 loong loong 4096 Jan 13 16:01 regex
5 drwxrwxr-x 6 loong loong 4096 Feb 15 10:57 src.tar
6 -rw-rw-r-- 1 loong loong 153 Mar 27 19:47 time_test.c
7 drwxrwxrwx 3 loong loong 4096 Mar 27 19:29 vimcdoc-1.7.0
8 drwxrwxr-x 3 loong loong 4096 Jan 10 00:18 VMwareTools

 

ls -l列举的信息包含7个以Tab的域:

  • 第一个域:第一个字符指明了文件类型
    • -: 普通文件;
    • d: 目录文件;
    • l: 符号链接;
    • s: socket文件;
    • b: 块设备;
    • c: 字符设备;
    • p: 管道文件。

    后面的9个字符指明了文件的访问权限:每三位指明一类用户的权限,分别是文件属主、同组用户、其它用户,权限分为读(r)、写(w)、执行(x)。

  • 第二个域:链接数。普通文件至少为1,目录至少为2(.和..);
  • 第三域:文件属主;
  • 第四域:用户组;
  • 第五域:文件大小,其中目录大小通常为块大小的整数倍;
  • 第六域:文件的最近修改日期和时间,修改文件意味着对其内文件或子目录的增添和修改;
  • 第七域:文件名

ls -t

按文件的修改时间列举文件,最近修改的在前。

1 [loong@localhost ~]$ ls
2 Desktop regex src.tar time_test.c vimcdoc-1.7.0 VMwareTools
3 [loong@localhost ~]$ ls -t
4 Desktop time_test.c vimcdoc-1.7.0 src.tar regex VMwareTools

ls -r

以相反顺序列举文件。

1 [loong@localhost ~]$ ls
2 Desktop regex src.tar time_test.c vimcdoc-1.7.0 VMwareTools
3 [loong@localhost ~]$ ls -r
4 VMwareTools vimcdoc-1.7.0 time_test.c src.tar regex Desktop

ls -1

单独行列举文件。

1 [loong@localhost ~]$ ls
2 Desktop regex src.tar time_test.c vimcdoc-1.7.0 VMwareTools
3 [loong@localhost ~]$ ls -1
4 Desktop
5 regex
6 src.tar
7 time_test.c
8 vimcdoc-1.7.0
9 VMwareTools

ls -F

可视化显示文件类型。

1 [loong@localhost ~]$ ls
2 Desktop regex src.tar time_test.c vimcdoc-1.7.0 VMwareTools
3 [loong@localhost ~]$ ls -F
4 Desktop/ regex/ src.tar/ time_test.c vimcdoc-1.7.0/ VMwareTools/
  • / : 目录
  • nothing : 普通文件.
  • @ :符号链接文件
  • * :可执行文件

ls -latr

组合选项,即是说,各个选项可以结合使用,除了互相排斥的选项,例如-a和-A。
1 [loong@localhost Desktop]$ ls
2 VMwareTools-8.1.4-227600.tar.gz vmware-tools-distrib
3 [loong@localhost Desktop]$ ls -latr
4 total 94984
5 drwxr-xr-x 7 loong loong 4096 Jan 23 2010 vmware-tools-distrib
6 -r--r--r-- 1 loong loong 97129687 Jan 23 2010 VMwareTools-8.1.4-227600.tar.gz
7 drwxr-xr-x 3 loong loong 4096 Mar 27 21:12 .
8 drwx------ 28 loong loong 4096 Mar 27 21:34 ..

使用alias

使用alias会更加方便,我系统当前帐号中定义的alias有:

 1 [loong@localhost Desktop]$ alias
2 alias l.='ls -d .* --color=tty'
3 alias ll='ls -l --color=tty'
4 alias ls='ls --color=tty'
5 alias vi='vim'
6 alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
7 [loong@localhost Desktop]$ l.
8 . ..
9 [loong@localhost Desktop]$ ll
10 total 94968
11 -r--r--r-- 1 loong loong 97129687 Jan 23 2010 VMwareTools-8.1.4-227600.tar.gz
12 drwxr-xr-x 7 loong loong 4096 Jan 23 2010 vmware-tools-distrib

ps:前3个alias定义在/etc/profile.d/目录下的colorls.csh和colorls.sh中,具体是哪个就不清楚了。

系统信息:

1 [loong@localhost /]$ cat /etc/redhat-release 
2 CentOS release 5.7 (Final)
3 [loong@localhost /]$ uname -a
4 Linux localhost.localdomain 2.6.18-274.18.1.el5 #1 SMP Thu Feb 9 12:45:52 EST 2012 i686 i686 i386 GNU/Linux

以上参考:http://www.dutor.net/index.php/2010/06/cmd-ls/

 

模糊列举: 其中,*代表任意个字符(包括0个),?代表一个字符。

ls /etc/a*e

列出/etc/目录下以字母a开头且以字母e结尾的目录和文件。

1 [loong@localhost /]$ ls /etc/a*e
2 /etc/adjtime /etc/asound.state

ls /bin/l?

列出/bin/目录下以字母b开头的目录和文件,且目录名或文件名的长度为2.

1 [loong@localhost /]$ ls /bin/l?
2 /bin/ln /bin/ls


ls |more

当要显示的文件数太多(如/usr/bin/下的文件),这是一页屏不能显示,如果直接运行“ls /usr/bin”,则不能看见最前面的文件。这时用到通道“|more”,来显示多页屏输出(按空格显示下一页,回车显示下一行)。

 1 [loong@localhost /]$ ls /usr/bin |more
2 [
3 411toppm
4 a2p
5 a2ps
6 ab
7 ac
8 aconnect
9 acpi_listen
10 activation-client
11 addftinfo
12 addr2line
13 addresses
14 afs5log
15 alacarte
16 alsamixer
17 amidi
18 amixer
19 amtu
20 amuFormat.sh
21 animate
22 anytopnm
23 aplay
24 aplaymidi
25 --More--

【注】此用法类似与windows下的cmd命令“dir /p”。


最后,附上ls的英文manual(Linux的man命令)。

man ls
  1 LS(1)                            User Commands                           LS(1)
2
3 NAME
4 ls - list directory contents
5
6 SYNOPSIS
7 ls [OPTION]... [FILE]...
8
9 DESCRIPTION
10 List information about the FILEs (the current directory by default). Sort entries alphabetically if
11 none of -cftuvSUX nor --sort.
12
13 Mandatory arguments to long options are mandatory for short options too.
14
15 -a, --all
16 do not ignore entries starting with .
17
18 -A, --almost-all
19 do not list implied . and ..
20
21 --author
22 with -l, print the author of each file
23
24 -b, --escape
25 print octal escapes for nongraphic characters
26
27 --block-size=SIZE
28 use SIZE-byte blocks
29
30 -B, --ignore-backups
31 do not list implied entries ending with ~
32
33 -c with -lt: sort by, and show, ctime (time of last modification of file status information) with
34 -l: show ctime and sort by name otherwise: sort by ctime
35
36 -C list entries by columns
37
38 --color[=WHEN]
39 control whether color is used to distinguish file types. WHEN may be ¡®never¡¯, ¡®always¡¯, or
40 ¡®auto¡¯
41
42 -d, --directory
43 list directory entries instead of contents, and do not dereference symbolic links
44
45 -D, --dired
46 generate output designed for Emacs¡¯ dired mode
47
48 -f do not sort, enable -aU, disable -lst
49
50 -F, --classify
51 append indicator (one of */=>@|) to entries
52
53 --file-type
54 likewise, except do not append ¡®*¡¯
55
56 --format=WORD
57 across -x, commas -m, horizontal -x, long -l, single-column -1, verbose -l, vertical -C
58
59 --full-time
60 like -l --time-style=full-iso
61
62 -g like -l, but do not list owner
63
64 -G, --no-group
65 like -l, but do not list group
66
67 -h, --human-readable
68 with -l, print sizes in human readable format (e.g., 1K 234M 2G)
69
70 --si likewise, but use powers
71
72 -H, --dereference-command-line
73 follow symbolic links listed on the command line
74
75 --dereference-command-line-symlink-to-dir
76 follow each command line symbolic link that points to a directory
77
78 --hide=PATTERN
79 do not list implied entries matching shell PATTERN (overridden by -a or -A)
80
81 --indicator-style=WORD append indicator with style WORD to entry names:
82 none (default), slash (-p), file-type (--file-type), classify (-F)
83
84 -i, --inode
85 with -l, print the index number of each file
86
87 -I, --ignore=PATTERN
88 do not list implied entries matching shell PATTERN
89
90 -k like --block-size=1K
91
92 -l use a long listing format
93
94 -L, --dereference
95 when showing file information for a symbolic link, show information for the file the link refer-
96 ences rather than for the link itself
97
98 -m fill width with a comma separated list of entries
99
100 -n, --numeric-uid-gid
101 like -l, but list numeric user and group IDs
102
103 -N, --literal
104 print raw entry names (don¡¯t treat e.g. control characters specially)
105
106 -o like -l, but do not list group information
107
108 -p, --indicator-style=slash
109 append / indicator to directories
110
111 -q, --hide-control-chars
112 print ? instead of non graphic characters
113
114 --show-control-chars
115 show non graphic characters as-is (default unless program is ¡®ls¡¯ and output is a terminal)
116
117 -Q, --quote-name
118 enclose entry names in double quotes
119
120 --quoting-style=WORD
121 use quoting style WORD for entry names: literal, locale, shell, shell-always, c, escape
122
123 -r, --reverse
124 reverse order while sorting
125
126 -R, --recursive
127 list subdirectories recursively
128
129 -s, --size
130 with -l, print size of each file, in blocks
131
132 -S sort by file size
133
134 --sort=WORD
135 extension -X, none -U, size -S, time -t, version -v, status -c, time -t, atime -u, access -u, use
136 -u
137
138 --time=WORD
139 with -l, show time as WORD instead of modification time: atime, access, use, ctime or status; use
140 specified time as sort key if --sort=times
141
142
143 --time-style=STYLE
144 with -l, show times using style STYLE: full-iso, long-iso, iso, locale, +FORMAT. FORMAT is
145 interpreted like ‘date’; if FORMAT is FORMAT1<newline>FORMAT2, FORMAT1 applies to non-recent
146 files and FORMAT2 to recent files; if STYLE is prefixed with ‘posix-’, STYLE takes effect only
147 outside the POSIX locale
148
149 -t sort by modification time
150
151 -T, --tabsize=COLS
152 assume tab stops at each COLS instead of 8
153
154 -u with -lt: sort by, and show, access time with -l: show access time and sort by name otherwise:
155 sort by access time
156
157 -U do not sort; list entries in directory order. In combination with one_per_line format ‘-1’, it
158 will show files immediately and it has no memory limitations.
159
160 -v sort by version
161
162 -w, --width=COLS
163 assume screen width instead of current value
164
165 -x list entries by lines instead of by columns
166
167 -X sort alphabetically by entry extension
168
169 -1 list one file per line
170
171 SELinux options:
172
173 --lcontext
174 Display security context. Enable -l. Lines will probably be too wide for most displays.
175
176 -Z, --context
177 Display security context so it fits on most displays. Displays only mode, user, group, security
178 context and file name.
179
180 --scontext
181 Display only security context and file name.
182
183 --help display this help and exit
184
185 --version
186 output version information and exit
187
188 SIZE may be (or may be an integer optionally followed by) one of following: kB 1000, K 1024, MB
189 1000*1000, M 1024*1024, and so on for G, T, P, E, Z, Y.
190
191 By default, color is not used to distinguish types of files. That is equivalent to using --color=none.
192 Using the --color option without the optional WHEN argument is equivalent to using --color=always. With
193 --color=auto, color codes are output only if standard output is connected to a terminal (tty). The
194 environment variable LS_COLORS can influence the colors, and can be set easily by the dircolors command.
195
196 Exit status is 0 if OK, 1 if minor problems, 2 if serious trouble.
197
198 AUTHOR
199 Written by Richard Stallman and David MacKenzie.
200
201 REPORTING BUGS
202 Report bugs to <bug-coreutils@gnu.org>.
203
204 COPYRIGHT
205 Copyright © 2006 Free Software Foundation, Inc.
206 This is free software. You may redistribute copies of it under the terms of the GNU General Public
207 License <http://www.gnu.org/licenses/gpl.html>. There is NO WARRANTY, to the extent permitted by law.
208
209 SEE ALSO
210 The full documentation for ls is maintained as a Texinfo manual. If the info and ls programs are prop-
211 erly installed at your site, the command
212
213 info ls
214
215 should give you access to the complete manual.
216
217 ls 5.97 July 2011 LS(1)

 

posted on 2012-03-27 21:19  Lo0ong  阅读(11551)  评论(0编辑  收藏  举报

导航