List the Directory

Ls Command

ls command is used to list the contents of a current directory.

Syntax

ls [options]

ls lists files and directories in the bare format where details like file types, size, modified date, modified time, permission, links, etc can’t be viewed.

There are some options that can be used with ls command to change the output format. The following table shows some common options that you may need.

Options Description Syntax

-l

Long listing, displays lots of information

ls -l

-a

Shows hidden files

ls -a

-t

Sorts by modification time

ls -t

-r

Sorts in reverse order

ls -r

-s

Prints the allocated size of each file, in blocks

ls -s

Create a File Structure Problem

Consider the following imaginary situation where you can put your Linux skills to work. Imagine that you work in a pet shop that has recently purchased a Linux system to keep track of its animals. You’ve been asked to create a file structure which has a file for each type of animal in the pet shop. Your first try was to create a "flat" file structure with all the animals in the same directory. This structure is shown below, with the files represented by pictures of animals.

dirStructure.png

Upon further consideration, you realize that as you add more and more animals to the structure, it will be easier to manage if you put them in separate directories. For example, different directories can be created for cats, dogs, fish, so on.

The command ls is used to list the contents of your current directory. Type the ls command from the pet directory which contains different animal subdirectories as follows:

pet$ls

Output

pet]$ ls
cats dogs fish monkeys snakes

The files are listed in alphabetical order from left to right. However, sometimes, the command prompt will list the files from top to bottom instead of left to right.