Example Usage of ls Command in Linux

0
292

In this tutorial you will learn how to use ls command in Linux/Unix Operating Systems. ls command is used to list files or directories contents from the command line (CLI).

Through ls command, we obtain information about the files in the current or in any other directory.

Command line syntax of ls command.

ls [OPTIONS] ...[FILES] …

Let’s dig in.

List Current Directory Contents

Open your terminal and type ls command.

ls
Data  file.txt  inputs.txt  Music  THM  user.txt

List Another Directory Contents

Type ls [directory name] i.e. ls /boot

ls /boot
config-5.4.0-21-generic  grub                         initrd.img-5.4.0-56-generic  memtest86+_multiboot.bin     System.map-5.4.0-56-generic  vmlinuz-5.4.0-56-generic
config-5.4.0-47-generic  initrd.img                   initrd.img.old               System.map-5.4.0-21-generic  vmlinuz                      vmlinuz.old

List Contents Including Hidden Contents

Type ls -a command to list all contents including hidden directories and files. Anything that begins with a period (.) is a hidden file in Linux/Unix.

ls -a
.  ..  .bash_history  .bash_logout  .bashrc  dejavu-fonts-ttf-2.37  Documents  Downloads  Music  Pictures  .profile  Videos

List Contents in Long Format

Type ls -l command to list files in an organized manner in form of columns.

ls -l
total 24
drwxrwxr-x 2 pilot pilot 4096 Mar  4 11:52 Data
-rw-rw-r-- 1 pilot pilot   45 Mar  3 22:34 file.txt
-rw-rw-r-- 1 pilot pilot   22 Mar  3 22:36 inputs.txt
drwxrwxr-x 4 pilot pilot 4096 Mar  4 11:51 Music
drwxrwxr-x 2 pilot pilot 4096 Mar  4 11:53 THM
-rw-rw-r-- 1 pilot pilot   54 Mar  3 22:35 user.txt

The columns include:

  • -permission of the files
  • -links to the files
  • -owner of the files
  • -group owner of the files
  • -size of the files
  • -last date/time files is modified
  • -files/directories names

List Contents in Long Format Including Hidden Contents and in Human Readable Format

Type ls -alh or ls -a -l -h command to list all hidden files in an organized manner in form of columns and human readable format sizes (e.g. 1K, 2M, 2G).

ls -alh
total 32K
drwxrwxr-x 5 pilot pilot 4.0K Mar  3 23:08 .
drwxr-xr-x 8 pilot pilot 4.0K Mar  4 11:36 ..
drwxrwxr-x 2 pilot pilot 4.0K Mar  4 11:52 Data
-rw-rw-r-- 1 pilot pilot   45 Mar  3 22:34 file.txt
-rw-rw-r-- 1 pilot pilot   22 Mar  3 22:36 inputs.txt
drwxrwxr-x 4 pilot pilot 4.0K Mar  4 11:51 Music
drwxrwxr-x 2 pilot pilot 4.0K Mar  4 11:53 THM

List Contents and Sort by Time and Date

Type ls -t command to sort files and directories by modification time and date showing newest to oldest in that order.

 ls -t
Videos  Pictures  Music  Documents  dejavu-fonts-ttf-2.37  Downloads

List Contents and Sort by File Size

Type ls -S (uppercase S) command to sort files by their sizes from biggest to smallest.

ls -S
dejavu-fonts-ttf-2.37  Documents  Downloads  Music  Pictures  Videos

List Contents With Their Sizes

Type ls -s (lowercase s) command to print the allocated sizes of each file, in blocks.

ls -s
total 24
4 dejavu-fonts-ttf-2.37  4 Documents  4 Downloads  4 Music  4 Pictures  4 Videos

List Directories Only

Type ls -d */ command to list directories only without their contents.

ls -d */
Data/  Music/  THM/

List Contents With Their Sub-directories

Type ls * command to lists files of the directory together with their sub-directories.

ls *
file.txt  inputs.txt  user.txt

Data:
outputs.txt

Music:
gospel  reggue  songs.txt

THM:
tutorial.txt

And this is a summary of how to use ls command in Linux/Unix OS.

Reference

You can learn more about ls command using their man and info pages:

man ls
info ls

Other Tutorials

Example Usage of ps Command in Linux

Run only Specific Commands with sudo in Linux

How to Add Users to sudo group in Linux

LEAVE A REPLY

Please enter your comment!
Please enter your name here