Menu

Log in

Sign up

From beginner to master of web design, coding, infrastructure operation, business development and marketing

  • COURSES
  • HTML & CSS Introduction
  • HTML & CSS Coding with AI
  • Linux Introduction
  • Docker Basics
  • Git & GitHub Introduction
  • JavaScript Coding with AI
  • Django Introduction
  • AWS Basics
  • Figma Introduction
  • SEO Tutorial for Beginners
  • SEO with AI
  • OTHERS
  • About
  • Terms of Service
  • Privacy Policy

© 2024 D-Libro. All Rights Reserved

Linux IntroductionChapter 2. Linux Key Commands

ln (Create Link to File and Directory)

ln (Create Link to File and Directory)

Managing Links with ln Command in Linux

The ln (LiNk) command is used to create links to files or directories. There are two types of links in Linux – symbolic link and hard link.

Symbolic Link

The symbolic link is an alias on Mac OS or a shortcut on Windows OS. It is linked with the original file or directory. Thus, when you delete the original file or directory, the symbolic link no longer works.

Creating a symbolic link

To create a symbolic link, use the ln command with the -s option. To see an example, create file_a first using the cat command.

Command Line - INPUT
cat > file_a
Command Line - INTERACTIVE
test test test

After quitting the typing mode with Ctrl + C and running the ln command to create a symbolic link to file_a.

Command Line - INPUT
ln -s file_a file_symbolic

When you run the ls -l command, you can see file details. The first letter l in the second row means that it is a symbolic link and you can see that file_symbolic is linked with file_a under the file name column.

Command Line - INPUT
ls -l
Command Line - RESPONSE
total 0
-rw-rw-r-- 1 ubuntu ubuntu 0 Jan  9 06:47 file_a
lrwxrwxrwx 1 ubuntu ubuntu 6 Jan  9 11:37 file_symbolic -> file_a

Hard Link

The concept of the hard link is a bit more complex. In the Linux file system, the actual data of each file is stored in a certain location on the hard disk. The location is described in the inode of each file. When you create a hard link of file_a, the link is connected to the inode of file_a directly. Thus, even if you delete file_a, still the link can exist unless all the hard links that are linked with the inode are deleted and no processes are using the data.

Creating a hard link

To create a hard link, use the ln command without any option. To create file_a's hard link named file_hard in the same directory, run the command below.

Command Line - INPUT
ln file_a file_hard

When you run the ls -l command, you can see file details. This time, there is no letter “l”. Instead, the number after the access mode changed from 1 to 2. This shows that there are two hard links to the file: file_a and file_hard.

Command Line - INPUT
ls -l
Command Line - RESPONSE
total 0
-rw-rw-r-- 2 ubuntu ubuntu 0 Jan  9 06:47 file_a
-rw-rw-r-- 2 ubuntu ubuntu 0 Jan  9 06:47 file_hard
lrwxrwxrwx 1 ubuntu ubuntu 6 Jan  9 11:37 file_symbolic -> file_a

You cannot create hard links to directories

To avoid the circular reference issue, you cannot create a hard link to directories. If you try to make it, you'll encounter an error message.

Command Line - INPUT
ln dir_a dir_b
Command Line - RESPONSE
ln: dir_a: hard link not allowed for directory

Deleting the original file

When you delete the original file, you may see the difference between symbolic links and hard links. When you delete file_a, file_symbolic and file_hard are still shown in the current directory; however, when you check the contents of each file by running the cat command, you can see that file_symbolic no longer exists while file_hard shows the original contents of file_a.

Command Line - INPUT
rm file_a
ls
Command Line - RESPONSE
file_hard file_symbolic
Command Line - INPUT
cat file_symbolic
Command Line - RESPONSE
cat: file_symbolic: No such file or directory
Command Line - INPUT
cat file_hard
Command Line - RESPONSE
test test test

IdeaNote: inode and inode number

In the Linux file system, the actual contents of each file are stored at a certain disk block location on the hard disk. inode, or Index NODE, is a unique identifier used to keep track of all the files’ and directories' data locations. Each inode is managed by an inode number. When you make a hard link, it records the inode number of the original file.


You can also learn this topic offline. Click AmazonKindle.

More Topics to Explore

How to Use Vim Editor for Text Editing on Linux

Chapter 3. Vim Editor

Creating and Editing Django Models

Create Django Models

Email Verification with Django Allauth: Console Method

Django Allauth (3) – Email Verification via Console

Introduction to Insert Mode in Vim Editor

Insert Mode

How to Use Cursor in Vim Editor Normal Mode

Normal Mode (1) – Move Cursor

How to Use Vim Editor for Text Editing on Linux

Chapter 3. Vim Editor

Creating and Editing Django Models

Create Django Models

Email Verification with Django Allauth: Console Method

Django Allauth (3) – Email Verification via Console

Introduction to Insert Mode in Vim Editor

Insert Mode

How to Use Cursor in Vim Editor Normal Mode

Normal Mode (1) – Move Cursor

Tags:

Hard Link

Symbolic Link

Linux Introduction
Course Content

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server

Chapter 1. Linux Basics

What Is OS?

CUI and GUI

Linux Distributions

Package Manager

Kernel and Shell

Current Working Directory

Linux Directory Structure

Absolute Path and Relative Path (Linux OS)

Linux Command Syntax

Special Characters and Escape Character

Chapter 2. Linux Key Commands

Setting Up Linux Environment on AWS

pwd (Print Working Directory)

cd (Change Directory)

ls (List Contents of Directory)

mkdir (Make Directory)

rmdir (Remove Directory)

touch (Create File)

rm (Remove File)

mv (Move File and Directory)

cp (Copy File and Directory)

cat (Display File Content)

sort (Sort File Contents)

grep (Global Regular Expression Print)

Regular Expression

find (Find File and Directory)

Wildcard

ln (Create Link to File and Directory)

Chapter 3. Vim Editor

What Is Vim and How to Launch It?

Normal, Insert and Visual Mode

Normal Mode (1) – Move Cursor

Normal Mode (2) – Delete

Normal Mode (3) – Copy and Paste

Normal Mode (4) – Undo and Redo

Normal Mode (5) – Search Phrase

Normal Mode (6) – Replace Phrase

Normal Mode (7) – Save and Exit

Insert Mode

Visual Mode

Chapter 4. User, Group and Permission

What Are User, Group And Permission in Linux?

Permission (Access Mode) by Owner Status

Superuser (Root User) vs. Normal User

sudo (Run Command with Superuser Privileges)

su (Switch User)

useradd (Add User)

passwd (Set Password)

userdel (Delete User)

Group – Primary Group and Secondary Group

groupadd (Add Group)

usermod (Modify User Account Information)

gpasswd (Add and Delete Users to Group)

groupdel (Delete Group)

chown (Change Owner of File and Directory)

chgrp (Change Group of File and Directory)

chmod (Change Access Mode)

chmod Command with Numbers

w and who (Check Current User Login Status)

id and groups (Check User ID and Group)

getent (Display User and Group Data)

Chapter 5. Redirection, Pipe and Shell Script

Standard Input Output and Redirection

Pipe (Combine Commands)

less (Display Content with Pager)

tr (Replace Characters)

cut (Extract Data Sections)

uniq (Extract Unique Data Lines)

Shell Script

echo (Echo input)

read (Read and Store Input)

Shell Variable and Environmental Variable

source (Execute Shell Script and Refresh Environmental Variables)

Chapter 6. Linux Commands for Command Management

history (Check Command History)

alias (Create Command Shortcuts)

man (Display Manual)

type, which and whereis (Display Command Information)

Package Manager Command

tree (Display Directory Tree)

Chapter 7. SSH Remote Connection

SSH (Secure Shell)

Locate .ssh Directory

SSH Remote Login (1) – Use Key Pair Generated by Server

SSH Remote Login (2) – Use Key Pair Generated by Client

SSH Config File

SSH Remote Login with Visual Studio Code

SCP (Secure Copy Protocol)

SCP with SSH Config File

SFTP (Secure File Transfer Protocol)

Other File Transfer Commands

Chapter 8. Linux Process Management

Process and Job

Foreground and Background Jobs

jobs and ps (Display Jobs and Processes)

Signals

Create, Stop and Terminate Jobs

Daemon Processes

What Is Service on Linux?

Systemd

Unit File

Systemctl Sub-Commands

Create Custom Unit and Start at Boot

Firewall

UFW (Uncomplicated Firewall)

Web Server

Launch Apache Web Server