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
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
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
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
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
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
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
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
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
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.