ls command is one of the most commonly used commands in Linux. If you're right, the ls command is the first command the entrant uses to enter the Linux Box command prompt.
We may not know or use all the available ls options, but we use ls commands frequently every day. In this article, we'll discuss the basic ls commands, and try to cover as many parameters as possible.
1. Use ls to list files without options
ls does not have the option to list files and directories in an exposed format, so we will not be able to view details such as file type, size, date and time modified, permissions and links, etc.
# ls
0001.pcap |
Desktop |
Downloads |
index.html |
install.log.syslog |
Pictures |
Templates
|
anaconda-ks.cfg |
Documents |
fbcmd_update.php |
install.log |
Music |
Public |
Videos |
2 list files with options – l
Here, ls -l (- l is not a character) shows the file or directory, size, date and time of modification, the name of the file or folder, and the owner of the file and its license.
# ls -l total 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.cfg drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents drwxr-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.syslog drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos
3. View hidden files
List all files, including hidden files at the beginning
# ls -a
. |
.bashrc |
Documents |
.gconfd |
install.log |
.nautilus |
.pulse-cookie |
.. |
.cache |
Downloads |
.gnome2 |
install.log.syslog |
.netstat.swp |
.recently-used.xbel
|
0001.pcap |
.config |
.elinks |
.gnome2_private |
.kde |
.opera |
.spice-vdagent
|
anaconda-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. Use the option - lh to list readable format files
Use the - lh option in combination to list readable formats and display sizes.
# ls -lh total 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.cfg drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Documents drwxr-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.syslog drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Music drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Public drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Templates drwxr-xr-x. 2 root root 4.0K Jul 31 02:48 Videos
5. List files and directories with "/" character at the end
Using the - F option in the ls command adds the '/' character to the end of each directory.
#Ls -F
0001.pcap
|
Desktop/
|
Downloads/
|
index.html
|
install.log.syslog
|
Pictures/
|
Templates/
|
anaconda-ks.cfg
|
Documents/
|
fbcmd_update.php
|
install.log
|
Music/
|
Public/
|
Videos/
|
6. List documents in reverse order
The following commands with the ls -r option display files and directories in reverse order
#Ls -r
Videos
|
Public
|
Music
|
install.log
|
fbcmd_update.php
|
Documents
|
anaconda-ks.cfg
|
Templates
|
Pictures
|
install.log.syslog
|
index.html
|
Downloads
|
Desktop
|
0001.pcap
|
7. List subdirectories recursively
The ls -R option lists a long list tree. See example command output.
# ls -R total 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. Reverse output sequence
When combined with - ltr, the latest modified file or directory date is displayed.
# ls -ltr total 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.cfg drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Videos drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music drwxr-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.php drwxr-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. Sort files by file size
When combined with - lS, the file size is displayed in order, and the large file is displayed first.
# ls -lS total 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.syslog drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Desktop drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Documents drwxr-xr-x. 4 root root 4096 Aug 16 02:55 Downloads drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Music drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Pictures drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Public drwxr-xr-x. 2 root root 4096 Jul 31 02:48 Templates drwxr-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. Display the index node number of the file or directory
We can see that some numbers are printed before the file / directory name. Use the - i option to list files / directories with inode numbers.
11 show version of ls command
Check the version of the 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. Display help page
Lists the help page for the ls command and its options.
# ls --help Usage: ls [OPTION]... [FILE]...
13. List catalog information
Use the ls -l command to list the files under directory / tmp. Where the - ld parameter displays the information of the / tmp directory.
# ls -l /tmp total 408 drwx------. 2 narad narad 4096 Aug 2 02:00 CRX_75DAF8CB7768 -r--------. 1 root root 384683 Aug 4 12:28 htop-1.0.1.tar.gz drwx------. 2 root root 4096 Aug 4 11:20 keyring-6Mfjnk drwx------. 2 root root 4096 Aug 16 01:33 keyring-pioZJr drwx------. 2 gdm gdm 4096 Aug 21 11:26 orbit-gdm drwx------. 2 root root 4096 Aug 19 08:41 pulse-gl6o4ZdxQVrX drwx------. 2 narad narad 4096 Aug 4 08:16 pulse-UDH76ExwUVoU drwx------. 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. UID and GID of display file
Displays UID s and GID S for files and directories. Use the option - n with the ls command.
# ls -n total 36 drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Downloads drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Music drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Pictures -rw-rw-r--. 1 500 500 12 Aug 21 13:06 tmp.txt drwxr-xr-x. 2 500 500 4096 Aug 2 01:52 Videos
15. ls command and its alias
We set the alias for the ls command. When we execute the ls command, the - l option will be used by default, and a long list as mentioned above will be displayed.
# alias ls="ls -l"
Note: with the following alias command, we can see the number of aliases available in the system, which can be non aliases, as shown in the following 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 a previously defined alias, simply use the unalias command.
# unalias ls
In our next article, we will introduce more or advanced ls commands with examples.