# 5.4 Special Directories and Files

### **5.4 Special Directories and Files**

**Weight:** 1

**Description:** Special directories and files on a Linux system including special permissions.

**Key Knowledge Areas:**

* Using temporary files and directories
* Symbolic links

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

* /tmp/, /var/tmp/ and Sticky Bit
* ls -d
* ln -s

### Introducing links <a href="#introducing-links" id="introducing-links"></a>

On a storage device, a file or directory is stored in a collection of blocks. Information about a file is held in an *inode*, which records information such as the owner, when the file was last accessed, how large it is, whether it is a directory or not, and who can read from or write to it.

A *directory entry* contains a name for a file or directory and a pointer to the inode where the information about the file or directory is stored.

<figure><img src="/files/TrnYV3qAWpCvjjzxshSO" alt=""><figcaption></figcaption></figure>

> The inode number is unique within a particular filesystem.

```
[payam@earth Working]$ ls -li
total 4
152641881 drwxr-xr-x. 2 payam payam  6 Dec 15 08:54 dir1
207052714 drwxr-xr-x. 2 payam payam  6 Dec 15 08:54 dir2
134372472 -rw-r-xr-x. 1 payam payam  0 Dec 15 08:54 file1
152514751 -rw-r--r--. 1 payam payam  0 Dec 15 08:54 file2
137345522 -rwxr-xr-x. 1 payam payam 30 Dec  8 11:58 myscript.sh

```

> **`-i`**  switch print the index number of each file

{% hint style="info" %}

### **Whats is link ?**&#x20;

A link is simply an additional directory entry for a file or directory, allowing two or more names for the same thing.
{% endhint %}

There are two types of links : **Hard Link**  (LPIC1) and **Soft Link**.&#x20;

### Soft link

a **soft link** or *symbolic link* is a directory entry that points to an inode that provides the name of another directory entry. Symbolic links are also called *symlinks*.

<figure><img src="/files/UPUh03fbQAX9qv4fqegI" alt=""><figcaption></figcaption></figure>

### Creating links

### ln

`ln` command with the `-s` option creates soft links. Soft links use file or directory names, which may be relative or absolute. If you are using relative names, you will usually want the current working directory to be the directory where you are creating the link. Otherwise, the link you create will be relative to another point in the file system.

```
### For  Soft link 
ln  -s FILE LINK
```

* If both the `FILE` and `LINK` are given, `ln` will create a link from the file specified as the first argument (`FILE`) to the file specified as the second argument (`LINK`).
* If only one file is given as an argument or the second argument is a dot (`.`), `ln` will create a link to that file in the current working directory . The symlink will have the same name as the file it points to.

By default, on success, `ln` doesn’t produce any output and returns zero.

#### Creating Symlink To a File <a href="#creating-symlink-to-a-file" id="creating-symlink-to-a-file"></a>

To create a symbolic link to a given file, open your terminal and type:

```
ln -s source_file symbolic_link
```

example:

```
[payam@earth Working]$ ls -l
total 4
drwxr-xr-x. 2 payam payam  6 Dec 15 08:54 dir1
drwxr-xr-x. 2 payam payam  6 Dec 15 08:54 dir2
-rw-r-xr-x. 1 payam payam  0 Dec 15 08:54 file1
-rw-r--r--. 1 payam payam  0 Dec 15 08:54 file2
-rw-r--r--. 1 payam payam  0 Dec 15 13:51 my_file.txt
-rwxr-xr-x. 1 payam payam 30 Dec  8 11:58 myscript.sh

[payam@earth Working]$ ln -s my_file.txt my_link.txt

```

To verify that the symlink was successfully created, use the `ls -l` command:

```
[payam@earth Working]$ ls -l
total 4
drwxr-xr-x. 2 payam payam  6 Dec 15 08:54 dir1
drwxr-xr-x. 2 payam payam  6 Dec 15 08:54 dir2
-rw-r-xr-x. 1 payam payam  0 Dec 15 08:54 file1
-rw-r--r--. 1 payam payam  0 Dec 15 08:54 file2
-rw-r--r--. 1 payam payam  0 Dec 15 13:51 my_file.txt
lrwxrwxrwx. 1 payam payam 11 Dec 15 13:51 my_link.txt -> my_file.txt
-rwxr-xr-x. 1 payam payam 30 Dec  8 11:58 myscript.sh
```

The `l` character is a file type flag that represents a symbolic link. The `->` symbol shows the file the symlink points to.

#### Creating Symlinks To a Directory <a href="#creating-symlinks-to-a-directory" id="creating-symlinks-to-a-directory"></a>

The command for creating a symbolic link to a directory is the same as when creating a symbolic link to a file. Specify the directory name as the first parameter and the symlink as the second parameter.

For example, if you want to create a symbolic link from the `/mnt/my_drive/movies` directory to the `~/my_movies` directory you would run:

```con
ln -s /mnt/my_drive/movies ~/my_movies
```

#### Overwriting Symlinks <a href="#overwriting-symlinks" id="overwriting-symlinks"></a>

If you attempt to create a symbolic link that already exists , the `ln` command will output an error message.

```
ln -s my_file.txt my_link.txt
```

```output
ln: failed to create symbolic link 'my_link.txt': File exists
```

To overwrite the destination path of the symlink, use the `-f` (`--force`) option.

```
ln -sf my_file.txt my_link.txt
```

### Removing Symlinks <a href="#removing-symlinks" id="removing-symlinks"></a>

To delete/remove symbolic links , use either the `unlink` or `rm` command.

The syntax of the `unlink` is very simple:

```sh
unlink symlink_to_remove
```

Removing a symbolic link using the `rm` command is the same as when removing a file:

```sh
rm symlink_to_remove
```

{% hint style="warning" %}
No matter which command you use, when removing a symbolic link do not append the `/` trailing slash at the end of its name.
{% endhint %}

Removing soft link doesn't affect anything but removing original file, the link becomes "dangling" link which points to nonexistent file.

If you delete or move the source file to a different location, the symbolic file will be left dangling (broken) and should be removed.

***

### sticky bit

The Linux sticky bit is a special directory permission preventing users from deleting or renaming files they don't own, even if they have write access to the directory, making it an "append-only" folder. It's commonly used on `/tmp` to let many users write files, but stop them from deleting others' files, represented as a 't' in `ls -l` output (e.g., `rwxrwxrwt`).

We can see if a directory contains the sticky bit permission by running the `ls` command to check the directory’s permissions:

```
[payam@earth Working]$ ls -ld /tmp/
drwxrwxrwt. 30 root root 4096 Dec 15 14:19 /tmp/
```

As we can see from the output, the `/tmp` directory contains permissions `rwxrwxrwt`. You should be used to seeing the read, write, and execute permissions – represented by `r`, `w`, and `x`, respectively – but the `t` at the end of those permissions indicates that this directory has sticky bit permissions.

{% hint style="success" %}

### Why Do We Need the Sticky Bit Permission?

There are certain directories on Linux which many users need access to. In order to grant users full access to a directory, they will need read, write, and execute permissions. This is true for the case of directories like `/tmp`, where many users (including non human system user accounts) write temporary data to. However, at the same time, we do not want other users interfering with the files of other users in that directory. This is the kind of niche scenario where the sticky bit becomes useful.

For this reason, there is a need for some mechanism to prevent users who do not own the directory or the actual files within the directory from renaming or removing another user’s files. This mechanism is called “Sticky Bit”. Sticky bit only allows root, directory owner and file owner to rename and delete files.
{% endhint %}

### How to Set Sticky Bit Permissions

Setting the sticky bit permission on a Linux directory is very simple and can be done using the chmod command.

```
[payam@earth Working]$ ls -ld dir1
drwxr-xr-x. 2 payam payam 6 Dec 15 08:54 dir1

[payam@earth Working]$ chmod +t dir1

[payam@earth Working]$ ls -ld dir1
drwxr-xr-t. 2 payam payam 6 Dec 15 08:54 dir1

```

{% hint style="info" %}
**if Sticky Bit set on a file does nothing but if is set on a direcotry  only owner can delete files**
{% endhint %}

that's all.

### Congratulation we have done lpi Linux Essentials course !!! do not forget to give a [star](https://github.com/Borosan) and [donate](http://linuxcert.ir/donation.html) :-) <a href="#congratulation-we-have-done-lpic1-102-do-not-forget-to-give-a-star-and-donate" id="congratulation-we-have-done-lpic1-102-do-not-forget-to-give-a-star-and-donate"></a>

### You can start studying my LPIC-1 book: <https://borosan.gitbook.io/lpic1-exam-guide/> <a href="#you-can-start-studying-my-lpic-2-book-https-borosan.gitbook.io-lpic2-exam-guide" id="you-can-start-studying-my-lpic-2-book-https-borosan.gitbook.io-lpic2-exam-guide"></a>

.

.

.

***

sources:

[https://app.gitbook.com/o/s9nDBEc3KAeK4VdvtgS3/s/ohrzk4YVVEIEOw3rrQKr/\~/edit/\~/changes/87/5.4-special-directories-and-files](/lpi-linux-essentials/5.4-special-directories-and-files.md)\
<https://linuxize.com/post/how-to-create-symbolic-links-in-linux-using-the-ln-command/>\
<https://linuxconfig.org/explaining-the-sticky-bit-what-the-t-in-linux-directory-permissions-means>


---

# 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/5.4-special-directories-and-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.
