Lệnh ls là một trong những lệnh được sử dụng thường xuyên nhất trong Linux. Tôi tin rằng lệnh 15 Lệnh “ls” cơ bản dưới đây là các lệnh đầu tiên bạn có thể sử dụng khi bạn nhận được vào dấu nhắc lệnh của Linux Box.
Chúng tôi sử dụng lệnh ls hàng ngày và thường xuyên mặc dù chúng tôi có thể không nhận thức và không bao giờ sử dụng tất cả các tùy chọn ls có sẵn. Trong bài này, chúng ta sẽ thảo luận về lệnh ls cơ bản mà chúng ta đã cố gắng bao quát càng nhiều thông số càng tốt.
1. Liệt kê các tập tin sử dụng lệnh ls mà không có tùy chọn
Ls không có tệp danh sách tùy chọn và thư mục ở dạng trống, nơi chúng tôi sẽ không thể xem các chi tiết như loại tệp, kích thước, ngày và thời gian đã sửa đổi,sự cho phép (permission) và liên kết vv
# ls0001.pcap Desktop Downloads index.html install.log.syslog Pictures Templatesanaconda-ks.cfg Documents fbcmd_update.php install.log Music Public Videos
2 Danh sách các tập tin với tùy chọn -l
Ở đây, ls -l (-l là ký tự không phải là một) hiển thị tệp hoặc thư mục, kích thước, ngày, thời gian đã sửa đổi, tên tệp hoặc tên thư mục và chủ sở hữu (owner) tệp file và đó là sự cho phép (permission).
# ls -ltotal 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.cfgdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktopdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documentsdrwxr-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.syslogdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Musicdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Picturesdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Publicdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templatesdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
3. Xem tập tin ẩn
Liệt kê tất cả các tệp tin kể cả tệp ẩn bắt đầu bằng ‘.’.
# ls -a. .bashrc Documents .gconfd install.log .nautilus .pulse-cookie.. .cache Downloads .gnome2 install.log.syslog .netstat.swp .recently-used.xbel0001.pcap .config .elinks .gnome2_private .kde .opera .spice-vdagentanaconda-ks.cfg .cshrc .esd_auth .gtk-bookmarks .libreoffice Pictures .tcshrc.bash_history .dbus .fbcmd .gvfs .local .pki Templates.bash_logout Desktop fbcmd_update.php .ICEauthority .mozilla Public Videos.bash_profile .digrc .gconf index.html Music .pulse .wireshark
4. Liệt kê các tệp với định dạng có thể đọc được của con người với tùy chọn -lh
Với sự kết hợp của tùy chọn -lh, hiển thị kích thước ở định dạng có thể đọc được của con người.
# ls -lhtotal 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.cfgdrwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktopdrwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Documentsdrwxr-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.syslogdrwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Musicdrwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Picturesdrwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Publicdrwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Templatesdrwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Videos
5. Liệt kê các Tệp và Thư Mục với Ký tự ‘/’ ở cuối
Sử dụng tùy chọn -F với lệnh ls, sẽ thêm ký tự ‘/’ ở cuối mỗi thư mục.
# ls -F0001.pcap Desktop/ Downloads/ index.html install.log.syslog Pictures/ Templates/anaconda-ks.cfg Documents/ fbcmd_update.php install.log Music/ Public/ Videos/
6. Liệt kê các tập tin theo thứ tự ngược
Lệnh sau với ls -r hiển thị tập tin và thư mục theo thứ tự ngược lại.
# ls -rVideos Public Music install.log fbcmd_update.php Documents anaconda-ks.cfgTemplates Pictures install.log.syslog index.html Downloads Desktop 0001.pcap
7. Danh sách đệ quy danh mục con
Ls -R sẽ liệt kê các cây thư mục liệt kê rất dài. Xem một ví dụ của đầu ra của lệnh.
# ls -Rtotal 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. Lệnh xuất hàng ngược
Với sự kết hợp của -ltr sẽ cho thấy tập tin sửa đổi mới nhất hoặc ngày thư mục như trước.
# ls -ltrtotal 176-rw-r--r--. 1 root root 11439 Jul 31 02:13 install.log.syslog-rw-r--r--. 1 root root 48867 Jul 31 02:17 install.log-rw-------. 1 root root 1586 Jul 31 02:17 anaconda-ks.cfgdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktopdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videosdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templatesdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Publicdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Picturesdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Musicdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents-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.phpdrwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads-rw-r--r--. 1 root root 683 Aug 19 09:59 0001.pcap
9. Sắp xếp tệp theo Kích thước tệp
Với kết hợp của -lS hiển thị kích thước tập tin theo thứ tự, sẽ hiển thị kích thước lớn đầu tiên.
# ls -lStotal 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.syslogdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktopdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documentsdrwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloadsdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Musicdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Picturesdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Publicdrwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templatesdrwxr-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. Hiển thị số Inode của File hoặc Directory
Chúng ta có thể thấy một số số được in trước tên file / thư mục. Với -i là tùy chọn danh sách tập tin / thư mục với số inode.
# ls -i20112 0001.pcap 23610 Documents 23793 index.html 23611 Music 23597 Templates23564 anaconda-ks.cfg 23595 Downloads 22 install.log 23612 Pictures 23613 Videos23594 Desktop 23585 fbcmd_update.php 35 install.log.syslog 23601 Public
11. Hiện phiên bản lệnh ls
Kiểm tra phiên bản lệnh ls.
# ls --versionls (GNU coreutils) 8.4Copyright (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. Hiển thị trang trợ giúp
Liệt kê trang trợ giúp của lệnh ls với tùy chọn của chúng.
# ls --helpUsage: ls [OPTION]... [FILE]...
13. Liệt kê Thông tin thư mục
Với lệnh ls-l danh sách các tập tin theo thư mục / tmp. Trong đó với -ld là thông số hiển thị thông tin của thư mục / tmp.
# ls -l /tmptotal 408drwx------. 2 narad narad 4096 Aug 2 02:00 CRX_75DAF8CB7768-r--------. 1 root root 384683 Aug 4 12:28 htop-1.0.1.tar.gzdrwx------. 2 root root 4096 Aug 4 11:20 keyring-6Mfjnkdrwx------. 2 root root 4096 Aug 16 01:33 keyring-pioZJrdrwx------. 2 gdm gdm 4096 Aug 21 11:26 orbit-gdmdrwx------. 2 root root 4096 Aug 19 08:41 pulse-gl6o4ZdxQVrXdrwx------. 2 narad narad 4096 Aug 4 08:16 pulse-UDH76ExwUVoUdrwx------. 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. Hiển thị UID và GID của Tệp
Để hiển thị UID và GID của tệp và thư mục. Sử dụng tùy chọn -n với lệnh
ls# ls -ntotal 36drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Downloadsdrwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Musicdrwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Pictures-rw-rw-r--. 1 500 500 12 Aug 21 13:06 tmp.txtdrwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Videos
15. Lệnh ls và nó là bí danh
Chúng tôi đã thực hiện alias cho lệnh ls, khi chúng tôi thực hiện lệnh ls nó sẽ mất -l tùy chọn theo mặc định và hiển thị danh sách dài như đã đề cập trước đó.
# alias ls="ls -l"
Lưu ý: Chúng ta có thể thấy số bí danh có sẵn trong hệ thống của bạn với lệnh bí danh dưới đây và cũng có thể là unalias như ví dụ thể hiện dưới đây .
# aliasalias 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'
Để loại bỏ một bí danh đã được xác định trước đó, chỉ cần sử dụng lệnh unalias.
# unalias ls
Link bài gốc: Tecmint