linux命令学习笔记之ls

简介:
ls是英文单词list的简写,其功能为列出目录的内容。在linux/unix下这是用户最常用的一个命令之一,因为用户需要不时地查看某个目录的内容.该命令类似于DOS下的dir命令。

语法如下:
To Display Contents of Directory or Name of File

ls [ -1 ] [ -A ] [ -C ] [ -F ] [-H | -L ] [ -N ] [ -R ] [ -X] [ -a ] [ -b ] [ -c ] [ -d ] [ -e ] [ -f ] [ -g ] [ -i ] [ -l] [ -m ] [ -n ] [ -o ] [ -p ] [ -q ] [ -r ] [ -s ] [ -t ] [ -u ] [ -U ] [ -x ] [ File … ]

To Display Contents of Directory

ls -f [ -C ] [ -d ] [ -i ] [ -m ] [ -s ] [ -X] [ -x ] [ -1 ] [ -U ] [ Directory … ]

对于常用参数的解释(完整解释参考后面引用的AIX的man手册):

-a 显示所有档案及目录 (ls内定将档案名或目录名称开头为”.”的视为隐藏档,不会列出)
-l 除档案名称外,亦将档案型态、权限、拥有者、档案大小等资讯详细列出
-r 将档案以相反次序显示(原定依英文字母次序)
-t 将档案依建立时间之先后次序列出
-A 同 -a ,但不列出 “.” (目前目录) 及 “..” (父目录)
-F 在列出的档案名称后加一符号;例如可执行档则加 “*”, 目录则加 “/”
-R 若目录下有档案,则以下之档案亦皆依序列出

示例:
列出目前工作目录下所有名称是 s 开头的档案,愈新的排愈后面 :
ls -ltr s*

将 /bin 目录以下所有目录及档案详细资料列出 :
ls -lR /bin

列出目前工作目录下所有档案及目录;目录于名称后加 “/”, 可执行档于名称后加 “*” :
ls -AF


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

AIX的man手册:

ls Command

Purpose

Displays the contents of a directory.

Syntax

To Display Contents of Directory or Name of File

ls [ -1 ] [ -A ] [ -C ] [ -F ] [-H | -L ] [ -N ] [ -R ] [ -X] [ -a ] [ -b ] [ -c ] [ -d ] [ -e ] [ -f ] [ -g ] [ -i ] [ -l
] [ -m ] [ -n ] [ -o ] [ -p ] [ -q ] [ -r ] [ -s ] [ -t ] [ -u ] [ -U ] [ -x ] [ File … ]

To Display Contents of Directory

ls -f [ -C ] [ -d ] [ -i ] [ -m ] [ -s ] [ -X] [ -x ] [ -1 ] [ -U ] [ Directory … ]

Description

The ls command writes to standard output the contents of each specified Directory parameter or the name of each specified
File parameter, along with any other information you ask for with the flags. If you do not specify a File or Directory
parameter, the ls command displays the contents of the current directory.

Specifying more than one of the options in the mutually exclusive pairs is not considered an error. The last option
specified in each pair determines the output format.

By default, the ls command displays all information in alphabetic order by file name. The collating sequence is determined
by the LANG or LC_COLLATE environment variable. The “National Language Support Overview for Programming” in AIX 5L Version
5.3 General Programming Concepts: Writing and Debugging Programs contains more details.

When the ls command displays the contents of a directory, it does not show entries for files whose names begin with a .
(dot) unless you use the -a or -A flag. If the command is executed by root, it uses the -A flag by default.

There are three main ways to format the output:
* List one entry per line.
* List entries in multiple columns by specifying either the -C or -x flag. The -C flag is the default format when output
is to a tty. The ls command displays single column output if file or directory names are too long.
* List entries in a comma-separated series by specifying the -m flag.

To determine the number of character positions in the output line, the ls command uses the COLUMNS environment variable. If
this variable is not set, the command gets the current column value of the display. If the ls command cannot determine the
number of character positions by either of these methods, it uses a default value of 80.

The mode displayed with the -U flag is the same as with the -l flag, except for the addition of an 11th character
interpreted as follows:
E
Indicates a file has extended attributes (EA) information. The EA of a file is displayed by using the getea command.

Indicates a file does not have extended attributes information.

The mode displayed with the -e and -l flags is interpreted as follows:

If the first character is:
d
The entry is a directory.
b

The entry is a block special file.
c
The entry is a character special file.
l
The entry is a symbolic link, and either the -N flag was specified or the symbolic link did not point to an existing
file.
p
The entry is a first-in,first-out (FIFO) special file.
s
The entry is a local socket.

The entry is an ordinary file.

The next nine characters are divided into three sets of three characters each. The first set of three characters show the
owner’s permission. The next set of three characters show the permission of the other users in the group. The last set of
three characters shows the permission of anyone else with access to the file. The three characters in each set indicate,
respectively, read, write, and execute permission of the file. Execute permission of a directory lets you search a
directory for a specified file.

Permissions are indicated as follows:
r
Read
w
Write (edit)
x
Execute (search)

Corresponding permission not granted

The group-execute permission character is s if the file has set-group-ID mode. The user-execute permission character is s
if the file has set-user-ID mode. The last character of the mode (usually x or -) is T if the 01000 (octal) bit of the mode
is set (see the chmod command for the meaning of this mode). The indications of set-ID and 01000 bit of the mode are
capitalized (S and T, respectively) if the corresponding execute permission is not set. The mode t indicates that the
sticky bit is on for the file or the directory.

The mode displayed with the -e flag is the same as with the -l flag, except for the addition of an 11th character
interpreted as follows:
+
Indicates a file has extended security information. For example, the file may have extended ACL, TCB, or TP attributes
in the mode.

The access control information (ACL) of a file is displayed by using the aclget command. The value of the TCB and TP
attributes are displayed by using the chtcb command.

Indicates a file does not have extended security information.

When the size of the files in a directory are listed, the ls command displays a total count of blocks, including indirect
blocks.

Flags

-A
Lists all entries except . (dot) and .. (dot-dot).
-a
Lists all entries in the directory, including the entries that begin with a . (dot).
-b
Displays nonprintable characters in an octal (\nnn) notation.
-c

Uses the time of last modification of the i-node for either sorting (when used with the -t flag) or for displaying
(when used with the -l flag). This flag must be used with either the -t or -l flag, or both.
-C
Sorts output vertically in a multicolumn format. This is the default method when output is to a terminal.
-d
Displays only the information for the directory named. Directories are treated like files, which is helpful when using
the -l flag to get the status of a directory.
-e
Displays the mode (including security information), number of links, owner, group, size (in bytes), time of last
modification, and name of each file. If the file is a special file, the size field contains the major and minor device
numbers. If the file is a symbolic link, the path name of the linked-to file is printed preceded by a -> (minus,
greater than) sign. The attributes of the symbolic link are displayed.
-E
Lists space reservation, fixed extent size, and extent allocation flag information for a file. -l must be specified
with this flag.
-f
Lists the name in each slot for each directory specified in the Directory parameter. This flag turns off the -l, -t,
-s, and -r flags, and turns on the -a flag. The order of the listing is the order in which entries appear in the
directory.
-F
Puts a / (slash) after each file name if the file is a directory, an * (asterisk) if the file can be executed, an =
(equal sign) if the file is a socket, a | (pipe) sign if the file is a FIFO, and an @ for a symbolic link.

Note: Symbolic links are displayed with the trailing -> only if the -N flag is used or if the link points to a
nonexistent file. Otherwise, information about the target file is displayed. You can also invoke this option by
entering the ls -f command.

-g
Displays the same information as the -l flag, except the -g flag suppresses display of the owner and symbolic link
information.
-H
If a symbolic link referencing a file of type directory is specified on the command line, the ls command shall
evaluate the file information and file type to be those of the file referenced by the link, and not the link itself;
however, the ls command shall write the name of the link itself and not the file referenced by the link.
-i
Displays the i-node number in the first column of the report for each file.
-L
Lists the file or directory contents that the link references. This is the default action. Symbolic links are
followed. If the -l option is used, the -N option becomes the default, and no symbolic links are followed. When the -l
option is used, only the -L option can override the -N default.
-l
(Lower case L) Displays the mode, number of links, owner, group, size (in bytes), and time of last modification for
each file. If the file is a special file, the size field contains the major and minor device numbers. If the time of
last modification is greater than six months ago, the time field is shown in the format month date year where as files
modified within six months the time field is shown as month date time format.

If the file is a symbolic link, the path name of the linked-to file is printed preceded by a ->. The attributes of the
symbolic link are displayed. The -n, -g, and -o flag overrides the -l flag.

Notes:
1 A symbolically linked file is followed by an arrow and the contents of the symbolic link.
2 The performance of the ls command when used with the -l option can be improved by executing the mkpasswd
command. This is helpful when a directory contains files owned by different users, such as the /tmp directory.
-m
Uses stream output format (a comma-separated series).
-n
Displays the same information as the -l flag, except that the -n flag displays the user and the group IDs instead of
the user and group names.

-N
Does not follow symbolic links when determining the status of a file.

Note: If both the -L and -N options are used, the last one will dominate. Also, any time a symbolic link is given that
includes a / (slash) as the final character, the link will automatically be followed regardless of any options used.

-o
Displays the same information as the -l flag, except the -o flag suppresses display of the group and symbolic link
information.
-p
Puts a slash after each file name if that file is a directory. This is useful when you pipe the output of the ls
command to the pr command, as follows:

ls -p | pr -5 -t -w80
-q
Displays nonprintable characters in file names as a ? (question mark).
-r
Reverses the order of the sort, giving reverse alphabetic or the oldest first, as appropriate.
-R
Lists all subdirectories recursively.
-s
Gives size in kilobytes (including indirect blocks) for each entry.
-t
Sorts by time of last modification (latest first) instead of by name.
-U
Displays similar information as the -l flag. Displays the mode (including security information and named extended
attribute information), number of links, owner, group, size (in bytes), time of last modification, and name of each
file. If the file is a special file, the size field contains the major and minor device numbers. If the file is a
symbolic link, the path name of the linked-to file is printed preceded by a -> (minus, greater than) sign. The
attributes of the symbolic link are displayed.
-u
Uses the time of the last access, instead of the time of the last modification, for either sorting (when used with the
-t flag) or for displaying (when used with the -l flag). This flag has no effect if it is not used with either the -t
or -l flag, or both.
-x
Sorts output horizontally in a multi-column format.
-X
Prints long user names when used with other flags that display user names. The upper limit is determined by the
max_logname ODM attribute in the PdAt and CuAt object classes. If a user name is greater than the max_logname
attribute, it will be truncated to the number of characters as specified by the max_logname attribute, less one
character.
-1
Forces output into one-entry-per-line format. This is the default when the output is not directed to a terminal.

Exit Status

This command returns the following exit values:
0
All files were written successfully.
>0
An error occurred.

Examples
1 To list all files in the current directory, type: ls -a

This lists all files, including . (dot), .. (dot-dot), and other files with names beginning with a dot.
2 To display detailed information, type: ls -l chap1 .profile

This displays a long listing with detailed information about chap1 and .profile.

3 To display detailed information about a directory, type: ls -d -l . manual manual/chap1

This displays a long listing for the directories . and manual, and for the file manual/chap1. Without the -d flag,
this would list the files in the . and manual directories instead of the detailed information about the directories
themselves.
4 To list the files in order of modification time, type: ls -l -t

This displays a long listing of the files that were modified most recently, followed by the older files.
5 To display detailed information with expanded user and group name, type:

ls -lX .profile
This displays a long listing with detailed information about .profile.
6 To display about whether extended attributes are set on the files in current directory, type:

ls -U
Example output:

-rwSr-x—+ 1 root system 28 Apr 29 03:23 only_aixc
-rwSr-x—E 1 root system 4 Apr 29 03:23 only_aixc_ea
-rw-r–r–E 1 root system 4 Apr 29 03:23 only_ea
———-+ 1 root system 265 Apr 29 03:23 only_nfs4
———-E 1 root system 64 Apr 29 03:23 only_nfs4_ea
-rw-r–r— 1 root system 4 Apr 29 03:23 only_regular

Files

/usr/bin/ls
Contains the ls command.
/etc/passwd
Contains user IDs.
/etc/group
Contains group IDs.
/usr/share/lib/terminfo/*
Contains terminal information.

Related Information

The aclget command, chmod command, chtcb command, find command, mkpasswd command, qprt command.

Directory Overview in AIX 5L Version 5.3 System User’s Guide: Operating System and Devices describes the structure and
characteristics of directories in the file system.

Files and Directories Overview in AIX 5L Version 5.3 System User’s Guide: Operating System and Devices.

Files Overview in AIX 5L Version 5.3 System User’s Guide: Operating System and Devices describes files, file types, and how
to name files.

Linking Files and Directories in AIX 5L Version 5.3 System User’s Guide: Operating System and Devices explains the concept
of file linking.

Shells Overview in AIX 5L Version 5.3 System User’s Guide: Operating System and Devices describes shells, the different
types, and how they affect the way commands are interpreted.

File and Directory Access Modes in AIX 5L Version 5.3 System User’s Guide: Operating System and Devices introduces file
ownership and permissions to access files and directories.

National Language Support Overview for Programming in AIX 5L Version 5.3 National Language Support Guide and Reference
explains collating sequences, equivalence classes, and locale.

此条目发表在lnmpa分类目录,贴了, 标签。将固定链接加入收藏夹。

发表评论

您的电子邮箱地址不会被公开。

此站点使用Akismet来减少垃圾评论。了解我们如何处理您的评论数据