Command Line Basics: List Files with ls

May 28, 2009 by
Filed under: bash, command line, HowTo, linux 

One of the most fundamental things you may want to do from the command line is to list the files in the current directory. That's where the ls command comes in. ls stands for, you guessed it, list.

Say, for example, you are in a directory that has three files in it called file1, file2, and file3. Entering ls on the command line will yield output that looks like:

ls
file1 file2 file3

A common option that is used with the ls command is the -l option. The -l option creates a long listing. The output from that would look like this:

ls -l
-rw-r--r-- 1 owner group 0 2009-05-28 20:43 file1
-rw-r--r-- 1 owner group 0 2009-05-28 20:43 file2
-rw-r--r-- 1 owner group 0 2009-05-28 20:43 file3

This listing output provides the file permissions, the user ID of the file owner, the group ID of the file owner, the file size, the date stamp, the time stamp, and finally, the file name.

Affiliate Link

Hidden Files

In Windows, files are hidden by setting a flag in the file properties. Linux uses a simpler method that has been used in Unix for years. Any file that starts with a "." is hidden by default. To view files in a directory, including the hidden files, use the -a switch with ls. Here's an example.

ls -a
. .. file1 file2 file3 .hidden1

This directory contains a hidden file called .hidden1. You'll also notice that there is a listing for "." and "..". The single dot is a shortcut that stands for the current directory. The double dot is a shortcut that stands for the parent directory.

A fundamental way that terminal commands work is that the options can be strung together to create the desired effect. To create a long listing of all files, you would enter

ls -la
drwxr-xr-x 2 owner group 4096 2009-05-28 20:44 .
drwxr-xr-x 165 owner group 16384 2009-05-28 20:56 ..
-rw-r--r-- 1 owner group 0 2009-05-28 20:43 file1
-rw-r--r-- 1 owner group 0 2009-05-28 20:43 file2
-rw-r--r-- 1 owner group 0 2009-05-28 20:43 file3
-rw-r--r-- 1 owner group 0 2009-05-28 20:44 .hidden1

The options can also be expressed in a number of ways. The following four ways will all yield the same output:

ls -la
ls -al
ls -a -l
ls -l -a

To find out more about ls, just look at the manual page.

man ls

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>