# 2.3 Using Directories and Listing Files

### **2.3 Using Directories and Listing Files**

**Weight:** 2

**Description:** Navigation of home and system directories and listing files in various locations.

**Key Knowledge Areas:**

* Files, directories
* Hidden files and directories
* Home directories
* Absolute and relative paths

**The following is a partial list of the used files, terms and utilities:**

* Common options for ls
* Recursive listings
* cd
* . and ..
* home and \~

## Linux Files and Directories

So what’s a file, In Linux, a “file” is a fundamental unit of storage that contains data or information. There’s a popular quote “On a UNIX system, everything is a file; if something is not a file, it is a process.” And just for clarification a “directory” is also a file in Linux, as it’s just another file containing name of other files. So now we know since everything is a file let’s see what all sorts of files are there:

* Directories
* Special files i.e. mechanism used for input and output like \`/dev\`
* Links
* Sockets: Yes, sockets are also a type of file
* Named pipes: Acts as communication channel between processes without using network socket semantics.

The best way to view these types of files and their types would be using the command which every LINUX user learns first i.e. \``` ls` `` follow this command with a \``-l`\` and voila you’re able to view the all details of a file.

```
[payam@earth ~]$ ls -l
total 12
drwxr-xr-x. 2 payam payam    6 Nov 17 12:09 Desktop
drwxr-xr-x. 2 payam payam    6 Nov 17 12:09 Documents
drwxr-xr-x. 4 payam payam 4096 Dec  1 09:35 Downloads
-rw-r--r--. 1 payam payam    0 Dec  1 16:27 file1
-rw-r--r--. 1 payam payam    0 Dec  1 16:27 file2.txt
drwx------. 3 payam payam 4096 Nov 18 15:29 kubeconf
drwxr-xr-x. 2 payam payam    6 Nov 17 12:09 Music
drwxr-xr-x. 2 payam payam 4096 Nov 30 14:31 Pictures
drwxr-xr-x. 2 payam payam    6 Nov 17 12:09 Public
drwxr-xr-x. 2 payam payam    6 Nov 17 12:09 Templates
drwxr-xr-x. 2 payam payam    6 Nov 17 12:09 Videos

```

If we look at the above command output `—` represents a regular file while `d`is representation of directory. You might be wondering why other `—`are there those are part of file permissions and we don’t need to look into that for now. Only the first character from \`ls\` output depicts what sort of file is that. Below is the list of different characters representing corresponding file types:

* `-`: Regular file
* `d`: Directory
* `l`: Symbolic link
* `c`: Character device file
* `b`: Block device file
* `p`: Named pipe (FIFO)
* `s`: Socket

Now since we have understood file types, we’ll take a look at `ls` command options.

### Listing Files and Directories in Linux

The `ls` command is used to list all files and directories in the Linux terminal.

* Displays the contents of the current working directory or a specified path.
* Lists items in alphabetical order by default.
* The command can be customized with flags to display file permissions, ownership, size, and modification date.
* Helps users view file and directory names with optional details.

as we have seen the basic usage of ls command is to see what's in the current folder. But ls command has other options:

### `ls` Options Overview

The `ls` command has a variety of options to customize its output:

* `-l` - Long listing format
* `-a` - Include hidden files
* `-h` - Human-readable sizes
* `-t` - Sort by modification time
* `-r` - Reverse order while sorting
* `-R` - List subdirectories recursively
* `-S` - Sort by file size
* `-1` - List one file per line
* `-d` - List directories themselves, not their contents
* `-F` - Append indicator (one of \*/=@|) to entries

#### Long Listing Format

The `-l` option gives you detailed information about files and folders.

It displays information such as:

* file permissions
* number of links
* owner name
* owner group
* file size
* time of last modification
* file or directory name

This format is useful for getting a comprehensive overview of the file attributes.

```
ls -l
total 24232
-rw-r--r-- 1 user 197609 23777028 Jan 15 20:38 Rules_Preview.pdf
drwxr-xr-x 1 user 197609        0 Apr  9 07:46 images/
-rw-r--r-- 1 user 197609      890 Apr  9 07:48 my_file.txt
-rw-r--r-- 1 user 197609   305366 Apr  9 07:48 report.csv
-rw-r--r-- 1 user 197609   720974 Apr  9 07:47 voiceover.wav

```

#### Listing hidden files

The `-a` option includes hidden files in the listing.

{% hint style="success" %}
Hidden files in Unix/Linux systems start with a dot (e.g., `.bashrc`).
{% endhint %}

This option is helpful when you need to view or manage configuration files that are not visible by default.

```
ls -a
./  ../  .my_secret_file  Rules_Preview.pdf
images/  my_file.txt  report.csv  voiceover.wav
```

#### Human-Readable Sizes

The `-h` option makes file sizes easier to read by converting byte counts into kilobytes (K), megabytes (M), gigabytes (G), etc.

This option is particularly useful when you want to quickly assess the size of files and directories without manually converting bytes.

```
ls -lh
total 24M
-rw-r--r-- 1 user 197609  23M Jan 15 20:38 Rules_Preview.pdf
drwxr-xr-x 1 user 197609    0 Apr  9 07:51 images/
-rw-r--r-- 1 user 197609  890 Apr  9 07:48 my_file.txt
-rw-r--r-- 1 user 197609 299K Apr  9 07:48 report.csv
-rw-r--r-- 1 user 197609 705K Apr  9 07:47 voiceover.wav
```

#### Sorting by Time

The `-t` option sorts files and directories by modification time, with the most recently modified files first.

This option is useful when you want to see the most recently updated files first.

```
ls -t
images/  my_file.txt  report.csv  voiceover.wav
Rules_Preview.pdf
```

#### Reverse Order

The `-r` option reverses the order of the sort.

When used in combination with other options like `-t`, it can display the oldest files first.

This option is useful for reversing the default sorting behavior to meet specific needs.

```
ls -r
voiceover.wav  report.csv  my_file.txt  images/
Rules_Preview.pdf
```

#### Recursive Listing

The `-R` option lists directories and their contents recursively.

This is useful for viewing the entire directory tree.

```
ls -R
.:
copy_of_my_file.txt  Rules_Preview.pdf
images/  my_file.txt  myfolder/  report.csv  voiceover.wav

./images:
1.png  2.png  3.png  4.png

./myfolder:
my_file.txt  new_directory/

./myfolder/new_directory:
```

#### Sort by Size

The `-S` option sorts files by size, with the largest files first.

This is helpful for quickly identifying large files in a directory.

```
ls -S
Rules_Preview.pdf  voiceover.wav  report.csv
copy_of_my_file.txt  my_file.txt  images/  myfolder/
```

#### One File per Line

The `-1` option lists one file per line, which is useful for scripts or when piping output to other commands.

```
ls -1
Rules_Preview.pdf
images/
my_file.txt
report.csv
voiceover.wav
```

#### Directories Only

The `-d` option lists directories themselves rather than their contents.

This is useful for seeing directory names without contents.

```
ls -d */
images//  myfolder//
```

#### Append Indicator

The `-F` option appends an indicator character to entries (e.g., `/` for directories, `*` for executables).

```
ls -F
Rules_Preview.pdf  images/
my_file.txt  report.csv  voiceover.wav
```

{% hint style="success" %}

### Using Multiple Options

You can combine multiple options to create more complex commands.

For example, `ls -l -a` will display a detailed listing of all files and directories, including hidden files.

```
ls -l -a
total 24248
drwxr-xr-x 1 user 197609        0 Apr  9 07:53 ./
drwxr-xr-x 1 user 197609        0 Apr  9 07:42 ../
-rw-r--r-- 1 user 197609      890 Apr  9 07:48 .my_secret_file
-rw-r--r-- 1 user 197609 23777028 Jan 15 20:38 Rules_Preview.pdf
drwxr-xr-x 1 user 197609        0 Apr  9 07:51 images/
-rw-r--r-- 1 user 197609      890 Apr  9 07:48 my_file.txt
-rw-r--r-- 1 user 197609   305366 Apr  9 07:48 report.csv
-rw-r--r-- 1 user 197609   720974 Apr  9 07:47 voiceover.wav
```

{% endhint %}

### Changing the Directory in Linux&#x20;

The `cd` (“change directory”) command is used to change the current working directory in Linux and other Unix-like operating systems. It is one of the most basic and frequently used commands when working on the Linux terminal.

The current working directory is the directory (folder) in which the user is currently working in. Each time you interact with your command prompt, you are working within a directory.

### cd Command  <a href="#cd-command" id="cd-command"></a>

`cd` is a shell builtin, and its behavior may slightly differ from shell to shell. It uses the shell environment variables to determine necessary information for its execution.

The syntax for the `cd` command is as follows:

```
cd [OPTIONS] directory
```

In its simplest form, when used without any argument, `cd` will take you to your home directory.

{% hint style="success" %}
When navigating through the file system, you can use the `Tab` key to autocomplete the names of directories. Adding a slash at the end of the directory name is optional.
{% endhint %}

{% hint style="info" %}
To switch to a directory, you must have executable permissions for that directory.
{% endhint %}

### Absolute and Relative Path Names <a href="#absolute-and-relative-path-names" id="absolute-and-relative-path-names"></a>

When specifying a directory to change to, you can use either absolute or relative path names. The absolute or full path starts from the system root `/`, and the relative path starts from your current directory.

By default, when you log into your Linux system, your current working directory is set to your home directory. Assuming that the `Downloads` directory exists in your home directory, you can navigate to it by using the relative path to the directory:

```
cd Downloads
```

You can also navigate to the same directory by using its absolute path:

```
cd /home/username/Downloads
```

In short, if the path starts with a slash (`/`), it is the absolute path to the directory.

### The Parent Directory <a href="#the-parent-directory" id="the-parent-directory"></a>

On Unix-like operating systems, the current working directory is represented by a single dot (`.`). Two dots (`..`), one after the other, represent the parent directory or the directory immediately above the current one.

If you type `cd .`, you will change into the current directory or, in other words, the command will do nothing.

Suppose you are currently in the `/usr/local/share` directory. To switch to the `/usr/local` directory (one level up from the current directory), you would type:

```
cd ../
```

To move two levels up to the `/usr` directory (the parent’s parent), you could run the following:

```
cd ../../
```

Here is another example. Let’s say you are in the `/usr/local/share` directory, and you want to switch to the `/usr/local/src`. You can do that by typing:

```
cd ../src
```

### Navigate to the Previous Directory <a href="#navigate-to-the-previous-directory" id="navigate-to-the-previous-directory"></a>

To change back to the previous working directory, pass the dash (`-`) character as an argument to the cd command:

```
cd ~
```

For example, if you want to navigate to the `Downloads` directory, which is inside your home directory, you would type:

```
cd ~/Downloads
```

You can also navigate to another user’s home directory using the following syntax:

```
cd ~username
```

### Directories with Space in Their Names [#](https://linuxize.com/post/linux-cd-command/#directories-with-space-in-their-names) <a href="#directories-with-space-in-their-names" id="directories-with-space-in-their-names"></a>

If the directory you want to change to has spaces in its name, you should either surround the path with quotes or use the backslash (`\`) character to escape the space:

```
cd 'Dir name with space'
```

```
cd Dir\ name\ with\ space
```

By now, you should have a good understanding of what is the current working directory and how to use the `cd` command to navigate through your system’s directory structure

.

.

.

***

sources:

<https://prasha7.medium.com/journey-of-a-linux-file-part-1-what-is-a-file-202d9edea3fe>\
<https://www.geeksforgeeks.org/linux-unix/ls-command-in-linux/>\
<https://www.w3schools.com/bash/bash_ls.php>\
<https://linuxize.com/post/linux-cd-command/>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://borosan.gitbook.io/lpi-linux-essentials/2.3-using-directories-and-listing-files.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
