ls –没有此两个字母的命令,Unix用户和sysadmins将无法生存。无论您是每天使用10次还是每天使用100次,了解ls命令的强大功能都可以使您的命令行旅途愉快。
在本文中,让我们回顾一下强大的ls命令的15个实际示例。
1.使用ls -t打开最后编辑的文件
To open the last edited file in the current 目录 use the combination of ls, head and vi commands as shown below.
ls -t 按修改时间对文件排序,首先显示最后编辑的文件。 头-1 拿起第一个文件。
$ vi first-long-file.txt $ vi second-long-file.txt $ vi `ls -t | 头-1` [Note: This will open the last file you edited (i.e second-long-file.txt)]
2.使用ls -1每行显示一个文件
要显示每行单个条目,请使用-1选项,如下所示。
$ ls-1 bin boot cdrom dev etc home initrd initrd.img lib
3.使用ls -l显示有关文件/目录的所有信息
显示有关文件/目录的详细列表信息。
$ ls-l -rw-r----- 1 拉梅什 团队开发 9275204 6月13日15:27 mthesaur.txt.gz
- 第一个角色– File Type: 第一个字符指定文件的类型。
在上面的示例中,第一个字符中的连字符(-)表示这是正常文件。以下是ls -l输出的第一个字符中可能的文件类型选项。- 栏位说明
- – normal file
- d 目录
- 套接字文件
- l链接文件
- 领域1– File Permissions: 接下来的9个字符指定文件许可权。每3个字符表示对用户,组和世界的读取,写入,执行权限。在此示例中,-rw-r—–表示对用户具有读写许可权,对组具有读许可权,对其他用户无许可权。
- 领域2– Number of links: 第二个字段指定该文件的链接数。在此示例中,1表示仅指向该文件的一个链接。
- 领域3– 所有者: 第三个字段指定文件的所有者。在此示例中,此文件归用户名所有‘ramesh’.
- 领域4– Group: 第四个字段指定文件的组。在此示例中,此文件属于”team-dev’ group.
- 领域5 – Size: Fifth field specifies 的大小file. In this example, ‘9275204’指示文件大小。
- 领域6– Last modified date & time: 第六字段指定文件的最后修改日期和时间。在这个例子中‘Jun 13 15:27’指定文件的最后修改时间。
- 领域7– File name: 最后一个字段是文件的名称。在此的示例文件名是mthesaur.txt.gz。
4.使用ls -lh以人类可读的格式显示文件大小
使用 ls -lh (h代表人类可读形式),以易于阅读的格式显示文件大小。即M代表MB,K代表KB,G代表GB。
$ ls-l
-rw-r----- 1 拉梅什 团队开发 9275204 Jun 12 15:27 arch-linux.txt.gz*
$ ls -lh
-rw-r----- 1 拉梅什 团队开发 8.9M Jun 12 15:27 arch-linux.txt.gz
5.使用ls -ld显示目录信息
使用时“ls -l”您将获得目录内容的详细信息。但是,如果需要目录的详细信息,则可以使用-d选项,例如,如果使用ls -l / etc,则将显示etc目录下的所有文件。但是,如果要显示有关/ etc /目录的信息,请使用-ld选项,如下所示。
$ ls-l /etc total 3344 -rw-r--r-- 1 root root 15276 Oct 5 2004 a2ps.cfg -rw-r--r-- 1 root root 2562 Oct 5 2004 a2ps-site.cfg drwxr-xr-x 4 root root 4096 Feb 2 2007 acpi -rw-r--r-- 1 root root 48 Feb 8 2008年 adjtime drwxr-xr-x 4 root root 4096 Feb 2 2007 alchemist $ ls -ld /etc drwxr-xr-x 21 root root 4096 Jun 15 07:02 /etc
6.使用ls -lt根据上次修改时间对文件进行排序
要按最后修改时间的顺序对显示的文件名进行排序,请使用-t选项。与-l选项结合使用时,您会发现它很方便。
$ ls -lt total 76 drwxrwxrwt 14 root root 4096 Jun 22 07:36 tmp drwxr-xr-x 121 root root 4096 Jun 22 07:05 etc drwxr-xr-x 13 root root 13780 Jun 22 07:04 dev drwxr-xr-x 13 root root 4096 Jun 20 23:12 root drwxr-xr-x 12 root root 4096 Jun 18 08:31 home drwxr-xr-x 2 root root 4096 May 17 21:21 sbin lrwxrwxrwx 1 root root 11 May 17 20:29 cdrom -> media/cdrom drwx------ 2 root root 16384 May 17 20:29 lost+found drwxr-xr-x 15 root root 4096 Jul 2 2008年 var
7.使用ls -ltr根据上次修改时间(反向)对文件排序
以相反的顺序对上次修改时间中的文件名进行排序。这将在最后一行显示最后编辑的文件,当列表超出页面时将非常方便。这是我的默认ls用法。每当我执行ls时,我总是使用ls -ltr,因为我发现这非常方便。
$ ls -ltr total 76 drwxr-xr-x 15 root root 4096 Jul 2 2008年 var drwx------ 2 root root 16384 May 17 20:29 lost+found lrwxrwxrwx 1 root root 11 May 17 20:29 cdrom -> media/cdrom drwxr-xr-x 2 root root 4096 May 17 21:21 sbin drwxr-xr-x 12 root root 4096 Jun 18 08:31 home drwxr-xr-x 13 root root 4096 Jun 20 23:12 root drwxr-xr-x 13 root root 13780 Jun 22 07:04 dev drwxr-xr-x 121 root root 4096 Jun 22 07:05 etc drwxrwxrwt 14 root root 4096 Jun 22 07:36 tmp
8.使用ls -a(或)ls -A显示隐藏文件
To show all the hidden files in the 目录, use ‘-a option’。 Unix中的隐藏文件开始于‘.’ in its file name.
$ ls -a [[email protected] ~]$ ls -a . Debian-Info.txt .. CentOS-Info.txt .bash_history Fedora-Info.txt .bash_logout .lftp .bash_profile libiconv-1.11.tar.tar .bashrc libssh2-0.12-1.2.el4.rf.i386.rpm
它将显示所有文件,包括‘.’(当前目录)和‘..’(父目录)。显示隐藏文件,但不显示‘.’(当前目录)和‘..’(父目录),使用选项-A。
$ ls -A Debian-Info.txt Fedora-Info.txt CentOS-Info.txt Red-Hat-Info.txt .bash_history SUSE-Info.txt .bash_logout .lftp .bash_profile libiconv-1.11.tar.tar .bashrc libssh2-0.12-1.2.el4.rf.i386.rpm [Note: . and .. are not displayed 这里]
9.使用ls -R递归显示文件
$ ls/etc/sysconfig/networking devices profiles $ ls -R /etc/sysconfig/networking /etc/sysconfig/networking: devices profiles /etc/sysconfig/networking/devices: /etc/sysconfig/networking/profiles: default /etc/sysconfig/networking/profiles/default:
要递归显示所有文件,请使用-R选项。从/执行此操作时,它将以递归方式显示整个文件系统中所有未隐藏的文件。
10.使用ls -i显示文件索引节点号
有时您可能想知道文件的inone编号以进行内部维护。如下所示,使用-i选项显示一个数字。使用索引节点号,您可以删除其中包含特殊字符的文件’的名称,如 find命令的示例#6 文章。
$ ls-i /etc/xinetd.d/ 279694 chargen 279724 cups-lpd 279697 daytime-udp 279695 chargen-udp 279696 daytime 279698 echo
11.使用ls -q隐藏控制字符
要打印问号而不是非图形控制字符,请使用-q选项。
ls -q
12.使用ls -n显示文件UID和GID
列出类似于-l的输出,但以数字格式而不是名称显示uid和gid。
$ ls-l ~/.bash_profile -rw-r--r-- 1 拉梅什 拉梅什 909 Feb 8 11:48 /home/ramesh/.bash_profile $ ls -n ~/.bash_profile -rw-r--r-- 1 511 511 909 Feb 8 11:48 /home/ramesh/.bash_profile [Note: This display 511 for uid and 511 for gid]
13.使用ls -F对具有特殊字符的文件进行视觉分类
而不是做‘ls -l’然后检查第一个字符以确定文件的类型。您可以使用-F对不同类型的文件使用不同的特殊字符对文件进行分类。
$ ls-F Desktop/ Documents/ [email protected] firstfile Music/ Public/ Templates/
因此,在上面的输出中,
- / – 目录.
- 没有– normal file.
- @ – link file.
- * – Executable file
14.文件的视觉分类 色彩 使用ls -F
通过文件显示的颜色识别文件类型是文件分类中的另一种类型。在上面的输出目录中,蓝色显示为蓝色,软链接以绿色显示,而普通文件以默认颜色显示。
$ ls--color=auto Desktop Documents Examples firstfile Music Pictures Public Templates Videos
15.有用的ls命令别名
您可以在上面采用一些必需的ls选项,并将其作为别名。我们建议以下内容。
- 以易于理解的形式长列出文件的大小。
别名ll ="ls -lh"
- 通过附加特殊字符对文件类型进行分类。
alias lv="ls -F"
- 通过颜色和特殊字符对文件类型进行分类。
alias ls="ls -F --color=auto"
很棒的Linux文章
以下是一些很棒的 15个例子 您可能会觉得有帮助的文章。
- 掌握Grep! – 15个实用的Grep命令示例
- Linux Crontab:15个很棒的Cron作业示例
- 妈妈,我找到了! — 15个实用的Linux Find命令示例
- 15个掌握Linux命令行历史记录的示例
如果您喜欢这篇文章,您可能还会喜欢..
![]() |
![]() |
![]() |
![]() |
我从未想过我’d我比使用The Geek Stuff更重视电子邮件订阅。老实说,我只是为了那本书–but I’m finding that I’我从这些条目中学到了很多东西。