Introduction
In this lab, you will learn how to use the Linux lndir command to create and manage symbolic links to a directory tree. The lndir command is particularly useful when you want to create a mirror of a directory structure without duplicating the actual files. You will start by understanding the purpose of the lndir command, then create symbolic links using it, and finally learn how to manage those symbolic links. This lab covers practical examples and use cases for the lndir command, which is a valuable tool for maintaining consistent directory structures across multiple locations.
Understand the Purpose of lndir Command
In this step, you will learn about the purpose of the lndir command in Linux. The lndir command is used to create symbolic links to a directory tree. It is particularly useful when you want to create a mirror of a directory structure without duplicating the actual files.
To begin, let's create a sample directory structure that we will use throughout this lab:
mkdir -p ~/project/source ~/project/destination
touch ~/project/source/file1.txt ~/project/source/file2.txt
Example output:
labex@ubuntu:~/project$ mkdir -p ~/project/source ~/project/destination
labex@ubuntu:~/project$ touch ~/project/source/file1.txt ~/project/source/file2.txt
The lndir command creates symbolic links to all the files and subdirectories in the source directory, allowing you to access the files in the destination directory as if they were physically present there. This is particularly useful when you want to share a directory structure across multiple locations without duplicating the actual files.
To create a symbolic link using lndir, run the following command:
lndir ~/project/source ~/project/destination
Example output:
labex@ubuntu:~/project$ lndir ~/project/source ~/project/destination
After running this command, you should see that the ~/project/destination directory now contains symbolic links to the files in the ~/project/source directory.
ls -l ~/project/destination
Example output:
labex@ubuntu:~/project$ ls -l ~/project/destination
total 0
lrwxrwxrwx 1 labex labex 23 Apr 11 12:34 file1.txt -> ../source/file1.txt
lrwxrwxrwx 1 labex labex 23 Apr 11 12:34 file2.txt -> ../source/file2.txt
The lndir command is a useful tool for managing symbolic links and maintaining a consistent directory structure across multiple locations.
Create Symbolic Links Using lndir
In this step, you will learn how to use the lndir command to create symbolic links to a directory tree.
First, let's create a new directory structure that we will use for this step:
mkdir -p ~/project/source2 ~/project/destination2
touch ~/project/source2/file3.txt ~/project/source2/file4.txt
Example output:
labex@ubuntu:~/project$ mkdir -p ~/project/source2 ~/project/destination2
labex@ubuntu:~/project$ touch ~/project/source2/file3.txt ~/project/source2/file4.txt
Now, let's create symbolic links in the ~/project/destination2 directory using the lndir command:
lndir ~/project/source2 ~/project/destination2
Example output:
labex@ubuntu:~/project$ lndir ~/project/source2 ~/project/destination2
To verify that the symbolic links were created, let's list the contents of the ~/project/destination2 directory:
ls -l ~/project/destination2
Example output:
labex@ubuntu:~/project$ ls -l ~/project/destination2
total 0
lrwxrwxrwx 1 labex labex 23 Apr 11 12:34 file3.txt -> ../source2/file3.txt
lrwxrwxrwx 1 labex labex 23 Apr 11 12:34 file4.txt -> ../source2/file4.txt
As you can see, the lndir command has created symbolic links to the files in the ~/project/source2 directory within the ~/project/destination2 directory.
Manage Symbolic Links with lndir
In this final step, you will learn how to manage the symbolic links created using the lndir command.
First, let's create a new file in the ~/project/source2 directory:
touch ~/project/source2/file5.txt
Example output:
labex@ubuntu:~/project$ touch ~/project/source2/file5.txt
Now, let's update the symbolic links in the ~/project/destination2 directory to include the new file:
lndir ~/project/source2 ~/project/destination2
Example output:
labex@ubuntu:~/project$ lndir ~/project/source2 ~/project/destination2
To verify that the symbolic links have been updated, let's list the contents of the ~/project/destination2 directory again:
ls -l ~/project/destination2
Example output:
labex@ubuntu:~/project$ ls -l ~/project/destination2
total 0
lrwxrwxrwx 1 labex labex 23 Apr 11 12:34 file3.txt -> ../source2/file3.txt
lrwxrwxrwx 1 labex labex 23 Apr 11 12:34 file4.txt -> ../source2/file4.txt
lrwxrwxrwx 1 labex labex 23 Apr 11 12:34 file5.txt -> ../source2/file5.txt
As you can see, the lndir command has updated the symbolic links in the ~/project/destination2 directory to include the new file5.txt file.
You can also use the lndir command to remove symbolic links. For example, to remove the symbolic link to file4.txt, you can run the following command:
rm ~/project/destination2/file4.txt
Example output:
labex@ubuntu:~/project$ rm ~/project/destination2/file4.txt
After running this command, the file4.txt symbolic link will be removed from the ~/project/destination2 directory.
Summary
In this lab, you learned about the purpose of the lndir command in Linux, which is used to create symbolic links to a directory tree. This is particularly useful when you want to create a mirror of a directory structure without duplicating the actual files. You also learned how to use the lndir command to create symbolic links to a directory, allowing you to access the files in the destination directory as if they were physically present there. This is a useful tool for managing symbolic links and maintaining a consistent directory structure across multiple locations.



