Using ls

26 July, 2021

Here’s the latest in an irregular series of short posts that teach some Linux command line basics that you might not know or may have forgotten.

While I keep saying that you don’t need to use the command line in order to use Linux, knowing a few basic commands can be a useful.

Take, for example, times when you have a folder on your hard drive packed with files. Hands up if you don’t have at least one. Yeah, I thought so. Me, too.

How do you effectively and efficiently view the files in a directory? And how do you pinpoint the files that you want to see? By using the ls command, of course.

Let’s take a look at some of the ways in which you can use ls.

Letting Your Fingers Do the Walking

Crack open a terminal and navigate to a folder. Then, type the command ls.

Getting a file listing

That gives you a listing of all the files in the directory. You get the names of the files but not much else. You can get more information about a file by typing the command ls -l.

Getting details of files

Using the -l option, you get the following information:

There’s one more option that I want to look at. It’s -a. When you run ls -a you get a listing of all the files in a directory, including the hidden ones. Hidden files begin with a dot, for example .mozilla.

Getting file attributes

Using Wildcards

Running ls list all of the files in a folder. Which is OK if there are a few of them. But what not so good if you have a lot of them. You can zero in on a file or type of file by using a wildcard.

For example, let’s say you have a folder filled with songs. And you want to get a listing of all of the Ogg Vorbis files. To do that, run the command **ls *.ogg**.

Using wildcards with ls

You can further narrow down the number of files returned by using another wildcard. Like what? Say you want to list all of the MP3 files in a folder whose names contain the word The. To do that, type ls *The*.mp3.

Using another wildcard with ls

Final Thoughts

The ls command is very useful and very flexible. And it has a number of other options, which you can read about here.

Scott Nesbitt