# Linux commands: ln

> Learn how the Linux ln command creates links to files: hard links that share content, and soft links made with -s that can point to directories too.

Author: [Flavio Copes](https://flaviocopes.com/about/) | Published: 2020-09-05 | Topics: [CLI](https://flaviocopes.com/tags/cli/) | Canonical: https://flaviocopes.com/linux-command-ln/

The `ln` command is part of the Linux file system commands.

It's used to create links. What is a link? It's like a pointer to another file. A file that points to another file. You might be familiar with Windows shortcuts. They're similar.

We have 2 types of links: **hard links** and **soft links**.

## Hard links

Hard links are rarely used. They have a few limitations: you can't link to directories, and you can't link to external filesystems (disks).

A hard link is created using

```bash
ln <original> <link>
```

For example, say you have a file called recipes.txt. You can create a hard link to it using:

```bash
ln recipes.txt newrecipes.txt
```

The new hard link you created is indistinguishable from a regular file:

![Terminal showing ls -al newrecipes.txt output displaying a regular file with standard permissions](https://flaviocopes.com/images/linux-command-ln/Screen_Shot_2020-09-02_at_11.26.21.png)

Now any time you edit any of those files, the content will be updated for both.

If you delete the original file, the link will still contain the original file content, as that's not removed until there is one hard link pointing to it.

![Terminal showing hard link creation and file access after deleting original, demonstrating content preservation](https://flaviocopes.com/images/linux-command-ln/Screen_Shot_2020-09-02_at_11.26.07.png)

## Soft links

Soft links are different. They are more powerful as you can link to other filesystems and to directories, but when the original is removed, the link will be broken.

You create soft links using the `-s` option of `ln`:

```bash
ln -s <original> <link>
```

For example, say you have a file called recipes.txt. You can create a soft link to it using:

```bash
ln -s recipes.txt newrecipes.txt
```

In this case you can see there's a special `l` flag when you list the file using `ls -al`, and the file name has a `@` at the end, and it's colored differently if you have colors enabled:

![Terminal showing ls -al output for soft link with l flag and arrow pointing to target file](https://flaviocopes.com/images/linux-command-ln/Screen_Shot_2020-09-02_at_11.27.18.png)

Now if you delete the original file, the links will be broken, and the shell will tell you "No such file or directory" if you try to access it:

![Terminal showing broken soft link error message No such file or directory when accessing deleted target](https://flaviocopes.com/images/linux-command-ln/Screen_Shot_2020-09-02_at_11.27.03.png)

> This command works on Linux, macOS, WSL, and anywhere you have a UNIX environment
