LS Command in Linux

We offer you a brighter future with FREE online courses - Start Now!!

In this article, you will all there is to the ls command in Linux. We will be going through what the ls command is, and what are its uses. We will also be going through the different options that are available with the ls command. In the end, we will be looking into some really helpful practical examples of how to use the ls command in the Linux terminal.

What is the ls command?

The ls command is a command-line-based tool in Linux-based operating systems that shows the full list of content in your present working directory. Ls id the abbreviation for List.

The ls command is one of the most loved and frequently used commands. It is so commonly used that the average Linux user, will use it at least once every day! In fact, it is one of the commands that is entered when you first enter the terminal.

What is ls command used for in Linux?

As I mentioned earlier, the ls command shows the full list of content in your present working directory. In slightly more intimidating words, the ls command displays or lists the files and directories within the file system.

The ls command is a part of the GNU core utility package which is installed on all Linux distributions. That’s right, there is no hassle in installing the ls tool onto your system as it comes preinstalled in all the Linux distros.

Just like you navigate in your file explores using the GUI, the ls command allows you to do the same via the command line.

Syntax of Linux ls command

The syntax of the ls command is pretty simple. In fact, you can even run just the ls common with no additional information:

syntax of the ls command

In the above output, the ls command listed all the content in my present working directory (/home/gopikiran)

I can also list the files of any other directory by simply going into the desired directory using the cs command and then using the ls command.

syntax of ls command

If observe the outputs closely, you will find that the sub-directories are blue text, whereas the files are in plain white text.

If at all you want more specific and desired outputs, we follow the below given general syntax:

ls <options> <directory>

Options used with the ls command

In the syntax of the ls command, there is a field where ls can take on options or flags which are used to give additional information to the ls command so it changes how files and directories are listed in your terminal.

In other words, the options alternate how the ls command works. Let us look at the available command that we use with the ls command.

1. -a

This option enlists the entire list of the current directory including the hidden files.

-a

2. ~

This option lists the contents of the home directory, irrespective of your present working directory.

~

3. -lh

This option will show you the file sizes in a human-readable format. By default the ls command will show the size of files in bytes, which is very hard to understand, Hence we use the option “-h” to print the sizes in Megabytes.

-lh

4. -lhS

This option displays the file in descending order of their size.

-lhS

5. -l – -block- size=<size>

This option displays the files in a specific size format.

block size

6. -d*/

This option displays only the subdirectories and not the regular files.

-d

7. -m

This option prints out the directories and files separated by a comma.

-m

8. -n

This option prints the group ID and owner ID instead of their names.

-n

9. –color <value>

This option prints the list as colored or discolored

color

10. -p

This option identifies the directories easily by marking the directories with a slash sign.

-p

11. -li

This option prints the index number if the file system is the file is displayed in the first column.

-li

12. -r

This option prints the list in reverse order.

-r

13. -R

This option displays the content of the sub-directories also.

R

14. -lX

This option groups the files with the same extensions together in the list.

-lx

15. -Q

This option adds quotation marks to all directories and files.

-q

16. -l

This option shows the list in a long list format

-l

Before preceding to other options, let us look at what each field means in the above output:

Column-1: This field consists of the permission of the files and directories.

Column -2: This field shows the number of links for that file or directory.

Column -3: This field shows the owner of the file or directory.

Column -4: This field shows the group of the file or directory.

Column -5: This field shows the size of the file or directory in bytes.

Column -6: This field shows the last modified date and time of the file.

Column -7: This field shows the name of the file or directory.

17. ../

This option lists the contents of the parent directory

content of parent directory

In the above output, even though I moved to the directory “DataFlair”, using the command “ls ../” printed the content of the “Desktop” directory, which is the parent directory of the “DataFlair” directory.

18. –version

This option prints information about the version of ls you are using.

version

19. –help

This option prints out all of the commands and information regarding them

20. -g

This option displays the files excluding the column of information regarding the group and user.

-g

21. -lt

This option sorts the list by displaying the recently modified field at the top.

-lt

22. /

This option lists the files in the root directory

files in root directory

23. *

This option lists the contents of the directory along with its subdirectories. It is exactly the same as the option “-R”

directory with subdirectories

24. -l <d>

This option lists the content of the directory you specify in long format.

long format

25. -ltr

This option sorts the list by displaying the recently modified field at the bottom.

-ltr

26. -q

This option hides control characters.

-q

27. -n ~/kv

This option displays the UID and GIDs of a file or directory. This option prints very similar to the option ‘-l’ but shows the UID and GID in numeric format instead of names.

uid

28. -F

This option classifies the files with special characters that have different meanings, let us look at what each character means:

1. / – directory

2. @ – link file

3. * – executable file

4. Nothing – regular file

-f

LS Additional commands

1. Opening the last edited file

You can open the last edited file using the command “ls -t”. The option “-t” sorts out the file and you can pipe it with the “head 1” command to select the first file.

opening the last edited file

2. Displaying only 1 file per line

We can display only file per line using the command “ls -1”

displaying only 1 file per line

3. Creating an alias for the ls command

We can create an alias command for the ls command such that we can include the desired function. Say I want the command “ls -l” to be created as an alias, then I use the following command:

alias ls="ls -l"

If you want to see the aliases in your computer, simply type “alias” in the terminal.

creating alias for ls command

If you want to remove a previously defined alias, you can do so by using the following command:

unalias ls

4. Displaying files under the /tmp directory

To access the files under the /tmp directory use the command “ls -l /tmp”

displaying files under the tmp directory

5. Listing files and storing the output in a file

If you want to store the output of a particular ls command in a specific file. You can do so by using the “>” operator. The “>” operator will store the output of the previously used ls command into the file you specify. Let us understand this with an example.

Let’s say that initially I listed the files of the directory “desktop” and in the next command I copy the output of the last command into the txt file “list”.

Let us take a closer look at this example using screenshots from the terminal :

First, I listed the contents of the Desktop directory:

listing files and storing the output in a file

Then I used the “>” operator in the command “ls > list.txt” to write the output of the previous ls command into the file “list.txt‘

Now, to confirm, that the output has been successfully logged into the file, you can use the cat command as follows:

 

Summary

As you have seen, the ls command is a very simple yet efficient tool that helps in listing the contents of a directory. Using the many options available, you can also change how the ls command works. Now you know what the ls command is all about, as we have seen its uses, syntax, options, and practical commands in the terminal.

Did we exceed your expectations?
If Yes, share your valuable feedback on Google

follow dataflair on YouTube

Leave a Reply

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