Tips for listing files with ls at the Linux command line

Tips for listing files with ls at the Linux command line

Learn some of the Linux 'ls' command's most useful variations.

open source button on keyboard
Image by : 

opensource.com

x

Get the newsletter

Join the 85,000 open source advocates who receive our giveaway alerts and article roundups.

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:

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:

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

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

To list contents by columns, use ls -c:

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

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.

About the author

Don Watkins - Educator, education technology specialist,  entrepreneur, open source advocate. M.A. in Educational Psychology, MSED in Educational Leadership, Linux system administrator, CCNA, virtualization using Virtual Box. Follow me at @Don_Watkins .