Table of Contents

ln and unlink - How to use ln and unlink command in Linux with examples



Format

ln -s SourceFile  TargetFile
ln -s SourceFile


Successful operation

[kurihara@centos7 tmp]$ echo aaa > org
[kurihara@centos7 tmp]$ cat org
aaa
[kurihara@centos7 tmp]$
-rw-rw-r-- 1 kurihara kurihara  0 Jun 20 23:36 org
[kurihara@centos7 tmp]$ ln -s org det
[kurihara@centos7 tmp]$ ls -lh
lrwxrwxrwx 1 kurihara kurihara  3 Jun 20 23:36 det -> org
[kurihara@centos7 tmp]$ cat org
aaa
[kurihara@centos7 tmp]$
[kurihara@centos7 tmp]$ cat dst
aaa
[kurihara@centos7 tmp]$ unlink det
[kurihara@centos7 tmp]$ ls -lh
-rw-rw-r-- 1 kurihara kurihara  0 Jun 20 23:36 org
[kurihara@centos7 tmp]$ 


Failed operation

[kurihara@centos7 tmp]$ echo aaa > org
[kurihara@centos7 tmp]$ cat org
aaa
[kurihara@centos7 tmp]$
-rw-rw-r-- 1 kurihara kurihara  0 Jun 20 23:36 org
[kurihara@centos7 tmp]$ ln -s org det
[kurihara@centos7 tmp]$ ls -lh
lrwxrwxrwx 1 kurihara kurihara  3 Jun 20 23:36 det -> org
[kurihara@centos7 tmp]$ unlink org
[kurihara@centos7 tmp]$ ls -lh
lrwxrwxrwx 1 kurihara kurihara  3 Jun 20 23:44 dst -> org
[kurihara@centos7 tmp]$ cat dst
cat: dst: No such file or directory
[kurihara@centos7 tmp]$ cat org
cat: org: No such file or directory
[kurihara@centos7 tmp]$


ln -s SourceFile

kurihara@hostname:/tmp$ mkdir test1
kurihara@hostname:/tmp$ mkdir test2
kurihara@hostname:/tmp$
kurihara@hostname:/tmp$ cd test1/
kurihara@hostname:/tmp/test1$ touch test.txt
kurihara@hostname:/tmp/test1$
kurihara@hostname:/tmp/test1$ ls -lh
total 0
-rw-rw-r-- 1 kurihara kurihara 0 Mar 11 23:44 test.txt
kurihara@hostname:/tmp/test1$ cd ../test2/
kurihara@hostname:/tmp/test2$ ln -s ../test1.txt
kurihara@hostname:/tmp/test2$ ls -lh
total 0
lrwxrwxrwx 1 kurihara kurihara 12 Mar 11 23:45 test1.txt -> ../test1.txt
kurihara@hostname:/tmp/test2$