ls – Listing the contents of a directory

Share this post to your friends !

This entry is part 1 of 6 in the series Linux commands: Part 3 - Exploring the system
ls – Listing the contents of a directory
5 (100%) 2 vote[s]

WriteBash - 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-listing-the-contents-of-a-directory-01 ls - Listing the contents of a directory
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-listing-the-contents-of-a-directory-02 ls - Listing the contents of a directory
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-listing-the-contents-of-a-directory-03 ls - Listing the contents of a directory
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-listing-the-contents-of-a-directory-04 ls - Listing the contents of a directory
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.

Continue reading the seriesNext part: Use “file” command to determine the type of file »»
If you liked this article, then please subscribe to our YouTube Channel for more video tutorials. You can also find us on Twitter and Facebook.


Share this post to your friends !
If you appreciate what we share in this blog, you can support us by:
  1. Stay connected to: Facebook | Twitter | Google Plus | YouTube
  2. Subscribe email to recieve new posts from us: Sign up now.
  3. Start your own blog with SSD VPS - Free Let's Encrypt SSL ($2.5/month).
  4. Become a Supporter - Make a contribution via PayPal.
  5. Support us by purchasing Ribbon Lite Child theme being using on this website.

We are thankful for your support.

single post bottom banner
«« »»

Got something to say? Join the discussion

Please keep in mind that all comments are subject to our Comment Policy. Your email address will not be published.
This site uses Akismet to reduce spam. Learn how your comment data is processed.