English
+370 5 205 5502 sales@monovm.com

How to use the ls command in Linux

The ls command is one of the most common and quite frankly, the most useful commands in the world of Linux. Everyone starting out as a beginner to expert uses this command frequently. In this article, we put together a comprehensive guide for the ls commands.

09 Feb, 22 by Susith Nonis 7 min Read

List of content you will read in this article:

Linux Command is an effective service that is provided by the Linux operating system. These commands are used to execute all the basic and advanced tasks. The implementation of the commands is done on the Linux terminal. The terminal is a commanding and powerful command-line interface that is used to cooperate with the system. The terminal emulator in Linux is identical to the command prompt in the Windows operating system. The commands that we use in Linux are case-sensitive.

There are numerous Linux commands pre-installed in the Linux OS that enables the user to direct the file system, install software packages, and organize the system and its applications. The commands are issued on the terminal by stating the title of an executable file. The Linux commands can be either a binary program or a script.

As mentioned earlier, there are two types of works that are can be executed through the terminal, basic tasks and advanced tasks. The basic tasks include the creation of a file, deletion of a file, moving a file, etc. while the advanced tasks include several managerial works (like installing a package, user management), network-related tasks, security tasks, etc.

To execute the commands you need first to know how to open the terminal. To open the terminal, press "CTRL + ALT + T" and for executing any command press the “Enter” key. In this article, we will mainly discuss the “ls commands”, what are they, their uses, and examples.

Different Types of Linux Commands

Linux Commands can be of different types. The different types of Linux commands are as follows:

  • Linux Directory Commands
  • Linux File Commands
  • Linux File Content Commands
  • Linux User Commands
  • Linux Filter Commands
  • Linux Networking Command
  • Linux Utility Commands

Prerequisites

  • A machine with files and directories
  • Linux operating system
  • Access to Linux terminal
  • The basic idea of the command-line interface

ls Commands

The ls command is one of the most regularly used commands in the Linux terminal. There are several ls commands that fulfil different needs. The ls commands are one type of Linux Directory commands. We’ll now discuss all the basic ls commands with examples and parameters.

The ls command shows all the available contents of a directory.

Syntax: ls

In Linux, a user can’t see hidden files generally. The hidden files start with a . (dot) and are not detectable in the regular directory. This is where the ls-a command comes in handy. This command enlists the complete content of the existing directory as well as the hidden files.

Syntax: ls -a

The ls command, as mentioned, only shows the files. But if the user wants to see the contents of the files? To get that detailed information, the users have to use the ls – l command. The file will be displayed in a long list format.

The long list format displays the contents column-wise.

Let’s say the contents has seven different attributes, then the attributes will be displayed in seven different columns.

For example,

The 1st column specifies particulars with regard to file permission.
The 2nd column specifies the total number of associations with the list.
The 3rd and 4th columns specify information related to the owner and group.
The 5th column specifies the total size of the file (in Bytes).
The 6th column displays the last updated date and time.
The 7th column displays the file/directory name.

Syntax: ls -l  

This command is used if the user desires to display the size of the file in their preferred format. There are eight different size formats. They are:

K = Kilobyte

M = Megabyte

G = Gigabyte

T = Terabyte

P = Petabyte

E = Exabyte

Z = Zettabyte

Y = Yottabyte

Users can replace SIZE with their preferred choice.

For example, ls -l --block-size=M  

If you select your size as M, then the size of all files will be displayed in Megabytes.

Syntax: ls -l --block-size=[SIZE]  

This command is used in those cases where you only want to show the sub-directories and not any other files.

Syntax: ls -d */ 

If in any situation you want to display all the other file information without the owner information column, then use this command.

Syntax: ls -g  

If in any situation you want to display all the other file information without the group column, then use this command.

The group column will be excluded from the output.

Syntax: ls -lG

In Linux, this command is used if the user wants to colourize and decolourize the list. To get a coloured list, you’ve to replace [VALUE] with 'auto' and if you want to get a decolourized list, replace [VALUE] with 'never'.

Syntax: ls --color=[VALUE]

The ls ~ command displays all the contents of the home directory.

Syntax: ls ~

Using this command you can enlist the details of the parent directory.

Let’s say there are two directories. The current directory is the “Downloads” directory and the directory that is previous to it i.e. its parent directory is the “Home” directory.

Syntax: ls ../

Using this command the user can look into the version of the ls command.

Syntax: ls -version

If you put the -F command with the ls command then it will add a new character ‘ / ‘ at the end of every directory.

Syntax: ls -F

Syntax: ls -help

Syntax: Is -n

The ls -r command shows all the existing files and directories but in reverse order.

Syntax: ls -r

The ls -R command only displays only the long listing directory trees.

Syntax: Is -R

When you use the ls -i command will display all the existing files or directories with an inode number before it.

Syntax: Is -i

In Linux, if the user wants to create an alias of their Is command, then they can use this command.

Syntax: Is -l

There are a plethora of ls commands that can be used to unlist the files and directories in a Linux operating system. But, these were some of the basic ls commands in the Linux operating system. These commands help users to implement their tasks comfortably and effortlessly. They are also proven to be very effective. 

Users need to practice them a lot as it may take some time to get adapted to these commands.

Susith Nonis

I'm fascinated by the IT world and how the 1's and 0's work. While I veture into the world of Technology I try to share what I know in the simplest way possible. Not a fan of coffee, a sweet addict and a self accredited 'master chef'.