ls是list的缩写,ls命令是Linux系统下最常用的命令之一。
ls命令用于打印当前目录的清单,如果指定其它目录,那么就会显示其他目录的文件及文件夹的清单。 通过ls 命令还可以查看文件其它的详细信息。
ls命令的输出信息可以进行彩色加亮显示,以分区不同类型的文件。
1.用法:
ls [选项] [目录名]
2.功能:
列出目标目录中所有的子目录和文件。
3.常用选项参数
1) -a 列出目录下的所有文件,包括以 . 开头的隐含文件。
2) -A 列出除. (当前目录)及..(当前目录的父目录)以外的任何项目。
3) -C 多列显示输出结果。这是默认选项。
4) -l 与"-C"选项功能相反,所有输出信息用单列格式输出,每行只列出一个文件,不输出为多列。-l 除了文件名之外,还将文件的权限、所有者、文件大小等信息详细列出来。
5) -R(–recursive) 同时列出所有子目录层。
6) -F 在每个输出项后追加文件的类型标识符.
具体含义:“*”表示具有可执行权限的普通文件,“/”表示目录,“@”表示符号链接,“|”表示命令管道FIFO,“=”表示sockets套接字。当文件为普通文件时,不输出任何标识符。
7) -m 所有项目以逗号分隔,并填满整行行宽
8) -i 显示文件索引节点号(inode),一个索引节点代表一个文件。
9) -t 以文件修改时间排序
10) -d(–directory) 将目录像文件一样显示,而不是显示其下的文件。
11) -n 打印文件的UID和ID
12) -h 以可以理解的方式显示出文件的大小
4.常用实例:
(1)[root@localhost boot]# ls -l 等价于[root@localhost boot]# ll 以单行单个的形式列出/boot目录下的文件的所有信息
输出结果: 总用量 89404 -rw-r--r--. 1 root root 123838 3月 6 2015 config-3.10.0-229.el7.x86_64 drwxr-xr-x. 2 root root 26 3月 21 02:27 grub drwxr-xr-x. 6 root root 104 3月 21 02:43 grub2 -rw-r--r--. 1 root root 43536327 3月 21 02:41 initramfs-0-rescue-35667f30fcac420f933d23d8835c4cf3.img -rw-r--r--. 1 root root 16844928 3月 21 02:43 initramfs-3.10.0-229.el7.x86_64.img -rw-r--r--. 1 root root 16984357 3月 21 05:15 initramfs-3.10.0-229.el7.x86_64kdump.img -rw-r--r--. 1 root root 867402 3月 21 02:39 initrd-plymouth.img -rw-r--r--. 1 root root 240039 3月 6 2015 symvers-3.10.0-229.el7.x86_64.gz -rw-------. 1 root root 2881257 3月 6 2015 System.map-3.10.0-229.el7.x86_64 -rwxr-xr-x. 1 root root 5029136 3月 21 02:41 vmlinuz-0-rescue-35667f30fcac420f933d23d8835c4cf3 -rwxr-xr-x. 1 root root 5029136 3月 6 2015 vmlinuz-3.10.0-229.el7.x86_64
(2)[root@localhost boot]# ls C 等价于[root@localhost boot]# ls 列出当前目录下的所有文件,每行填充满,再到下一行,由于需要控制显示格式,这里自动每行列出2个文件
config-3.10.0-229.el7.x86_64 initrd-plymouth.img grub symvers-3.10.0-229.el7.x86_64.gz grub2 System.map-3.10.0-229.el7.x86_64 initramfs-0-rescue-35667f30fcac420f933d23d8835c4cf3.img vmlinuz-0-rescue-35667f30fcac420f933d23d8835c4cf3 initramfs-3.10.0-229.el7.x86_64.img vmlinuz-3.10.0-229.el7.x86_64 initramfs-3.10.0-229.el7.x86_64kdump.img
(3)[root@localhost boot]# ls -R 递归地显示出当前工作目录下所有的文件信息
.: config-3.10.0-229.el7.x86_64 initrd-plymouth.img grub symvers-3.10.0-229.el7.x86_64.gz grub2 System.map-3.10.0-229.el7.x86_64 initramfs-0-rescue-35667f30fcac420f933d23d8835c4cf3.img vmlinuz-0-rescue-35667f30fcac420f933d23d8835c4cf3 initramfs-3.10.0-229.el7.x86_64.img vmlinuz-3.10.0-229.el7.x86_64 initramfs-3.10.0-229.el7.x86_64kdump.img ./grub: splash.xpm.gz ./grub2: device.map fonts grub.cfg grubenv i386-pc locale themes ./grub2/fonts: unicode.pf2 ./grub2/i386-pc: acpi.mod date.mod gcry_twofish.mod mdraid09_be.mod password.mod test_blockarg.mod adler32.mod datetime.mod gcry_whirlpool.mod mdraid09.mod password_pbkdf2.mod testload.mod affs.mod diskfilter.mod gdb.mod mdraid1x.mod pata.mod test.mod afs.mod disk.mod geli.mod memdisk.mod pbkdf2.mod testspeed.mod ahci.mod div_test.mod gettext.mod memrw.mod pbkdf2_test.mod tftp.mod all_video.mod dm_nv.mod gfxmenu.mod minicmd.mod pcidump.mod tga.mod aout.mod drivemap.mod gfxterm_background.mod minix2_be.mod pci.mod time.mod archelp.mod echo.mod gfxterm_menu.mod minix2.mod plan9.mod trig.mod ata.mod efiemu32.o gfxterm.mod minix3_be.mod play.mod tr.mod at_keyboard.mod efiemu64.o gptsync.mod minix3.mod png.mod truecrypt.mod backtrace.mod efiemu.mod gzio.mod minix_be.mod priority_queue.mod true.mod bfs.mod ehci.mod halt.mod minix.mod probe.mod udf.mod biosdisk.mod elf.mod hashsum.mod mmap.mod procfs.mod ufs1_be.mod bitmap.mod eval.mod hdparm.mod moddep.lst progress.mod ufs1.mod bitmap_scale.mod exfat.mod hello.mod modinfo.sh pxechain.mod ufs2.mod blocklist.mod exfctest.mod help.mod morse.mod pxe.mod uhci.mod blscfg.mod ext2.mod hexdump.mod mpi.mod raid5rec.mod usb_keyboard.mod boot.img extcmd.mod hfs.mod msdospart.mod raid6rec.mod usb.mod boot.mod fat.mod hfspluscomp.mod multiboot2.mod read.mod usbms.mod bsd.mod file.mod hfsplus.mod multiboot.mod reboot.mod usbserial_common.mod btrfs.mod font.mod http.mod nativedisk.mod regexp.mod usbserial_ftdi.mod bufio.mod freedos.mod iorw.mod net.mod reiserfs.mod usbserial_pl2303.mod cat.mod fshelp.mod iso9660.mod newc.mod relocator.mod usbserial_usbdebug.mod cbfs.mod fs.lst jfs.mod nilfs2.mod romfs.mod usbtest.mod cbls.mod functional_test.mod jpeg.mod normal.mod scsi.mod vbe.mod cbmemc.mod gcry_arcfour.mod keylayouts.mod ntfscomp.mod search_fs_file.mod verify.mod cbtable.mod gcry_blowfish.mod keystatus.mod ntfs.mod search_fs_uuid.mod vga.mod cbtime.mod gcry_camellia.mod ldm.mod ntldr.mod search_label.mod vga_text.mod chain.mod gcry_cast5.mod legacycfg.mod odc.mod search.mod video_bochs.mod cmdline_cat_test.mod gcry_crc.mod legacy_password_test.mod offsetio.mod sendkey.mod video_cirrus.mod cmosdump.mod gcry_des.mod linux16.mod ohci.mod serial.mod video_colors.mod cmostest.mod gcry_dsa.mod linux.mod part_acorn.mod setjmp.mod video_fb.mod cmp.mod gcry_idea.mod loadenv.mod part_amiga.mod setjmp_test.mod videoinfo.mod command.lst gcry_md4.mod loopback.mod part_apple.mod setpci.mod video.lst configfile.mod gcry_md5.mod lsacpi.mod part_bsd.mod sfs.mod video.mod core.img gcry_rfc2268.mod lsapm.mod part_dfly.mod signature_test.mod videotest_checksum.mod cpio_be.mod gcry_rijndael.mod lsmmap.mod part_dvh.mod sleep.mod videotest.mod cpio.mod gcry_rmd160.mod ls.mod part_gpt.mod sleep_test.mod xfs.mod cpuid.mod gcry_rsa.mod lspci.mod partmap.lst spkmodem.mod xnu.mod crc64.mod gcry_seed.mod luks.mod part_msdos.mod squash4.mod xnu_uuid.mod cryptodisk.mod gcry_serpent.mod lvm.mod part_plan.mod syslinuxcfg.mod xnu_uuid_test.mod crypto.lst gcry_sha1.mod lzopio.mod part_sun.mod tar.mod xzio.mod crypto.mod gcry_sha256.mod macbless.mod part_sunpc.mod terminal.lst zfscrypt.mod cs5536.mod gcry_sha512.mod macho.mod parttool.lst terminal.mod zfsinfo.mod datehook.mod gcry_tiger.mod mda_text.mod parttool.mod terminfo.mod zfs.mod ./grub2/locale: ast.mo de_CH.mo en@arabic.mo en@hebrew.mo en@quot.mo fi.mo hu.mo ja.mo pa.mo ru.mo tr.mo zh_CN.mo ca.mo de@hebrew.mo en@cyrillic.mo en.mo eo.mo fr.mo id.mo lt.mo pl.mo sl.mo uk.mo zh_TW.mo da.mo de.mo en@greek.mo en@piglatin.mo es.mo gl.mo it.mo nl.mo pt_BR.mo sv.mo vi.mo ./grub2/themes: system ./grub2/themes/system:
(4)[root@localhost /]# ls -R boot/grub2 递归的显示出指定文件目录的下的所有文件
boot/grub2: device.map fonts grub.cfg grubenv i386-pc locale themes boot/grub2/fonts: unicode.pf2 boot/grub2/i386-pc: acpi.mod date.mod gcry_twofish.mod mdraid09_be.mod password.mod test_blockarg.mod adler32.mod datetime.mod gcry_whirlpool.mod mdraid09.mod password_pbkdf2.mod testload.mod affs.mod diskfilter.mod gdb.mod mdraid1x.mod pata.mod test.mod afs.mod disk.mod geli.mod memdisk.mod pbkdf2.mod testspeed.mod ahci.mod div_test.mod gettext.mod memrw.mod pbkdf2_test.mod tftp.mod all_video.mod dm_nv.mod gfxmenu.mod minicmd.mod pcidump.mod tga.mod aout.mod drivemap.mod gfxterm_background.mod minix2_be.mod pci.mod time.mod archelp.mod echo.mod gfxterm_menu.mod minix2.mod plan9.mod trig.mod ata.mod efiemu32.o gfxterm.mod minix3_be.mod play.mod tr.mod at_keyboard.mod efiemu64.o gptsync.mod minix3.mod png.mod truecrypt.mod backtrace.mod efiemu.mod gzio.mod minix_be.mod priority_queue.mod true.mod bfs.mod ehci.mod halt.mod minix.mod probe.mod udf.mod biosdisk.mod elf.mod hashsum.mod mmap.mod procfs.mod ufs1_be.mod bitmap.mod eval.mod hdparm.mod moddep.lst progress.mod ufs1.mod bitmap_scale.mod exfat.mod hello.mod modinfo.sh pxechain.mod ufs2.mod blocklist.mod exfctest.mod help.mod morse.mod pxe.mod uhci.mod blscfg.mod ext2.mod hexdump.mod mpi.mod raid5rec.mod usb_keyboard.mod boot.img extcmd.mod hfs.mod msdospart.mod raid6rec.mod usb.mod boot.mod fat.mod hfspluscomp.mod multiboot2.mod read.mod usbms.mod bsd.mod file.mod hfsplus.mod multiboot.mod reboot.mod usbserial_common.mod btrfs.mod font.mod http.mod nativedisk.mod regexp.mod usbserial_ftdi.mod bufio.mod freedos.mod iorw.mod net.mod reiserfs.mod usbserial_pl2303.mod cat.mod fshelp.mod iso9660.mod newc.mod relocator.mod usbserial_usbdebug.mod cbfs.mod fs.lst jfs.mod nilfs2.mod romfs.mod usbtest.mod cbls.mod functional_test.mod jpeg.mod normal.mod scsi.mod vbe.mod cbmemc.mod gcry_arcfour.mod keylayouts.mod ntfscomp.mod search_fs_file.mod verify.mod cbtable.mod gcry_blowfish.mod keystatus.mod ntfs.mod search_fs_uuid.mod vga.mod cbtime.mod gcry_camellia.mod ldm.mod ntldr.mod search_label.mod vga_text.mod chain.mod gcry_cast5.mod legacycfg.mod odc.mod search.mod video_bochs.mod cmdline_cat_test.mod gcry_crc.mod legacy_password_test.mod offsetio.mod sendkey.mod video_cirrus.mod cmosdump.mod gcry_des.mod linux16.mod ohci.mod serial.mod video_colors.mod cmostest.mod gcry_dsa.mod linux.mod part_acorn.mod setjmp.mod video_fb.mod cmp.mod gcry_idea.mod loadenv.mod part_amiga.mod setjmp_test.mod videoinfo.mod command.lst gcry_md4.mod loopback.mod part_apple.mod setpci.mod video.lst configfile.mod gcry_md5.mod lsacpi.mod part_bsd.mod sfs.mod video.mod core.img gcry_rfc2268.mod lsapm.mod part_dfly.mod signature_test.mod videotest_checksum.mod cpio_be.mod gcry_rijndael.mod lsmmap.mod part_dvh.mod sleep.mod videotest.mod cpio.mod gcry_rmd160.mod ls.mod part_gpt.mod sleep_test.mod xfs.mod cpuid.mod gcry_rsa.mod lspci.mod partmap.lst spkmodem.mod xnu.mod crc64.mod gcry_seed.mod luks.mod part_msdos.mod squash4.mod xnu_uuid.mod cryptodisk.mod gcry_serpent.mod lvm.mod part_plan.mod syslinuxcfg.mod xnu_uuid_test.mod crypto.lst gcry_sha1.mod lzopio.mod part_sun.mod tar.mod xzio.mod crypto.mod gcry_sha256.mod macbless.mod part_sunpc.mod terminal.lst zfscrypt.mod cs5536.mod gcry_sha512.mod macho.mod parttool.lst terminal.mod zfsinfo.mod datehook.mod gcry_tiger.mod mda_text.mod parttool.mod terminfo.mod zfs.mod boot/grub2/locale: ast.mo de_CH.mo en@arabic.mo en@hebrew.mo en@quot.mo fi.mo hu.mo ja.mo pa.mo ru.mo tr.mo zh_CN.mo ca.mo de@hebrew.mo en@cyrillic.mo en.mo eo.mo fr.mo id.mo lt.mo pl.mo sl.mo uk.mo zh_TW.mo da.mo de.mo en@greek.mo en@piglatin.mo es.mo gl.mo it.mo nl.mo pt_BR.mo sv.mo vi.mo boot/grub2/themes: system boot/grub2/themes/system:
(5)[root@localhost sunjimeng]# ls -n 打印文件的UID(用户ID user ID)和GID (组ID,group ID)
总用量 4588 -rw-------. 1 1000 1000 4694016 4月 14 21:54 core.3246 drwxr-xr-x. 2 0 0 4096 4月 26 03:38 empty drwxr-xr-x. 2 0 0 32 4月 22 22:29 text drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 公共 drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 模板 drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 视频 drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 图片 drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 文档 drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 下载 drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 音乐 drwxr-xr-x. 2 1000 1000 6 4月 19 03:30 桌面
(6)[root@localhost sunjimeng]# ls -m 水平列出文件夹中文件的内容,与ls命令的区别是,加上了逗号区别各个子项
core.3246, empty, text, 公共, 模板, 视频, 图片, 文档, 下载, 音乐, 桌面
(7)[root@localhost sunjimeng]# ls -tl 按修改时间排序输出文件的详细信息
总用量 4588 drwxr-xr-x. 2 root root 4096 4月 26 03:38 empty drwxr-xr-x. 2 root root 32 4月 22 22:29 text drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 公共 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 模板 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 视频 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 图片 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 文档 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 下载 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 音乐 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 桌面 -rw-------. 1 sunjimeng sunjimeng 4694016 4月 14 21:54 core.3246
很明显,(7)的结果与(6)有明显不同。
(8)[root@localhost sunjimeng]# ls -ihl 详细地输出文件的索引号(-i)和文件的大小(-h),索引号即每个文件的inode号
总用量 4.5M 2843373 -rw-------. 1 sunjimeng sunjimeng 4.5M 4月 14 21:54 core.3246 272893290 drwxr-xr-x. 2 root root 4.0K 4月 26 03:38 empty 406101499 drwxr-xr-x. 2 root root 32 4月 22 22:29 text 2504431 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 公共 406095911 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 模板 2504433 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 视频 406095912 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 图片 136673997 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 文档 270959540 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 下载 270959541 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 音乐 136673996 drwxr-xr-x. 2 sunjimeng sunjimeng 6 4月 19 03:30 桌面
(9)[root@localhost sunjimeng]# ls -dl 只显示当前目录,而不显示当前目录下的文件,把当前目录当作文件一样显示
drwx------. 16 sunjimeng sunjimeng 4096 4月 29 00:36 .
(10)[root@localhost sunjimeng]# ls -F /boot 用标志类型的方式显示/boot文件夹下的各个类型的子项
config-3.10.0-229.el7.x86_64 grub/ grub2/ initramfs-0-rescue-35667f30fcac420f933d23d8835c4cf3.img initramfs-3.10.0-229.el7.x86_64.img initramfs-3.10.0-229.el7.x86_64kdump.img initrd-plymouth.img symvers-3.10.0-229.el7.x86_64.gz System.map-3.10.0-229.el7.x86_64 vmlinuz-0-rescue-35667f30fcac420f933d23d8835c4cf3* vmlinuz-3.10.0-229.el7.x86_64*
(11)[root@localhost sunjimeng]# ls -a与[root@localhost sunjimeng]# ls -A的比较
[root@localhost sunjimeng]# ls -a . .bash_profile .core.3246.swp .local 公共 下载 .. .bashrc empty .mozilla 模板 音乐 .1.log.swp .cache .esd_auth .serverauth.3770 视频 桌面 .bash_history .config .hellow.java.swp text 图片 .bash_logout core.3246 .ICEauthority .viminfo 文档 [root@localhost sunjimeng]# ls -A .1.log.swp .cache .esd_auth .serverauth.3770 视频 桌面 .bash_history .config .hellow.java.swp text 图片 .bash_logout core.3246 .ICEauthority .viminfo 文档 .bash_profile .core.3246.swp .local 公共 下载 .bashrc empty .mozilla 模板 音乐
ls -a命令产生28个子项,而ls -A只产生26个子项。相比较少了 ”.“和”..“。
(12)[root@localhost sunjimeng]# ls -F /boot |grep /$ 只显示当前文件夹下的子目录而不显示文件
grub/ grub2/
(13)[root@localhost sunjimeng]# ls -lF /boot |grep /$与[root@localhost sunjimeng]# ls -lF /boot |grep "^d"
[root@localhost sunjimeng]# ls -Fl /boot |grep /$ drwxr-xr-x. 2 root root 26 3月 21 02:27 grub/ drwxr-xr-x. 6 root root 104 3月 21 02:43 grub2/ [root@localhost sunjimeng]# ls -Fl /boot |grep "^d" drwxr-xr-x. 2 root root 26 3月 21 02:27 grub/ drwxr-xr-x. 6 root root 104 3月 21 02:43 grub2/
除了颜色之外,没有什么差别。F有在目录名后加上红色/的功能
(14)[root@localhost sunjimeng]# ls -l /boot |grep "^d"与[root@localhost sunjimeng]# ls -l /boot |grep "^drw"
[root@localhost sunjimeng]# ls -l /boot |grep "^d" drwxr-xr-x. 2 root root 26 3月 21 02:27 grub drwxr-xr-x. 6 root root 104 3月 21 02:43 grub2 [root@localhost sunjimeng]# ls -l /boot |grep "^drw" drwxr-xr-x. 2 root root 26 3月 21 02:27 grub drwxr-xr-x. 6 root root 104 3月 21 02:43 grub2
(15)[root@localhost sunjimeng]# ls | sed "s:^:`pwd`/:" 列出文件的绝对路径
/home/sunjimeng/core.3246 /home/sunjimeng/empty /home/sunjimeng/text /home/sunjimeng/公共 /home/sunjimeng/模板 /home/sunjimeng/视频 /home/sunjimeng/图片 /home/sunjimeng/文档 /home/sunjimeng/下载 /home/sunjimeng/音乐 /home/sunjimeng/桌面
5.其他:
显示彩色目录列表
打开/etc/bashrc, 加入如下一行:
alias ls="ls --color"
下次启动bash时就可以像在Slackware里那样显示彩色的目录列表了, 其中颜色的含义如下:
1. 蓝色-->目录
2. 绿色-->可执行文件
3. 红色-->压缩文件
4. 浅蓝色-->链接文件
5. 灰色-->其他文件
更多信息:1.http://www.cnblogs.com/peida/archive/2012/10/23/2734829.html
2.http://linux.51yip.com/search/ls
3.http://man.linuxde.net/ls