Sei sulla pagina 1di 6

15 Basic ls Command Examples in Linux

ls command is one of the most frequently used command in Linux. I believe ls command is the first command you may use when you get into the command prompt of Linux Box. We use ls command daily basis and frequently even though we may not aware and never use all the option available. In this article, well be discussing basic ls command where we have tried to cover as much parameters as possible. Linux ls Command 1. List Files using ls with no option ls with no option list files and directories in bare format where we wont be able to view details like file types, size, modified date and time, permission and links etc.
# ls 0001.pcap Desktop Pictures Templates anaconda-ks.cfg Documents Public Videos Downloads fbcmd_update.php index.html install.log install.log.syslog Music

2 List Files With option l Here, ls -l (-l is character not one) shows file or directory, size, modified date and time, file or folder name and owner of file and its permission.
# ls -l total 176 -rw-r--r--. -rw-------. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. -rw-r--r--. -rw-r--r--. -rw-r--r--. -rw-r--r--. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. 1 1 2 2 4 1 1 1 1 2 2 2 2 2 root root root root root root root root root root root root root root root root root root root root root root root root root root root root 683 1586 4096 4096 4096 21262 46701 48867 11439 4096 4096 4096 4096 4096 Aug Jul Jul Jul Aug Aug Jul Jul Jul Jul Jul Jul Jul Jul 19 31 31 31 16 12 31 31 31 31 31 31 31 31 09:59 02:17 02:48 02:48 02:55 12:42 09:58 02:17 02:13 02:48 02:48 02:48 02:48 02:48 0001.pcap anaconda-ks.cfg Desktop Documents Downloads fbcmd_update.php index.html install.log install.log.syslog Music Pictures Public Templates Videos

3. View Hidden Files List all files including hidden file starting with ..
# ls -a

. .bashrc Documents .gconfd .nautilus .pulse-cookie .. .cache Downloads .gnome2 install.log.syslog .netstat.swp .recently-used.xbel 0001.pcap .config .elinks .gnome2_private .opera .spice-vdagent anaconda-ks.cfg .cshrc .esd_auth .gtk-bookmarks Pictures .tcshrc .bash_history .dbus .fbcmd .gvfs .pki Templates .bash_logout Desktop fbcmd_update.php .ICEauthority Public Videos .bash_profile .digrc .gconf index.html .pulse .wireshark

install.log

.kde .libreoffice .local .mozilla Music

4. List Files with Human Readable Format with option -lh With combination of -lh option, shows sizes in human readable format.
# ls -lh total 176K -rw-r--r--. -rw-------. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. -rw-r--r--. -rw-r--r--. -rw-r--r--. -rw-r--r--. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. 1 1 2 2 4 1 1 1 1 2 2 2 2 2 root root root root root root root root root root root root root root root root root root root root root root root root root root root root 683 1.6K 4.0K 4.0K 4.0K 21K 46K 48K 12K 4.0K 4.0K 4.0K 4.0K 4.0K Aug Jul Jul Jul Aug Aug Jul Jul Jul Jul Jul Jul Jul Jul 19 31 31 31 16 12 31 31 31 31 31 31 31 31 09:59 02:17 02:48 02:48 02:55 12:42 09:58 02:17 02:13 02:48 02:48 02:48 02:48 02:48 0001.pcap anaconda-ks.cfg Desktop Documents Downloads fbcmd_update.php index.html install.log install.log.syslog Music Pictures Public Templates Videos

5. List Files and Directories with / Character at the end Using -F option with ls command, will add the / Character at the end each directory.
# ls -F 0001.pcap Desktop/ Downloads/ install.log.syslog Pictures/ Templates/ anaconda-ks.cfg Documents/ fbcmd_update.php Public/ Videos/ index.html install.log Music/

6. List Files in Reverse Order The following command with ls -r option display files and directories in reverse order.

# ls -r Videos Documents Templates Desktop Public Music anaconda-ks.cfg Pictures install.log.syslog 0001.pcap install.log index.html fbcmd_update.php Downloads

7. Recursively list Sub-Directories ls -R option will list very long listing directory trees. See an example of output of the command.
# ls -R total 1384 -rw-------. 1 root -rw-------. 1 root root root 33408 Aug 30508 Aug 8 17:25 anaconda.log 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 -rw-r--r--. 1 root root

0 Aug 12 03:17 access.log 390 Aug 12 03:17 access.log-20120812.gz

8. Reverse Output Order With combination of -ltr will shows latest modification file or directory date as last.
# ls -ltr total 176 -rw-r--r--. -rw-r--r--. -rw-------. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. -rw-r--r--. -rw-r--r--. drwxr-xr-x. -rw-r--r--. 1 1 1 2 2 2 2 2 2 2 1 1 4 1 root root root root root root root root root root root root root root root root root root root root root root root root root root root root 11439 48867 1586 4096 4096 4096 4096 4096 4096 4096 46701 21262 4096 683 Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Jul Aug Aug Aug 31 31 31 31 31 31 31 31 31 31 31 12 16 19 02:13 02:17 02:17 02:48 02:48 02:48 02:48 02:48 02:48 02:48 09:58 12:42 02:55 09:59 install.log.syslog install.log anaconda-ks.cfg Desktop Videos Templates Public Pictures Music Documents index.html fbcmd_update.php Downloads 0001.pcap

9. Sort Files by File Size With combination of -lS displays file size in order, will display big in size first.
# ls -lS total 176 -rw-r--r--. -rw-r--r--. -rw-r--r--. -rw-r--r--. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. -rw-------. -rw-r--r--. 1 1 1 1 2 2 4 2 2 2 2 2 1 1 root root root root root root root root root root root root root root root root root root root root root root root root root root root root 48867 46701 21262 11439 4096 4096 4096 4096 4096 4096 4096 4096 1586 683 Jul Jul Aug Jul Jul Jul Aug Jul Jul Jul Jul Jul Jul Aug 31 31 12 31 31 31 16 31 31 31 31 31 31 19 02:17 09:58 12:42 02:13 02:48 02:48 02:55 02:48 02:48 02:48 02:48 02:48 02:17 09:59 install.log index.html fbcmd_update.php install.log.syslog Desktop Documents Downloads Music Pictures Public Templates Videos anaconda-ks.cfg 0001.pcap

10. Display Inode number of File or Directory We can see some number printed before file / directory name. With -i options list file / directory with inode number.
# ls -i 20112 23611 23564 23612 23594 23601 0001.pcap 23610 Documents Music 23597 Templates anaconda-ks.cfg 23595 Downloads Pictures 23613 Videos Desktop 23585 fbcmd_update.php Public 23793 index.html 22 install.log 35 install.log.syslog

11. Shows version of ls command Check version of ls command.


# ls --version ls (GNU coreutils) 8.4 Copyright (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. Show Help Page

List help page of ls command with their option.


# ls --help Usage: ls [OPTION]... [FILE]...

13. List Directory Information With ls -l command list files under directory /tmp. Wherein with -ld parameters displays information of /tmp directory.
# ls -l /tmp total 408 drwx------. 2 narad -r--------. 1 root drwx------. 2 root drwx------. 2 root drwx------. 2 gdm drwx------. 2 root drwx------. 2 narad drwx------. 2 gdm -rw-------. 1 root 34LJTAa1.yumtx # ls -ld /tmp/

narad 4096 Aug 2 02:00 CRX_75DAF8CB7768 root 384683 Aug 4 12:28 htop-1.0.1.tar.gz root 4096 Aug 4 11:20 keyring-6Mfjnk root 4096 Aug 16 01:33 keyring-pioZJr gdm 4096 Aug 21 11:26 orbit-gdm root 4096 Aug 19 08:41 pulse-gl6o4ZdxQVrX narad 4096 Aug 4 08:16 pulse-UDH76ExwUVoU gdm 4096 Aug 21 11:26 pulse-wJtcweUCtvhn root 300 Aug 16 03:34 yum_save_tx-2012-08-16-03-

drwxrwxrwt. 13 root root 4096 Aug 21 12:48 /tmp/

14. Display UID and GID of Files To display UID and GID of files and directories. use option -n with ls command.
# ls -n total 36 drwxr-xr-x. drwxr-xr-x. drwxr-xr-x. -rw-rw-r--. drwxr-xr-x. 2 2 2 1 2 500 500 500 500 500 500 500 500 500 500 4096 4096 4096 12 4096 Aug 2 01:52 Aug 2 01:52 Aug 2 01:52 Aug 21 13:06 Aug 2 01:52 Downloads Music Pictures tmp.txt Videos

15. ls command and its Aliases We have made alias for ls command, when we execute ls command itll take -l option by default and display long listing as mentioned earlier.
# alias ls="ls -l"

Note: We can see number of alias available in your system with below alias command and same can be unalias as shown below example.
# alias

alias 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'

To remove an alias previously defined, just use the unalias command.


# unalias ls

In our next article well cover up more or advanced ls command with their examples. If weve missed anything in the list, please update us via comment section.

Potrebbero piacerti anche