Authors Top

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

1. Overview

When working in Linux, we can create a link to a pre-existing file. This link works as a file pointer or a file reference – essentially, it serves as a shortcut for accessing the original file.

In this tutorial, we’ll learn how to link all files from one directory to another in Linux using various techniques.

2. Sample Directory

Let’s see the list of files within the sample directory we’ll be using. We can do this with the help of the ls -l command:

$ ls -l sample
total 16
[email protected] 1 bhat  bhat  1657 Jul  9  2021 file1.rtf
[email protected] 1 bhat  bhat  1657 Jul  9  2021 file2.rtf

Our sample directory contains two files: file1.rtf and file2.rtf.

The ultimate goal of this tutorial is to populate another directory, which we’ll call samplelink, so that it contains links to all the files from the sample directory. But first, let’s learn a little more about soft links.

3. Creating a Soft Link

A soft link is a file generated to contain the actual file path. It does not hold the contents of the original file. We can create a soft link using the ln command.

Let’s now create a soft link for the file1.rtf file, which is within our sample directory:

$ ln -s file1.rtf linkfile1

Using the above command, we have generated a soft link called linkfile1 that points to the file1.rtf file. The ln command with the -s option creates a soft link for a file.

We can check the output with the help of the ls -l command:

$ ls -l
total 16
[email protected] 1 bhat  bhat 1657 Jul  9  2021 file1.rtf
[email protected] 1 bhat  bhat 1657 Jul  9  2021 file2.rtf
lrwxr-xr-x  1 bhat  bhat    5 Jan 22 23:15 linkfile1.rtf -> file1.rtf

Also, once a soft link file is created, it’ll have a different inode number than the actual file. We can see this with the help of the ls -i command:

$ ls -i
12746289 file1.rtf	12745765 file2.rtf	12747072 linkfile1.rtf

Alternatively, we can create a soft link without even providing a name for the link. In this approach, the ln command generates a soft link – in the current working directory – that has the exact same name as the target file.

Let’s now create a soft link for the file1.rtf file, which is within our sample directory:

$ ln -s /sample/file1.rtf

Here, we have generated a soft link for the file1.rtf file.

Let’s have a look at our results:

$ ls -l
lrwxr-xr-x  1 bhat  bhat   17 Jan 30 23:32 file1.rtf -> /sample/file1.rtf

However, we must keep in mind that the above example won’t give the desired result if we run it from inside the sample directory.

Let’s execute the same command again within the sample directory:

$ cd sample
$ ln -s /sample/file1.rtf
ln: ./file1.rtf: File exists

In the above example, we see that there has been no creation of a soft link for the file1.rtf file, and we’ve received the “File exists” message as output.

4. Link Every File from One Directory

Let’s discuss some common solutions for linking all files from one directory to another directory.

4.1. Using the ln -s Command

We can create multiple links at once for files in a directory and store them in another directory with the help of the ln -s command.

We’ll now generate soft links for the files file1.rtf and file2.rtf (which are within our sample directory) inside another directory called samplelink:

$ ln -s sample/* samplelink

Using the above command, we’ve generated soft links for all the files within the sample directory. The (*) represents all files inside the sample directory. The samplelink directory should now contain links to every file from the sample directory:

$ ls -l samplelink
total 0
lrwxr-xr-x  1 bhat  bhat  16 Jan 23 22:57 file1.rtf -> sample/file1.rtf
lrwxr-xr-x  1 bhat  bhat  16 Jan 23 22:57 file2.rtf -> sample/file2.rtf

4.2. Using the ln -fs Command

We can also link all files from one directory to another directory using the ln -fs command:

$ ln -fs  sample/* samplelink

This time, we’ve created soft links for all the files (denoted by sample/*) that reside in the sample directory. Here, we’ve parsed the command-line options (-f and -s). This is done by fusing more than one single letter option into one option string.

The ln command with option -f deletes pre-existing destination files, and the -s option creates the soft links.

Let’s have a look at our results:

$ ls -l samplelink
total 0
lrwxr-xr-x  1 bhat  bhat  16 Jan 24 14:27 file1.rtf -> sample/file1.rtf
lrwxr-xr-x  1 bhat  bhat  16 Jan 24 14:27 file2.rtf -> sample/file2.rtf

We must note that none of the approaches discussed in this section will work for any hidden files in a directory. 

5. Conclusion

In this article, we learned how to link all files from one directory to another using the ln command. In addition, we reviewed how to create a soft link for a file in Linux.

Authors Bottom

If you have a few years of experience in the Linux ecosystem, and you’re interested in sharing that experience with the community, have a look at our Contribution Guidelines.

Comments are closed on this article!