Linux中的15个基本‘ls’命令示例

Linux中的15个基本‘ls’命令示例

ls命令是Linux系统中最被频繁使用的命令之一,我相信ls命令一定是你进入一台Linux系统的电脑打开命令提示符后第一个使用的命令。我们每天都在频繁地使用ls命令,即使我们可能没有意识也从来用不到所有可用的选项。本文中,我们将讨论下一些基本的ls命令并且覆盖尽可能多的有关参数来讲解。

Linux的ls命令

1. 不带任何选项列出文件

不带选项的ls命令来光秃秃地列出文件和目录,我们是不能看到像文件类型、大小、修改日期和时间、权限以及链接这样具体的信息的。


  1. # ls
  2. 0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templates
  3. anaconda-ks.cfg Documents fbcmd_update.php install.log Music Public Videos

2 带 –l 选项列出文件列表

你看,ls -l(-l是字母不是“1”)就能展示出是文件还是目录,它的大小、修改日期和时间、文件或目录的名字以及文件的属主和它的权限。


  1. # ls -l
  2. total 176
  3. -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
  4. -rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
  5. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
  6. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
  7. drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
  8. -rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
  9. -rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
  10. -rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
  11. -rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
  12. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
  13. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
  14. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
  15. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
  16. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos

3. 浏览隐藏文件

列出所有文件包括以‘.’开头的隐藏文件。


  1. # ls -a
  2. . .bashrc Documents .gconfd install.log .nautilus .pulse-cookie
  3. .. .cache Downloads .gnome2 install.log.syslog .netstat.swp .recently-used.xbel
  4. 0001.pcap .config .elinks .gnome2_private .kde .opera .spice-vdagent
  5. anaconda-ks.cfg .cshrc .esd_auth .gtk-bookmarks .libreoffice Pictures .tcshrc
  6. .bash_history .dbus .fbcmd .gvfs .local .pki Templates
  7. .bash_logout Desktop fbcmd_update.php .ICEauthority .mozilla Public Videos
  8. .bash_profile .digrc .gconf index.html Music .pulse .wireshark

4. 用 -lh 选项来以易读方式列出文件

用-lh组合选项,以易读方式来显示大小。


  1. # ls -lh
  2. total 176K
  3. -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
  4. -rw-------. 1 root root 1.6K Jul 31 02:17 anaconda-ks.cfg
  5. drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop
  6. drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Documents
  7. drwxr-xr-x. 4 root root 4.0K Aug 16 02:55 Downloads
  8. -rw-r--r--. 1 root root 21K Aug 12 12:42 fbcmd_update.php
  9. -rw-r--r--. 1 root root 46K Jul 31 09:58 index.html
  10. -rw-r--r--. 1 root root 48K Jul 31 02:17 install.log
  11. -rw-r--r--. 1 root root 12K Jul 31 02:13 install.log.syslog
  12. drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Music
  13. drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Pictures
  14. drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Public
  15. drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Templates
  16. drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Videos

5. 以尾部以‘/’字符结尾的方式列出文件和目录

使用 ls 命令的 -F 选项,会在每个目录的末尾添加“/”字符显示。


  1. # ls -F
  2. 0001.pcap Desktop/ Downloads/ index.html install.log.syslog Pictures/ Templates/
  3. anaconda-ks.cfg Documents/ fbcmd_update.php install.log Music/ Public/ Videos/

6. 倒序列出文件

ls -r 选项能以倒序方式显示文件和目录。


  1. # ls -r
  2. Videos Public Music install.log fbcmd_update.php Documents anaconda-ks.cfg
  3. Templates Pictures install.log.syslog index.html Downloads Desktop 0001.pcap

7. 递归列出子目录

ls -R 选项能列出非常长的目录树,来看看示例输出:


  1. # ls -R
  2. total 1384
  3. -rw-------. 1 root root 33408 Aug 8 17:25 anaconda.log
  4. -rw-------. 1 root root 30508 Aug 8 17:25 anaconda.program.log
  5. ./httpd:
  6. total 132
  7. -rw-r--r-- 1 root root 0 Aug 19 03:14 access_log
  8. -rw-r--r--. 1 root root 61916 Aug 10 17:55 access_log-20120812
  9. ./lighttpd:
  10. total 68
  11. -rw-r--r-- 1 lighttpd lighttpd 7858 Aug 21 15:26 access.log
  12. -rw-r--r--. 1 lighttpd lighttpd 37531 Aug 17 18:21 access.log-20120819
  13. ./nginx:
  14. total 12
  15. -rw-r--r--. 1 root root 0 Aug 12 03:17 access.log
  16. -rw-r--r--. 1 root root 390 Aug 12 03:17 access.log-20120812.gz

8. 以修改时间倒序列出

带-ltr组合选项能以文件或目录的最新修改时间的次序来显示它们。


  1. # ls -ltr
  2. total 176
  3. -rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
  4. -rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
  5. -rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
  6. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
  7. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
  8. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
  9. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
  10. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
  11. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
  12. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
  13. -rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
  14. -rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
  15. drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
  16. -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap

9. 按文件大小排序

带-lS组合选项能按文件从大到小的次序显示。


  1. # ls -lS
  2. total 176
  3. -rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log
  4. -rw-r--r--. 1 root root 46701 Jul 31 09:58 index.html
  5. -rw-r--r--. 1 root root 21262 Aug 12 12:42 fbcmd_update.php
  6. -rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog
  7. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop
  8. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents
  9. drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads
  10. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music
  11. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures
  12. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public
  13. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates
  14. drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
  15. -rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfg
  16. -rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap

10. 显示文件或目录的索引节点号

我们有时候可以看到一些数字打印在文件或目录名之前,带-i选项就能列出文件或目录的索引节点号。


  1. # ls -i
  2. 20112 0001.pcap 23610 Documents 23793 index.html 23611 Music 23597 Templates
  3. 23564 anaconda-ks.cfg 23595 Downloads 22 install.log 23612 Pictures 23613 Videos
  4. 23594 Desktop 23585 fbcmd_update.php 35 install.log.syslog 23601 Public

11. 显示ls命令的版本

查看ls命令的版本。


  1. # ls --version
  2. ls (GNU coreutils) 8.4
  3. Copyright (C) 2010 Free Software Foundation, Inc.
  4. License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
  5. This is free software: you are free to change and redistribute it.
  6. There is NO WARRANTY, to the extent permitted by law.
  7. Written by Richard M. Stallman and David MacKenzie.

12. 显示帮助页面

列出ls命令的选项帮助页面。


  1. # ls --help
  2. Usage: ls [OPTION]... [FILE]...

13. 列出目录信息

用ls -l命令列出/tmp目录下的文件,其中-ld参数可以只显示/tmp目录的信息。


  1. # ls -l /tmp
  2. total 408
  3. drwx------. 2 narad narad 4096 Aug 2 02:00 CRX_75DAF8CB7768
  4. -r--------. 1 root root 384683 Aug 4 12:28 htop-1.0.1.tar.gz
  5. drwx------. 2 root root 4096 Aug 4 11:20 keyring-6Mfjnk
  6. drwx------. 2 root root 4096 Aug 16 01:33 keyring-pioZJr
  7. drwx------. 2 gdm gdm 4096 Aug 21 11:26 orbit-gdm
  8. drwx------. 2 root root 4096 Aug 19 08:41 pulse-gl6o4ZdxQVrX
  9. drwx------. 2 narad narad 4096 Aug 4 08:16 pulse-UDH76ExwUVoU
  10. drwx------. 2 gdm gdm 4096 Aug 21 11:26 pulse-wJtcweUCtvhn
  11. -rw-------. 1 root root 300 Aug 16 03:34 yum_save_tx-2012-08-16-03-34LJTAa1.yumtx


  1. # ls -ld /tmp/
  2. drwxrwxrwt. 13 root root 4096 Aug 21 12:48 /tmp/

14. 显示文件的UID和GID

用ls -n命令来显示文件和目录的UID(译者注:userid,用户ID)和GID(译者注:groupid,组ID)。


  1. # ls -n
  2. total 36
  3. drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Downloads
  4. drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Music
  5. drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Pictures
  6. -rw-rw-r--. 1 500 500 12 Aug 21 13:06 tmp.txt
  7. drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Videos

15. ls命令和它的别名

我们给ls命令设置如下别名之后,当我们执行ls命令的时候它会默认执行-l选项并且像上文提到的那样显示长列表。


  1. # alias ls="ls -l"

注意:我们可以通过不加任何参数的alias命令来看到目前系统中可用的所有alias设置,当然它们同时也可以unalias来取消。


  1. # alias
  2. alias cp='cp -i'
  3. alias l.='ls -d .* --color=auto'
  4. alias ll='ls -l --color=auto'
  5. alias ls='ls --color=auto'
  6. alias mv='mv -i'
  7. alias rm='rm -i'
  8. alias which='alias | /usr/bin/which --tty-only --read-alias --show-dot --show-tilde'

删除一项之前定义的alias设置,只需用unalias命令即可。


  1. # unalias ls

下篇文章我们将讨论更多更高级的ls命令以及示例,如果我们在本文有遗漏了任何东西,请通过评论让我们获悉。

原文发布时间:2015-03-25

本文来自云栖合作伙伴“linux中国”

时间: 2024-08-03 13:02:20

Linux中的15个基本‘ls’命令示例的相关文章

Linux中的15个‘echo’ 命令实例

Linux中的15个'echo' 命令实例 echo是一种最常用的与广泛使用的内置于Linux的bash和C shell的命令,通常用在脚本语言和批处理文件中来在标准输出或者文件中显示一行文本或者字符串.   echo命令的语法是: echo [选项] [字符串] 1. 输入一行文本并显示在标准输出上 $ echo Tecmint is a community of Linux Nerds 会输出下面的文本: Tecmint is a community of Linux Nerds 2. 输出

Linux中RPM文件操作的常用命令总结_Linux

简介 众所周知在Linux操作系统中,有一个系统软件包,它的功能类似于Windows里面的"添加/删除程序",但是功能又比"添加/删除程序"强很多,它就是Red Hat Package Manager(简称RPM).本文介绍的就是关于Linux中RPM文件操作的常用命令,下面话不多说,来一起看看吧. 一.RPM 安装操作 命令: rpm -i 需要安装的包文件名 举例如下: rpm -i example.rpm 安装 example.rpm 包: rpm -iv e

Linux中文件的压缩与解压缩命令操作示例集锦

  所谓压缩就是将原有的文件通过不同的编码技术进行运算,以减少数据存储所需要的空间,使用前再利用解压缩还原源文件的内容即可. 和windows一样,在linux下也存在多种压缩与解压缩方法. 1.zip压缩与解压缩 zip是最为广泛使用的压缩程序,经它压缩的文件会产生扩展名为zip的压缩文件,而且这种格式在多种系统上可以使用,像windows中的winzip 下面看一下在linux中如何建立zip文件. 我们在终端中输入zip会出现这个命令的一些介绍和参数的意义. 代码如下: xiaopeng@

Linux中快捷键的使用,who命令,rm命令,ps命令,cd命令,kill命令,find命令,grep命令,tar命令(gz、tar、bz2),用户管理,vim部分配置,相关命令

    1.进入Ubuntu之后打开终端窗口的快捷键是:           ctrl + alt+t:通过这个命令可以打开终端.          ctrl + alt+t:通过这个命令可以打开终端.     再开一个tab选项卡式的终端的快捷键是:ctrl+shift+t,切换还窗口的方式是:Alt+序号     另外也可以通过ctrl+shift+n的方式打开另外一种终端   2.关闭一个终端窗口的方式是:           Alt +F4,在Ubuntu下还可以是exit     3.

Linux中用于进程显示的top命令使用实例集锦

  Linux中的top命令显示系统上正在运行的进程.它是系统管理员最重要的工具之一.被广泛用于监视服务器的负载.在本篇中,我们会探索top命令的细节.top命令是一个交互命令.在运行top的时候还可以运行很多命令.我们也会探索这些命令.(注:不同发行版的top命令在各种细节有不同,如果发现不同时,请读你的帮助手册和命令内的帮助.) 1. Top 命令输出: 首先,让我们了解一下输出.top命令会显示系统的很多信息.我们需要理解不同部分输出的意义:默认运行时,top命令会显示如下输出: (默认显

Linux中查看进程及杀死进程命令

Linux中想杀死fcitx进程,然后再重启它. root@www.linuxidc.com:/home/zhangbin# ps -e | grep 'fcitx'  3405 ?        00:00:00 fcitx <defunct>  3415 ?        00:00:02 fcitx   不显示标题 root@www.linuxidc.com:/home/zhangbin# ps -h Warning: bad ps syntax, perhaps a bogus '-'

linux中cat、more、less命令区别详解

众所周知linux中命令cat.more.less均可用来查看文件内容,主要区别有:cat是一次性显示整个文件的内容,还可以将多个文件连接起来显示,它常与重定向符号配合使用,适用于文件内容少的情况:more和less一般用于显示文件内容超过一屏的内容,并且提供翻页的功能.more比cat强大,提供分页显示的功能,less比more更强大,提供翻页,跳转,查找等命令.而且more和less都支持:用空格显示下一页,按键b显示上一页.下面详细介绍这3个命令. cat   [plain] view p

Linux中目录的创建与删除命令使用说明

  mkdir命令 mkdir 用于创建一个或多个目录 语法: 语法是 mkdir [命令开关] 目录 命令开关: -m 文目录设置操作权限 -p 如果上级目录不存在,同时创建它们. -v 输出创建的每个目录的信息 示例: Create directory: mkdir test 上述命令将创建目录 'test'. 创建目录并设置访问权限: mkdir -m 666 test 上述命令将创建目录 'test' 并设置读取和写入权限. rmdir 命令 rmdir 命令用来删除/移除目录及其子目录

linux中cat more less head tail 命令区别

1.cat 显示文件连接文件内容的工具: cat 是一个文本文件查看和连接工具.查看一个文件的内容,用cat比较简单,就是cat 后面直接接文件名. 比如:[root@localhost ~]# cat /etc/fstab 为了便于新手弟兄灵活掌握这个工具,我们多说一点常用的参数:1.0 cat 语法结构: cat [选项] [文件]... 选项  -A, --show-all           等价于 -vET  -b, --number-nonblank    对非空输出行编号  -e