Tips for listing files with ls at the Linux command line

Learn some of the Linux 'ls' command's most useful variations.
338 readers like this.
open source button on keyboard

Opensource.com

One of the first commands I learned in Linux was ls. Knowing what’s in a directory where a file on your system resides is important. Being able to see and modify not just some but all of the files is also important.

My first LInux cheat sheet was the One Page Linux Manual, which was released in1999 and became my go-to reference. I taped it over my desk and referred to it often as I began to explore Linux. Listing files with ls -l is introduced on the first page, at the bottom of the first column.

Later, I would learn other iterations of this most basic command. Through the ls command, I began to learn about the complexity of the Linux file permissions and what was mine and what required root or sudo permission to change. I became very comfortable on the command line over time, and while I still use ls -l to find files in the directory, I frequently use ls -al so I can see hidden files that might need to be changed, like configuration files.

According to an article by Eric Fischer about the ls command in the Linux Documentation Project, the command's roots go back to the listf command on MIT’s Compatible Time Sharing System in 1961. When CTSS was replaced by Multics, the command became list, with switches like list -all. According to Wikipedia, ls appeared in the original version of AT&T Unix. The ls command we use today on Linux systems comes from the GNU Core Utilities.

Most of the time, I use only a couple of iterations of the command. Looking inside a directory with ls or ls -al is how I generally use the command, but there are many other options that you should be familiar with.

$ ls -l provides a simple list of the directory:

linux_ls_directory list.png

Using the man pages of my Fedora 28 system, I find that there are many other options to ls, all of which provide interesting and useful information about the Linux file system. By entering man ls at the command prompt, we can begin to explore some of the other options:

linux_ls_man command.png

To sort the directory by file sizes, use ls -lS:

linux_ls_sort by file size.png

To list the contents in reverse order, use ls -lr:

linux_ls_list in reverse order.png

To list contents by columns, use ls -c:

linux_ls_list contents by column.png

ls -al provides a list of all the files in the same directory:

linux_ls_list all files in directory.png

Here are some additional options that I find useful and interesting:

  • List only the .txt files in the directory: ls *.txt
  • List by file size: ls -s
  • Sort by time and date: ls -d
  • Sort by extension: ls -X
  • Sort by file size: ls -S
  • Long format with file size: ls -ls
  • List only the .txt files in a directory: ls *.txt

To generate a directory list in the specified format and send it to a file for later viewing, enter ls -al > mydirectorylist. Finally, one of the more exotic commands I found is ls -R, which provides a recursive list of all the directories on your computer and their contents.

For a complete list of the all the iterations of the ls command, refer to the GNU Core Utilities.

User profile image.
Educator, entrepreneur, open source advocate, life long learner, Python teacher. M.A. in Educational Psychology, M.S. Ed. in Educational Leadership, Linux system administrator.

4 Comments

Thank you for the detailed information about this command.
The command "ls" is used very often - almost every day, recently I learned the options I need of this command in this video.
https://www.tubeyou.live/video/904788/tips-of-ls-unix-command
Better try to learn the basics of the command line, this greatly reduces the operating time in Linux systems when you administrating unix servers.

Thanks, I'm always looking for these commands.

-s seemed to sort alphabetically, -S by size. -d gave directories not date/time

I like to use ls -A which is similar to ls -a, but doesn't show the '.' and '..' files.

Creative Commons LicenseThis work is licensed under a Creative Commons Attribution-Share Alike 4.0 International License.