Home » Linux » Basic Linux Commands » The “ls” Command in Linux

The “ls” Command in Linux

We use the “ls” command to list the files and directories on Linux based operating systems. “ls” is the most frequently used command and the mostly first one which is used by someone when they start learning Linux.

“ls” command also used to identify the file information such as size.

ls : Will list all the directories on the system.

$ ls
Desktop    Downloads Music     Public     Videos
Documents  Pictures  Templates

To change the way the files are listed, “ls” command accepts flags/options as mentioned below:

ls / : Lists the contents of the root directory.

$ ls /
bin   home            lib32       media  root  srv  var
boot  initrd.img      lib64       mnt    run   sys  vmlinuz
dev   initrd.img.old  libx32      opt    sbin  tmp  vmlinuz.old
etc   lib             lost+found  proc   snap  usr

ls ~ :  Will list the contents in the users’s home directory.

$ ls ~
Desktop    Downloads Music     Public     Videos
Documents  Pictures  Templates

ls -d */ : Lists only directories available.

$ ls -d */
bin/   etc/   lib32/   lost+found/  opt/   run/   srv/  usr/
boot/  home/  lib64/   media/       proc/  sbin/  sys/  var/
dev/   lib/   libx32/  mnt/         root/  snap/  tmp/

ls * : List the contents of a directory with it’s sub-directories.

$ ls *

home:
myuser


opt:
google

run:
alsa                 gdm3            plymouth           systemd
alsactl.pid          gdm3.pid        sendsigs.omit.d    tmpfiles.d

snap:
README

usr:
bin    include  lib32  libexec  local  share
games  lib      lib64  libx32   sbin   src

var:
backups  cache  lib  local  lock  log  mail  opt  run  snap  spool  tmp

ls -s (the s is lowercase) : Lists files or directories with their sizes:

$ ls -s
total 72
 0 bin    0 initrd.img       0 libx32       0 proc   4 srv   0 vmlinuz
 4 boot   0 initrd.img.old  16 lost+found   4 root   0 sys   0 vmlinuz.old
 0 dev    0 lib              4 media        0 run    4 tmp
12 etc    0 lib32            4 mnt          0 sbin   4 usr
 4 home   0 lib64            4 opt          4 snap   4 var

ls -l  : Will list the contents of the directory in a table format with the following columns :

  • Permissions
  • Links to the content
  • Owner
  • Group owner
  • Size (in bytes)
  • Last modified date / time
  • Name of the file/directory
$ ls -l
total 72
lrwxrwxrwx   1 root root     7 Jan  4 08:19 bin -> usr/bin
drwxr-xr-x   3 root root  4096 Apr 19 21:41 boot
drwxr-xr-x  19 root root  3400 May 18 14:08 dev
drwxr-xr-x 120 root root 12288 May 18 14:14 etc
drwxr-xr-x   3 root root  4096 Jan  4 08:40 home
lrwxrwxrwx   1 root root     7 Jan  4 08:19 lib -> usr/lib
lrwxrwxrwx   1 root root     9 Jan  4 08:19 lib32 -> usr/lib32
lrwxrwxrwx   1 root root     9 Jan  4 08:19 lib64 -> usr/lib64
lrwxrwxrwx   1 root root    10 Jan  4 08:19 libx32 -> usr/libx32

ls -lh : Lists the files or directories in a table format with a column representing the size of each file/directory.

ls -lh
total 72K
lrwxrwxrwx   1 root root    7 Jan  4 08:19 bin -> usr/bin
drwxr-xr-x   3 root root 4.0K Apr 19 21:41 boot
drwxr-xr-x  19 root root 3.4K May 18 14:08 dev
drwxr-xr-x 120 root root  12K May 18 14:14 etc
drwxr-xr-x   3 root root 4.0K Jan  4 08:40 home

ls -t  : Can be used to list files or directories and sort by last modified date in descending order.

$ ls -t
proc  etc   run  boot        root   usr             mnt    libx32  bin

ls -tr: Will reverse the sorting order to ascending order.

$ ls -tr
lost+found  lib32   lib64  vmlinuz.old     home   vmlinuz     opt  snap  tmp

ls -S  (the S is uppercase) : Lists files or directories and sort by size in a descending order.

$ ls -S
lost+found  home   opt   srv  var  initrd.img      vmlinuz.old  lib64  lib

ls -Sr: Is used to reverse the sorting order.

$ ls -Sr
sys   bin    lib32        vmlinuz         run  usr  snap  mnt    boot

That’s all in this article! If you have any questions and/or suggestion, do write to us or comment below.


Subscribe our Rurban Life YouTube Channel.. "Rural Life, Urban LifeStyle"

Leave a Comment