歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux技術 >> 每天一個Linux命令(1)ls命令

每天一個Linux命令(1)ls命令

日期:2017/3/3 14:11:19   编辑:Linux技術

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 [email protected] [email protected] [email protected] fi.mo hu.mo ja.mo pa.mo ru.mo tr.mo zh_CN.mo

ca.mo [email protected] [email protected] en.mo eo.mo fr.mo id.mo lt.mo pl.mo sl.mo uk.mo zh_TW.mo

da.mo de.mo [email protected] [email protected] 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      [email protected]    [email protected]    [email protected]  fi.mo  hu.mo  ja.mo  pa.mo     ru.mo  tr.mo  zh_CN.mo
ca.mo   [email protected]  [email protected]  en.mo           eo.mo       fr.mo  id.mo  lt.mo  pl.mo     sl.mo  uk.mo  zh_TW.mo
da.mo   de.mo         [email protected]     [email protected]  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

Copyright © Linux教程網 All Rights Reserved