ls – Listing the contents of a directory

by Daniel Pham
Published: Updated:
This entry is part 1 of 6 in the series Linux commands: Part 3 - Exploring the system

To list the files and directories in the current working directory, we use the ls command.

If on the Windows operating system, when you move to a specific folder, the files and subdirectories in it is automatically displayed for you to view. But for the Linux operating system (for the server) you need to use the ls command

Meaning of the command “ls”

The ls command lets you list files or subdirectories contained in a directory. The use is quite simple, on the command line of linux, you just type ls if you are standing in the directory that you need to list.
For example, as shown below, i’m in the /etc directory of the system, i type ls and all files or directories listed.

ls command
ls command

If you want to list the contents of a directory without looking at where you are, then add the path to the directory immediately after the ls command.

ls /etc

A few advanced uses of the “ls” command

The first is often used to list the contents of a folder and display the results in the latest time stamp at the bottom. Type the command as follows:

Command below with the parameters as follows:

  • -l: use a long listing format.
  • -t: sort by modification time, newest first.
  • -r: reverse order while sorting.
ls -ltr
ls command with options -ltr
ls command with options -ltr

The second case is to list the contents of the folder and display the results according to the latest timestamp at the top. Type the command as follows:

The -a parameter: do not ignore entries starting with .

If you look at the image below will see there are several folders with the directory name is a dot.

ls -alt
ls command with options -alt
ls command with options -alt

The third case is to list the contents of the folder and display the results by the size of the file or folder with the largest size at the top. Type the command as follows:

The -S (s uppercase character) parameter: sort by file size, largest first.

ls -l -S
ls command with options -l -S
ls command with options -l -S

For more options of the ls command then you type the command below:

ls --help

Conclusion

With this article, I hope you can understand how to use this command to list the contents of a certain folder, as well as how to use the parameters of the this command.

(This is an article from my old blog that has been inactive for a long time, I don’t want to throw it away so I will keep it and hope it helps someone).

0 0 votes
Article Rating
Series NavigationNext part: Use “file” command to determine the type of file »»

You may also like

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments

DevOps Lite is a personal blog specializing in technology with main topics about DevOps, DevSecOps, SRE and System Administrator. Articles are shared for free and contributed to the community.

SUPPORT US

FOLLOW US

Subscribe my Newsletter for new blog posts. Stay updated from your inbox!

© 2021-2024 DevOpsLite.com – All rights reserved.

Please write sources “DevOpsLite.com” when using articles from this website.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More

2
0
Would love your thoughts, please comment.x
()
x

Adblock Detected

Please support us by disabling your AdBlocker extension from your browsers for our website.