上一篇:蛋疼设计之带ARM处理器的鼠标
下一篇:Firefox 6正式版抢鲜下载

Linux/Unix LS命令详解与实例

Linux或者Unix下最常用的命令莫过于ls了,可以说木有ls命令,*nix使用者将无法生存。不管你一天使用这个命令十次还是一百次,了解ls的多种用法将会是你的工作更加有趣。

这里搜集了ls命令的15中典型用法。

1、使用ls -t命令的组合形式来修改最后一次编辑的文件
ls -t是将文件按照修改时间排列起来,最近修改的在最上面,那么有下面这个命令来编辑最近编辑过的文件

$ vi first-file.txt
$ vi second-file.txt
$ vi `ls -t | head -1`

其中head -1(数字)是用来获取ls之后的第一个文件的,| 代表管道操作,这个命令会自动选择second-file.txt进行编辑

2、使用ls -1(数字)按行列出文件

$ ls -1
bin
boot
cdrom
dev
etc
home
lib

3、使用ls -l(小写字母)列出文件和文件夹的详细信息

$ ls -l
-rw-r—– 1 ramesh team-dev 9275204 Jun 13 15:27 methesaur.txt.gz

这段信息分别代表文件类型、文件权限设置、链接文件数、所有者、所有组、文件大小、最后修改时间和文件名

4、使用ls -lh用可被人读的文件大小列出文件和文件夹的详细信息
比较拗口,运行起来看一下,和ls -l比起来文件大小使用MB的方式显示了

$ ls -lh
-rw-r—– 1 ramesh team-dev 8.9M Jun 13 15:27 methesaur.txt.gz


5、使用ls -ld显示文件夹的信息
看看下面两个命令的区别就知道为什么要使用ls -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

ls -ld只会显示etc文件夹的信息,而不会进入etc文件夹列出所含文件的信息

6、使用ls -lt按时间列出文件和文件夹的详细信息
组合前面的ls -l(小写字母)和ls -t命令就可以了

$ 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 -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命令显示隐藏文件

$ 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
$ 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

主要区别就是ls -a会列出当前目录“.”和上级目录“..”,而ls -A不会

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:

10、使用ls -i显示文件的inode号码
每个文件都有唯一的inode号码,建立两个文件,两个名字相近,区别在于其中一个文件名最后有个空格,也许ls的时候你很难区别这两个文件

# touch “test-file-name”
# touch “test-file-name ”
# ls -1 test*
test-file-name
test-file-name
# ls -i1 test*
16187429 test-file-name
16187430 test-file-name 

11、使用ls -q隐藏控制字符
作用是将非图形字符打印为问号?

12、使用ls -n打印文件的UID和GID

$ ls -l ~/.bash_profile
-rw-r–r– 1 ramesh ramesh 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

13、使用ls -F按照特殊字符对文件进行分类

$ ls -F
Desktop/ Documents/ Ubuntu-App@ firstfile Music/ Public/ Templates/

文件名末尾带斜杠/的为文件夹,带@的为链接文件,带*的是可执行文件,什么都不带的是一般文件

14、使用ls –color列出文件并标记颜色分类

$ ls –color=auto
Desktop Documents Examples firstfile Music Pictures Public Templates Videos

15、恰当的使用alias简化命令

alias ll=”ls -lh”
alias lv=”ls -F”
alias ls=”ls -F –color=auto”

发表在 Linux ,Unix | 标签: , ,

[互联网周末 oBugs.net][第三眼] 获取文章短链接

读完这篇文章,你觉得内容怎么样?如果你喜欢,那就 吧!

相关文章/随机文章:

3条评论

  1. 一苇 评论到:
    Google Chrome 15.0.849.0 Google Chrome 15.0.849.0 Windows XP Windows XP

    三眼兄真是linux高手啊,一直打算系统的学一下,只是…只是这命令也太繁琐了…

  2. 弗洛缺德 评论到:
    Firefox 8.0a1 Firefox 8.0a1 GNU/Linux x64 GNU/Linux x64

    ls -1 | wc -l

  3. redswallow 评论到:
    Chromium 13.0.782.218 Chromium 13.0.782.218 Ubuntu 10.10 Ubuntu 10.10

    mark 一般只会ls -ail :eek:
    ls -R -n -h好像满有用^^

上一篇:蛋疼设计之带ARM处理器的鼠标
下一篇:Firefox 6正式版抢鲜下载

发表评论


使用组合键[ Ctrl + Enter ]亦可

CommentLuv badge
读完这篇文章,你觉得内容怎么样?如果你喜欢,那就