Using the ls command in Linux


In my opinion, the ls command is Most used Linux command It is often used to check the result of the previous operation by List the contents of the directory.

The ls command stands for List; All it does is list the contents of the specified directory.

In this tutorial, I will walk you through several examples of using the ls command.

I will also share some practice questions to test your learning.

How to use the ls command

To use the ls command, you have to follow the simple command syntax:

ls [OPTIONS] Targeted_Directory

here,

  • [OPTIONS]: is used to modify the default behavior of the ls command.
  • Targeted_Directory: This is where you provide the directory name or absolute path to the directory.

You may be wondering what happens when you use the ls command without any options. To answer this, I used the ls command in my current working directory:

ls 
Use the ls command to list the contents of the working directory

As you can see, I have listed all available directories and files in the current working directory. But you can do more than just get the names and files in the current working directory.

Let me show you how.

1. List files and directories with ownership

One of the primary uses of the ls command is to find the permissions and ownership of a specific file or directory.

So you should use a file -l option (also called long list) with the ls command:

ls -l

Once you do that, you can expect similar outputs:

[email protected]:~$ ls -lh
-rwxrw-r-- 1 sagar sagar 666M Dec 10 18:16 Fedora.iso

Notice how I used add -h option? I will discuss it in the next section.

For now, if the output seems too complicated, let me simplify things for you:

Explain the file ownership read and write permissions in Linux with the ls command

As you can see, each block of text has its own meaning such as owner, group and others permissions.

If you want to dig deeper into file permissions, I recommend this Our detailed guide on file permissions in Linux:

Explain Linux file permissions and ownership with examples

Linux file permissions explained in simpler terms. Also, learn how to change file permissions and ownership in Linux in this detailed beginner’s guide.

2. Obtain information in a human-readable format

By default, the file size is shown in bytes which is not the best way to know your file size. So how do you get the same information but in a human readable format?

basic. You are using a file -h option with ls command:

ls -l -h

Here’s a comparison between the default form and the human readable form:

List files with the ls command in a human readable format
Comparison of the virtual model and the human readable model (click to expand)

Much better. is not it?

💡

The ls command is good for knowing file sizes. However, it will not give you the directory size which is always displayed as 4K. To get the size of a directory, use the du command.

3. List of hidden files

Like any other file manager, the ls command will not list hidden files (I mean they are supposed to be hidden. Right?).

But what if you want to List of hidden files with normal files? To do this, you can use -a option:

ls -a

As you can see, the file name starts with a period . They are hidden files.

List hidden files in Linux Terminal using the ls command
Click to expand

💡

You can also use files ls -A which works roughly the same as ls -a It will not include . And .. directories.

4. List files recursively

there Multiple ways to recursively list files One of them is using the ls command.

In case you didn’t know, listing files recursively means listing the files of all existing subdirectories until the last item of each subdirectory is displayed.

And to list files recursively, you can use an extension -R Flag as shown:

ls -R
List files recursively in Linux using ls command

In a way, it gives you the current directory structure. I personally prefer this one but you will have to install it first.

💡

You don’t have to be in the directory to list its contents. You can also list the contents of a directory by providing its absolute or relative path like this: ls /var/log

5. Differentiate between files and directories while using ls

While different colors for files and directories should do the job. But for some reason, if you want to mark up files and directories here you have it.

In the ls command, you have a file -F A tag that adds a forward slash / For each directory name:

ls -F
Differentiate between files and folders while using the ls command

6. List only files that have certain file extensions

There are times when you just want to list files with certain file extensions and trust me, this is the easiest of all.

To do this, you do not have to use any options. Just append the file extension with an asterisk * like *.pngAnd *.txtetc:

ls *.extension

For example, if I just wanted to list the ISO files, I would use the following command:

ls *.iso
List files of specific file extensions

7. Sort the output based on size

To sort the output based on file size, you must use the file extension -S Flag and it will list the files from largest to smallest (descending):

ls -lhS
Sort files based on their size using the ls command

Similarly, if you want to reverse this order to list the smallest files first, you can use the -r Flag to reverse the order:

ls -lhSr
Sort files from smallest to largest using the ls command

8. Sort files based on date and time

The ls command includes the modified time in its list.

To list the most recent files first, you can use an extension -t Flag as shown:

ls -lht
Display the latest files first while using the ls command

You can use the -r Flag as I explained earlier to reverse the order here as well.

ls -lrt

This will give you the most recently modified files at the bottom of the screen. This is especially useful if you have a very large number of files in a directory and want to know which files have been modified recently. I used this while troubleshooting my software project.

using the ls -lrt command

Let’s recap what you’ve learned so far!

Here, I’m going to share a table with multiple options that were used with the ls command in this tutorial:

order a description
ls -l Long list of files and directories
ls -lh Prints information in a human-readable format
ls -a Include hidden files in the list
ls -R List files recursively
ls -F Add a forward slash to the directory name
ls *.ext List of files that have specific extensions
ls -lS Sort files based on file size
ls -lt Sort files based on time
-r reverse sort (with s or t)

🏋️ and practice your learning

It’s always a good idea to practice what you’ve learned, which is why we try to add a practice section in every terminal guide.

So here are some simple exercises with the ls command:

  • List of contents /var/log
  • Save the command output in a file called output.txt
  • Select the 3 most recent files (use time-based sorting)
  • Display files based on their size but in reverse order
  • Check if there are any hidden files

That will be a good practice for you. Stay tuned for more learning Linux commands.

And if you’re new to the terminal, don’t forget to follow our Terminal Basics series

Linux command tutorials for absolute beginners

Never used Linux commands before? No thanks necessary. This tutorial series is for absolute beginners to the Linux terminal.

enjoy 🙂



Source link

Leave a Comment

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