ls命令是Linux中最常用的命令之一。我相信ls命令是你进入Linux 系统命令提示符时的首选命令。

我们每天都在使用ls命令,甚至常常意识不到这一点,也从没有使用所有可用的ls选项。在这篇文章,我们将讨论基本的ls命令并尽可能多的覆盖其参数。

1、使用不带选项的ls命令列出文件

不带选项的ls命令列出的文件和目录几乎不带什么格式。我们无法看到像文件类型,大小,修改时间,权限和链接状态等细节信息。

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

2、带-l选项列出文件

这里,ls -l(-l不是一个字符)显示文件或目录的大小,修改时间,文件或目录名,文件所有者和它的权限。

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

3、查看隐藏文件

显示包括以点('.')开头的所有文件

# ls -a
. anaconda-ks.cfg .bash_logout .bashrc .lesshst .ssh .viminfo
.. .bash_history .bash_profile .cshrc .pki .tcshrc

4、通过-lh选项以人类易读的方式列出文件

通过结果-lh选项,以人类易读的方式显示文件大小。

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

5、显示文件和目录以"/"结尾

使用带-F选项的ls命令,将在每个目录的末尾加上“/”

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

6、以反序显示文件列表

接下来的命令ls -r以反序显示文件和目录

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

7、递归显示子目录

ls -R选项将显示非常长的目录树。看一个这条命令的示例输出。

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

8、向序输出

ls命令结合-ltr选项将在最后显示修改过的文件或目录

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

10、显示文件或目录的inode

我们能看到在根目录前一些数字被打印出来。通过i选项显示文件或目录的inode号。

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

11、显示ls的版本

检查ls的版本

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

12、显示帮助页

显示ls命令以及选项的帮助

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

13、显示目录信息

在/tmp目录下,用ls -l显示文件。其中-ld选项显示/tmp目录的信息。

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

14、显示文件的UID和GID

为显示文件和目录的UID和GID,使用ls命令的-n选项

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

15、ls命令和它的别名

我们已经为ls命令建立了别名,当我们执行ls命令的时候,它将默认带上-l选项并以如前所述的长格式显示。

# alias ls="ls -l"

注意:我们可以看到如下系统中可用的别名数量。alias命令同样可以如下示例取消别名

# alias
alias cp='cp -i'
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'

为删除前面定义的别名,使用unalias命令即可。

# unalias ls

在我们的下一篇文章中我们将谈及更多或高级的ls命令以及他们的示例。同时,我建议你看看关于ls命令的面试题,如果我们在这个列表遗漏了什么,请在评论区提醒我们。

原文链接:https://www.tecmint.com/15-basic-ls-command-examples-in-linux/

Linux中的15个基本'ls'命令示例的更多相关文章

  1. [转帖]Linux中的15个基本‘ls’命令示例

    Linux中的15个基本‘ls’命令示例 https://linux.cn/article-5109-1.html ls -lt 和 ls -ltr 来查看文件新旧顺序. list time rese ...

  2. Linux中假设系统丢失了ls命令

    假设系统丢失了ls命令,如何修复: 1.查询ls命令属于那个rpm包[root@localhost ~]# rpm -qf /bin/lscoreutils-8.4-43.el6.x86_64 2.挂 ...

  3. Linux中的13个基本Cat命令示例

    cat(“ concatenate ”的缩写)命令是Linux / Unix等操作系统中最常用的命令之一.cat命令允许我们创建单个或多个文件,查看文件包含,连接文件以及在终端或文件中重定向输出.在本 ...

  4. linux中cat、more、less命令区别详解##less 最合适最好用,和vim一样好用

    linux中cat.more.less命令区别详解 caoxinyiyi关注 0.0362018.07.02 15:46:17字数 641阅读 516 linux中命令cat.more.less均可用 ...

  5. linux在home目录下使用ls命令卡死

    linux在home目录下使用ls命令卡死,原因可能是mount的某个服务器挂掉或出啥问题了,这个时候umount掉就正常了,如果umount提示device is busy,这时可以使用强制卸载   ...

  6. Linux基础知识第九讲,linux中的解压缩,以及软件安装命令

    目录 Linux基础知识第九讲,linux中的解压缩,以及软件安装命令 一丶Linux Mac Windows下的压缩格式简介 2.压缩以及解压缩 3.linux中的软件安装以及卸载 1.apt进行安 ...

  7. 在Linux中新增与删除用户可以使用命令:Useradd

    在Linux中新增与删除用户可以使用命令:Useradd 我们先使用man命令理解一下Useradd的用法 新增与删除用户操作需要先获取高级用户权限 输入命令:sudo -i 确定后输入高级用户密码 ...

  8. Linux中查看各文件夹大小命令:du -h --max-depth=1

    Linux中查看各文件夹大小命令:du -h --max-depth=1 du [-abcDhHklmsSx] [-L <符号连接>][-X <文件>][--block-siz ...

  9. Linux命令备忘录: jobs 显示Linux中的任务列表及任务状态命令

    Linux jobs命令用法详解:显示Linux中的任务列表及任务状态命令 jobs命令用于显示Linux中的任务列表及任务状态,包括后台运行的任务.该命令可以显示任务号及其对应的进程号.其中,任务号 ...

随机推荐

  1. Rocketmq源码导入eclipse时报错的解决方法

    1,Plugin execution not covered by lifecycle configuration: org.apache.maven.plugins:maven-help-plugi ...

  2. tiny6410的启动参数

    bootargs=root=/dev/mtdblock2 rootfstype=yaffs2 init=/linuxrc console=ttySAC0,115200 bootcmd=nand led ...

  3. WIFI CAT ET III Adapter Caterpillar ET3 New Arrival

    The old bluetooth cat et adatper iii has stopped production, and you can get the new WIFI CAT Caterp ...

  4. laravel中empty(),is_null() 以及isEmpty()

    PHP中 empty() empty() 函数用于检查一个变量是否为空. if(empty($result->order)){ //操作 } is_null() is_null() 函数用于检测 ...

  5. Sanic

    基础 厉害了我的 Sanic hello word, Sanic

  6. Sublime text 3 For LINUX 注册方法&关闭更新提示

    在 /etc/hosts 文件加入以下 127.0.0.1 www.sublimetext.com 127.0.0.1 license.sublimehq.com 然后再Sublime输入以下注册码 ...

  7. CVE-2018-8120 Windows权限提升

    来源 : bigric3/cve-2018-8120 Detail : cve-2018-8120-analysis-and-exploit 演示图 下载 CVE-2018-8120.zip

  8. 【Entity Framework】Model First Approach

    EF中的model first 所谓mf, 就是使用vs提供的edm designer去设计model,然后将设计好的model使用vs在指定的数据库中生成数据库即可. 当你的项目既没有数据库也没有c ...

  9. 数组toString()方法,数组常用操作

    int[] arr ={1,2,3,4,5}; String arrString = Arrays.toString(arr); //输出[I@7150bd4d System.out.println( ...

  10. winform左右滑动

    public static class FormTransform { public static void TransformSize(Form frm, int newWidth, int new ...