Using directories and listing files

Files are used to store data such as text. Directories (folders) are used to store files and to provide a hierarchical organization structure. The Filesystem Hierarchy Standard (FHS) defines the directory structure in Linux distributions. In the FHS, all files and directories appear under the root directory. Most of these directories exist in all Unix-like operating systems and are generally used in the same way. File and directory names in Linux are case sensitive. This means that a file or directory named ABC is not the same as a file or directory named abc. To list the contents of the current directory you're in, use the ls.

$ ls

By itself, the ls just provide the names of the files and directories within the current (or specified) directory. You can display more information about a file by using the -l option to the ls.

$ ls -l

The first character of each line in the output indicates the type of file. The next characters represents the permissions of the file. After permissions is a number which indicates how many hard links point to this file. After hard link count is user owner of the file and group owner of the file. After group owner is the size of the file in bytes. After file size is timestamp which indicates the time that the file's contents were last modified. The final field contains the name of the file or directory. When you display file sizes with the -l option, it would be better if the file size was presented in a more human readable size, like megabytes or gigabytes. To accomplish this, use the -h option with the -l option.

$ ls -lh

For more detailed modification time you can use the --full-time option to display the complete timestamp.

$ ls --full-time

To sort files by size, you can use the -S option. It is most useful when used with the -l and -h option.

$ ls -lS

The -t option will list the most recently modified files first.

$ ls -lt

It is possible to perform a reverse sort with either the -S or -t options by using the -r option. The following command will sort files by size, smallest to largest, and by modification date, oldest to newest:

$ ls -lrS
$ ls -lrt

There will be times when you want to display all contents of a directory as well as all contents of the subdirectories. This is called a recursive listing. You can use the -R option to perform a recursive listing.

$ ls -R

The ls command doesn't display hidden files. A hidden file is any file (or directory) that begins with a dot ('.'). To display all files, including hidden files, use the -a option.

$ ls -a

On most Linux distributions there is a directory called home under the root directory. Under home directory there will be a directory for each user on the system. The directory name will be the same as the username. Most directories in a Linux are protected with file permissions. The only users who can access any files in your home directory are you and the administrator on the system. When you first open a terminal, the current directory should be your home directory. The tilde ('~') character represents your home directory.

A path allows you to specify the exact location of a directory. For the doc directory, the path would be /usr/share/doc. The first / character represents the root directory, while each other / character is used to separate the directory names. This sort of path is called an absolute path. With an absolute path, you always provide directions to a directory starting from the top of the directory structure, the root directory. You can use the cd command with a path to a directory to change your current directory.

$ cd /usr/share/doc

To change back to your home directory, the cd command can be executed without a path

$ cd

or with the tilde as an argument.

$ cd ~

Suppose you are in the /usr/share/doc directory and you want to go to the directory above the current directory. You can use a relative path to move up one level or one level below your current location. Change to the bash directory under the current directory.

/usr/share/doc$ cd bash

Use .. to change to the directory above the current directory.

/usr/share/doc/bash$ cd ..

Use a relative path to change up one level from the current directory and then down into the dict directory.

/usr/share/doc$ ../dict

While the double dot ('..') is used to refer to the directory above the current directory, the single dot ('.') is used to refer to the current directory.