ls命令
1、 ls命令用来显示目标列表,在Linux中是使用率较高的命令。ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件。 2、语法
ls(选项)(参数)
3、选项
-a:显示所有档案及目录(ls内定将档案名或目录名称为“.”的视为影藏,不会列出);
-A:显示除影藏文件“.”和“..”以外的所有文件列表;
-C:多列显示输出结果。这是默认选项;
-l:与“-C”选项功能相反,所有输出信息用单列格式输出,不输出为多列;
-F:在每个输出项后追加文件的类型标识符,具体含义:“*”表示具有可执行权限的普通文件,“/”表示目录,“@”表示符号链接,“|”表示命令管道FIFO,“=”表示sockets套接字。当文件为普通文件时,不输出任何标识符;
-b:将文件中的不可输出的字符以反斜线“”加字符编码的方式输出;
-c:与“-lt”选项连用时,按照文件状态时间排序输出目录内容,排序的依据是文件的索引节点中的ctime字段。与“-l”选项连用时,则排序的一句是文件的状态改变时间;
-d:仅显示目录名,而不显示目录下的内容列表。显示符号链接文件本身,而不显示其所指向的目录列表;
-f:此参数的效果和同时指定“aU”参数相同,并关闭“lst”参数的效果;
-i:显示文件索引节点号(inode)。一个索引节点代表一个文件;
--file-type:与“-F”选项的功能相同,但是不显示“*”;
-k:以KB(千字节)为单位显示文件大小;
-l:以长格式显示目录下的内容列表。输出的信息从左到右依次包括文件名,文件类型、权限模式、硬连接数、所有者、组、文件大小和文件的最后修改时间等;
-m:用“,”号区隔每个文件和目录的名称;
-n:以用户识别码和群组识别码替代其名称;
-r:以文件名反序排列并输出目录内容列表;
-s:显示文件和目录的大小,以区块为单位;
-t:用文件和目录的更改时间排序;
-L:如果遇到性质为符号链接的文件或目录,直接列出该链接所指向的原始文件或目录;
-R:递归处理,将指定目录下的所有文件及子目录一并处理;
--full-time:列出完整的日期与时间;
--color[=WHEN]:使用不同的颜色高亮显示不同类型的。
4、参数
**目录:**指定要显示列表的目录,也可以是具体的文件。
5、实例
- ls -l 列出文件的详细信息
[root@ajlinux-01 ~]# ls -l /root
总用量 4
-rw-------. 1 root root 1418 3月 22 22:03 anaconda-ks.cfg
- ls -i 查看文件的inod号
[root@ajlinux-01 ~]# ls -i /root
33582978 anaconda-ks.cfg
- ls -lh 自动变化单位
[root@ajlinux-01 ~]# ls -lh /root
总用量 4.0K
-rw-------. 1 root root 1.4K 3月 22 22:03 anaconda-ks.cfg
- ls -la查看隐藏目录 .表示当前目录
[root@ajlinux-01 ~]# ls -la /root
总用量 28
dr-xr-x---. 3 root root 147 3月 23 15:10 .
dr-xr-xr-x. 17 root root 224 3月 23 14:56 ..
-rw-------. 1 root root 1418 3月 22 22:03 anaconda-ks.cfg
-rw-------. 1 root root 340 3月 23 15:15 .bash_history
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
drwx------. 2 root root 57 3月 23 15:14 .ssh
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
- ls -lta 以时间的倒序来排列文件
[root@ajlinux-01 ~]# ls -lta /root
总用量 28
-rw-------. 1 root root 340 3月 23 15:15 .bash_history
drwx------. 2 root root 57 3月 23 15:14 .ssh
dr-xr-x---. 3 root root 147 3月 23 15:10 .
dr-xr-xr-x. 17 root root 224 3月 23 14:56 ..
-rw-------. 1 root root 1418 3月 22 22:03 anaconda-ks.cfg
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
- ls -d 列出目录本身
[root@ajlinux-01 ~]# ls -d /root
/root
- man ls 查看ls的其他用法
- ls -halt 自动选择大小、显示隐藏文件、详细信息、按时间倒序排列
[root@ajlinux-01 ~]# ls -halt /root
总用量 28K
-rw-------. 1 root root 340 3月 23 15:15 .bash_history
drwx------. 2 root root 57 3月 23 15:14 .ssh
dr-xr-x---. 3 root root 147 3月 23 15:10 .
dr-xr-xr-x. 17 root root 224 3月 23 14:56 ..
-rw-------. 1 root root 1.4K 3月 22 22:03 anaconda-ks.cfg
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
文件类型
[root@ajlinux-01 ~]# ls -l /dev/log
srw-rw-rw-. 1 root root 0 3月 23 15:01 /dev/log
第一列的第一位,用来表示文件类型,d表示目录,-表示普通文件或者二进制文件,c表示字符串设备(鼠标键盘等)、l表示软链接文件(相当于windows的快捷方式)、b块设备(光驱、磁盘等)、s通讯文件socket文件
普通文档可以使用cat来查看,但是带有颜色的文件无法使用cat来查看内容,这些文件无法查看。
alias
1、 alias用来设置指令的别名,我们可以使用该命令可以将一些较长的命令进行简化。使用alias时,用户必须使用单引号''将原来的命令引起来,防止特殊字符导致错误。
[root@ajlinux-01 ~]# alias ajiang='ls -halt'
[root@ajlinux-01 ~]# ajiang /root
总用量 28K
-rw-------. 1 root root 340 3月 23 15:15 .bash_history
drwx------. 2 root root 57 3月 23 15:14 .ssh
dr-xr-x---. 3 root root 147 3月 23 15:10 .
dr-xr-xr-x. 17 root root 224 3月 23 14:56 ..
-rw-------. 1 root root 1.4K 3月 22 22:03 anaconda-ks.cfg
-rw-r--r--. 1 root root 18 12月 29 2013 .bash_logout
-rw-r--r--. 1 root root 176 12月 29 2013 .bash_profile
-rw-r--r--. 1 root root 176 12月 29 2013 .bashrc
-rw-r--r--. 1 root root 100 12月 29 2013 .cshrc
-rw-r--r--. 1 root root 129 12月 29 2013 .tcshrc
2、alias 可以查看有哪些别名
[root@ajlinux-01 ~]# alias
alias cp='cp -i'
alias egrep='egrep --color=auto'
alias fgrep='fgrep --color=auto'
alias grep='grep --color=auto'
alias l.='ls -d .* --color=auto'
alias ll='ls -l --color=auto'
alias ls='ls --color=auto'
alias mv='mv -i'
alias rm='rm -i'
alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'
3、unalias 可以取消别名
[root@ajlinux-01 ~]# unalias ajiang
[root@ajlinux-01 ~]# ajiang /root
-bash: ajiang: 未找到命令
4、 which命令用于查找并显示给定命令的绝对路径,环境变量PATH中保存了查找命令时需要遍历的目录。which指令会在环境变量$PATH设置的目录里查找符合条件的文件。也就是说,使用which命令,就可以看到某个系统命令是否存在,以及执行的到底是哪一个位置的命令。
[root@ajlinux-01 ~]# which ls
alias ls='ls --color=auto'
/usr/bin/ls
[root@ajlinux-01 ~]# which ll
alias ll='ls -l --color=auto'
/usr/bin/ls
[root@ajlinux-01 ~]# which man
/usr/bin/man
[root@ajlinux-01 ~]# which yum
/usr/bin/yum
5、PATH系统的环境变量,echo $PATH 查看path的内容,命令在?这几个目录,即可直接运行。
[root@ajlinux-01 ~]# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin