Open Source Musings The thoughts, ideas, and opinions of an open source guy

Using ls

A bunch of physical files, all in a row

While I’m no command line wizard, I’m a borderline junkie. I use the command line a lot. And, yes, I remember saying that you don’t need to use the command line in order to use Linux. But it 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:

  • The name of the file
  • The size of the file in kilobytes
  • The permissions on the file
  • Who created the file
  • Who last modified the file
  • The date on which the file was last modified

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

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 that begin with the letter S. Just type ls S*.mp3.

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

Thoughts? Let's start a conversation on Twitter.

Did you enjoy this post or find it useful? Then please consider supporting this blog with a micropayment via Liberapay. Thanks!