ls Command in Linux to List Files and Directories

ls Command in Linux

The ls command in Linux is likely one of the first commands you ever need to use. In this article, we’ll go over the command and commonly used parameters.

My preferred set of options is as follows:

ls -Zaltrh

Let’s dig into each option individually, and explain why the entire glob of options is helpful.

Linux LS Command Syntax

#ls [OPTION] [FILE]
OPTIONS:
[-a], do not ignore entries starting with . or ..
[-h], with -l, print sizes in human readable format (e.g., 1K 234M 2G)
[-l], long list format (shows more information)
[-r], reverse order while sorting
[-t] sort by time, newest first
[-Z], display security context so it fits on most displays.

Let’s look at a basic ls output.

ls
ls output

ls output

You’ll notice it has some default color scheming but is otherwise a bare listing of files and directories. No specific details. You can check where the coloring is configured by typing in:

alias ls
alias ls='ls --color=auto'

Options

Now let’s look at the [-a] entry.

ls -a
ls -a output

ls -a output

The [-a] option allows us to see hidden files, as well as the top directory [..] and the current directory [.] permissions. Let’s add the [-l] option now.

ls -al
ls -al output

ls -al output

More info now. We can see the file permissions, the owner and group, size in bytes, and date last modified. Helpful, but we can add more useful information. Let’s make it human-readable.

ls -ahl
ls -ahl output

ls -ahl output

Now the size formats to readable information. Instead of 4096, we see 4.0K. This is less accurate, but normally we don’t need down-to-the-byte size accuracy. Next step, I prefer to see files sorted by time modified.

ls -athl
ls -athl output

ls -athl output

Okay, so the last thing modified is on top, I prefer it on the bottom. That’s where I’ll look first, for the most recent object modified. Let’s reverse the sort.

ls -arthl
ls -arthl output

ls -arthl output

Now the last file I edited is on the bottom. I work with SELinux often, so if that’s your boat the [-Z] flag is a lifesaver. Let’s change up the ordering of the flags, I prefer a pronounceable arrangement. Zee-alter-h.

ls -Zaltrh
ls -Zaltrh output

ls -Zaltrh output

And there’s the security context I need to know. Or in this case, a “?”. Now if you see a “?”, don’t worry. You simply are not using SELinux enforcement. If you run the same ls with options on AWS, you’ll see a set of security contexts.   That’s the combination I find myself using the most. Some other helpful options are sorting by size and listing recursively.

Sort by Size

ls -Sharl
ls -Sharl output

ls -Sharl output

There, my largest file is on the bottom in a human-readable format. Followed by the root directories.

List Recursively

Sometimes, I need to view an entire directory. ls has a recursive option, [-R]. I recommend dropping the [-a] option as it will show the [.] and [..] directories many times, and isn’t helpful.

ls -HaltrR
ls -HaltrR output

ls -HaltrR output

That’s everything sorted by time, with the last modified directory on the bottom. Sorting by size with [-S] will put the largest directory on the bottom.

Conclusion

That’s it. You now know how to view all your files and directories in Linux along with information about them. Sort for the largest files and directories, or most recently modified. In Linux, the ls command is helpful for troubleshooting any recent changes, clearing out large directories, and much more.

Learn more about Linux shell tips here.

SHARE:
nv-author-image

Samuel Berry

I'm a writer, in the sense that there are words written and things needing explaining. Years of schooling, running on twelve now, taught me one thing, I like taking the complicated down to complex. So, I'm writing about Linux. One of those things that starts as complicated, and after a few years turns into complex. Until the next new thing rolls out anyways. Working in IT, I learned advocating for your product is the only way to ensure adoption. Providing user manuals, pictures, diagrams, and everything else possible to our community. That's what builds the "user-friendly" experience. Not only design, but inclusion. Talk to me about Linux, I'm here to learn by teaching.

Leave a Reply

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