ls Command in Linux. List Files and Directories

ls command

Introduction

The ls command is a common and most frequently used command in Linux. And if you are a Linux user you must be knowing and most have used once or more. This ls command is used to list directory contents. And you can also use this command to list out information of files and directories in your File system. And also The  ls utility is a part of the GNU core utilities package which is installed on all Linux distributions.

But in this chapter i will try to cover and discuss about the ls command in detailed. Also discuss about the various type of ls options, with different practical examples.

Synopsis Or Syntax

The general syntax of ls command is as mentioned below.

ls [Option] [Files]

But when ls command is invoked without any argument it will list all the available files in the present working directory.

$ ls

Output

Options

ls have a large number of options . But in this table some of the most used and important options are discussed.

OptionDescription
-aShows all the filenames starting with a dot (Including . and ..)
-AShows all filenames but do not list implied . and ..
-xMulticolumnar output
-lLong listing in ASCII collating sequence
-tSorts filenames by last modification time
-uSorts filenames by last access time
-idisplays inode number
-RRecursive list
-rSorts filenames in reverse order
-FAppend indicator (one of */=>@|) to entries. Executibles(*), Directories(/), Symbolic link(@)

How to see color Output ?

In Linux and UNIX this ls command helps to distinguish file types by its color. We can invoke the below command to see color output.

$ ls --color=auto

Output

In this above output you can see different color combination is there, now let us know the color code and what it defines.

  • Skyblue —> Symbolic link file
  • Blue —> Directory
  • Light Green —> Binary file
  • red —> zip file

There are lot but i have mentioned few.

If you want to remove the colorful output then invoke the below command.

$ unalias ls

Examples

Here are the few of examples for better understanding of ls command, and how it gives the output while invoked with different options.

Long Listing Files

As you know when you invoke the ls command it just lists the available files and directories. But using the -l (Upper case L) with ls it will long list in ASCII collating sequence showing the 7 attributes of a file in 9 fields.

$ ls -l /home/Desktop/black.jpg
Output

-rwxrw-rw- 1 tcd tcd 180500 Jun 5 02:15 black.jpg

You can see 7 attributes of black.jpg comes in the output but in 9 fileds. Below are the attributes which came in the result.

  • Type and Permission Field
  • No of Hardlinks
  • Owner of the File
  • Group of the File
  • Size of the File in bytes
  • Last Modification Time
  • File Name

Sort Files

Sometimes its very important to find a latest filename in your directory. Using the below command you will be able to sort the files by its modification time.

$ ls -t

It is a better practice to use -t option with -l as the output will have a good understanding result.

Show Hidden Files

Linux system does not show its hidden files by default. To see the hidden files we have to use ‘-a’ option.

A hidden file always start with (.) or (..)

Pro Tip: You can rename a file with (.filename) to hide it.

Below is the command to see all the files including . and ..

$ ls -a

Use -l to get a listing output.

You can see i have created a directory named .Project. While running ls command its not showing.

Conclusion

It will take long more if i describe all of the options. For example i have discussed few. You can explore more. Hope this tutorial gives you a clarity on ls command .

Want to see more option or details about ls command, run man ls in your terminal.

One Comment on “ls Command in Linux. List Files and Directories”

Leave a Reply

Your email address will not be published. Required fields are marked *