he pedals thru little events ordinary things, testing, movies, photography, tamil, linux, வைரமுத்து-தாசன், One small voice Ellesworth Monkton Toohay.
Monday, May 26, 2014
Lincoln and Benz - logo - closeup
Thursday, May 15, 2014
Tuesday, May 13, 2014
All about Linux hard links and soft links/symbolic links
1. how to create a hard link
# ln target-file1 hard-link-for-target-file1
2. you can not link two existing files. you only can create a hard link for an existing file to a new file.
# ln file1 file2
ln: creating hard link `file2' to `file1': File exists
3. when you change target file, its hard linked file also changes
4. when ls --color=auto, hardlink files appear green color:
5. how to find hard link files / to search for hardlinks:
find /path -links 3 -exec ls -ld {} \; -print
files with less than 3 links
find /path -links -3 -exec ls -ld {} \; -print
files with more than 3 links
find /path -links +3 -exec ls -ld {} \; -print
6. when you copy a hard link target/hard link file, it just copies the current file. the copied new file will not be a hard link'ed file
7. hard link target and hard links will have the same inode number. to find inode of the files: ls -li
# ls -li
total 12
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 file1
229419 -rw-rw---- 1 root esm 0 May 2 23:26 file2
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link-for-file1
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link2-for-file1
8. when you remove a hard linked file, target is still valid. (just the hardlink count will reduce by 1)
9. hard link not allowed for directory
root@sandp - TEST - MLM [/home/sandp]
# ln hard-soft-links dir1
ln: `hard-soft-links': hard link not allowed for directory
root@sandp - TEST - MLM [/home/sandp]
10. hard link can not be created on two different file systems:
root@sandp - TEST - MLM [/]
# ln /tmp/test1 /home/HL-test1
ln: creating hard link `/home/HL-test1' to `/tmp/test1': Invalid cross-device link
root@sandp - TEST - MLM [/]
11. You can also use the find command to search for symbolic links using the -type l find expression:
# find /home -type l -exec ls -l {} \; -print
12. on hard links, you can rename a file. hard links will be still valid:
Soft links vs Hard links - from wiki
############################
Soft links - Symbolic Links
############################
1. create a softlink: ln -s targetfile SL-for-targetfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln -s testfile softlink-for-testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
2. rename a softlink file --- the target file remains unchanged. the softlink still works as expected.
3. rename a softlink target file --- the softlink will not work
(to find out this one, use "ls -H", if target is not available, it will print "?" on the file permissions)
4. create a softlink between directories
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln -s SL-dir1 SL-for-dir1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
5. when long listing, the softlink for a directory will have "l" on the first letter. it will not have a "d", but its still a directory, its not a regular
file (ie, softlink for a directory is a also a "directory", not a "file")
5. ls -H and ls -l dereference
-L, --dereference
when showing file information for a symbolic link, show information for the file the link references rather than for the link itself
-H, --dereference-command-line
follow symbolic links listed on the command line
i am not sure of this -H option. but -L is useful only. if the target file is not available, it will print "question marks" (????) for the file permissions.
# ll softlink-for-testfile-renamed
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
# ls -H softlink-for-testfile-renamed
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
# ls -L softlink-for-testfile-renamed
?--------- ? ? ? ? ? softlink-for-testfile-renamed
6. softlink to a hardlinked file? (softlink to a hardlinked file is still a hardlinked file, not a softlink file)
7. how to find the softlinks
--------------------------------------
find /path -type l
find /path -type l -exec ls -l {} \; -print
8. who links to me? (from a given hard link file, how to find out what other files are hard-linked to this given file)
# find /home -samefile file1
# find /home -inum 229418
To find which files are hard links to a particular inode, you can use the find command and the -samefile option with a filename or the -inum option with an
inode number, as shown in Listing 11.
Listing 11. Finding hard links to the same file
To find which files link symbolically to a particular file, you can use the find command and the -lname option with a filename, as illustrated in Listing 12.
Links may use a relative or absolute path, so you probably want a leading asterisk in the name to find all matches.
Copying versus linking
--------------------------
Depending on what you want to accomplish, sometimes you will use links and sometimes it may be better to make a copy of a file. The major difference is that
links provide multiple names for a single file, while a copy creates two sets of identical data under two different names.
You would certainly use copies for backup and also for test purposes where you want to try out a new program without putting your operational data at risk.
You use links when you need an alias for a file (or directory), possibly to provide a more convenient or shorter path. In the next section, we'll look at
some other uses for links.
As you have seen, when you update a file, all the links to it see the update, which is not the case if you copy a file. You have also seen that symbolic
links can be broken but that subsequent write operations may create a new file. Use links with care.
the uses of softlinks:
----------------------------
1. Links, especially symbolic links, are frequently used in Linux system administration. Commands are often aliased so the user does not have to know a
version number for the current command, but can access other versions by longer names if necessary.
As shown in Listing 13, the gcc command is a symlink and there are three different names for it on my system.
ian@attic4:~$ which gcc
/usr/bin/gcc
ian@attic4:~$ ls -l /usr/bin/gcc
lrwxrwxrwx 1 root root 7 2009-12-28 23:17 /usr/bin/gcc -> gcc-4.4
ian@attic4:~$ find /usr/bin -lname "*gcc-4.4"
/usr/bin/x86_64-linux-gnu-gcc-4.4
/usr/bin/gcc
/usr/bin/x86_64-linux-gnu-gcc
Above commands executions listed here:
1. create a hard link
--------------------------
2. you can not link two existing files. you only can create a hard link for an existing file to a new file.
--------------------------------------------------------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# touch file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# touch file2
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 0
-rw-rw---- 1 root esm 0 May 2 23:26 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln file1 file2
ln: creating hard link `file2' to `file1': File exists
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 0
-rw-rw---- 1 root esm 0 May 2 23:26 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln file1 hard-link-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 0
-rw-rw---- 2 root esm 0 May 2 23:26 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rw-rw---- 2 root esm 0 May 2 23:26 hard-link-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln file1 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 0
-rw-rw---- 3 root esm 0 May 2 23:26 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rw-rw---- 3 root esm 0 May 2 23:26 hard-link-for-file1
-rw-rw---- 3 root esm 0 May 2 23:26 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# echo hi > file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 12
-rw-rw---- 3 root esm 3 May 2 23:28 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rw-rw---- 3 root esm 3 May 2 23:28 hard-link-for-file1
-rw-rw---- 3 root esm 3 May 2 23:28 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat hard-link-for-file1
hi
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
3. when you change target file, its hard linked file also changes:
--------------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# chmod 777 hard-link-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 12
-rwxrwxrwx 3 root esm 3 May 2 23:28 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link-for-file1
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
4. when ls --color=auto, hardlink files appear green color:
--------------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ls --color=auto
file1 file2 hard-link-for-file1 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
5. to find hard links / to search for hardlinks:
--------------------------------------------------------------------
find . -links 3 -exec ls -l {} \; -print
these two forms are not working as expected. `man find` does not say anything about this. looks like this feature is not available on find.
files with less than 3 links
find . -links -3 -print
files with more than 3 links
find . -links +3 -print
find . -links +3 -exec ls -l {} \; -print
find /home -links +9 ---actully listing directories as well..
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# find . -links +2 -exec ls -l {} \; -print
-rwxrwxrwx 3 root esm 3 May 2 23:28 ./hard-link2-for-file1
./hard-link2-for-file1
-rwxrwxrwx 3 root esm 3 May 2 23:28 ./file1
./file1
-rwxrwxrwx 3 root esm 3 May 2 23:28 ./hard-link-for-file1
./hard-link-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# find . -links 2 -exec ls -l {} \; -print
total 12
-rwxrwxrwx 3 root esm 3 May 2 23:28 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link-for-file1
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link2-for-file1
.
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# find . -links -2 -exec ls -l {} \; -print
-rw-rw---- 1 root esm 0 May 2 23:26 ./file2
./file2
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
6. when you copy a hard link target/hard link file, it just copies the current file. the copied new file will not be a hard link'ed file
----------------------------------------------------------------------------------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cp hard-link-for-file1 hard-link-for-file1-new
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 16
-rwxrwxrwx 3 root esm 3 May 2 23:28 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link-for-file1
-rwxrwx--- 1 root esm 3 May 2 23:53 hard-link-for-file1-new
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cp file1 cp-test
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 20
-rwxrwx--- 1 root esm 3 May 2 23:53 cp-test
-rwxrwxrwx 3 root esm 3 May 2 23:28 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link-for-file1
-rwxrwx--- 1 root esm 3 May 2 23:53 hard-link-for-file1-new
-rwxrwxrwx 3 root esm 3 May 2 23:28 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat cp-test
hi
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat hard-link-for-file1-new
hi
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
#
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# echo hello >> file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 20
-rwxrwx--- 1 root esm 3 May 2 23:53 cp-test
-rwxrwxrwx 3 root esm 9 May 2 23:54 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link-for-file1
-rwxrwx--- 1 root esm 3 May 2 23:53 hard-link-for-file1-new
-rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat cp-test
hi
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat hard-link-for-file1-new
hi
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
7. inode of the hard linked files:
-------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ls -li
total 12
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 file1
229419 -rw-rw---- 1 root esm 0 May 2 23:26 file2
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link-for-file1
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
#
8. when you remove a hard linked file, target is still valid:
----------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ls -li
total 12
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 file1
229419 -rw-rw---- 1 root esm 0 May 2 23:26 file2
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link-for-file1
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# rm hard-link-for-file1
rm: remove regular file `hard-link-for-file1'? y
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 8
-rwxrwxrwx 2 root esm 9 May 2 23:54 file1
-rw-rw---- 1 root esm 0 May 2 23:26 file2
-rwxrwxrwx 2 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ls -li
total 8
229418 -rwxrwxrwx 2 root esm 9 May 2 23:54 file1
229419 -rw-rw---- 1 root esm 0 May 2 23:26 file2
229418 -rwxrwxrwx 2 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# rm file1
rm: remove regular file `file1'? y
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ls -li
total 4
229419 -rw-rw---- 1 root esm 0 May 2 23:26 file2
229418 -rwxrwxrwx 1 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat hard-link2-for-file1
hi
hello
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
#
9. hard link not allowed for directory
---------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll -i
total 12
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 HL-file1
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 file1
229419 -rw-rw---- 1 root esm 0 May 2 23:26 file2
229418 -rwxrwxrwx 3 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cd ..
root@sandp - TEST - MLM [/home/sandp]
# ll
total 4
drwxrws--- 2 root esm 4096 May 3 00:00 hard-soft-links
root@sandp - TEST - MLM [/home/sandp]
# ln hard-soft-links dir1
ln: `hard-soft-links': hard link not allowed for directory
root@sandp - TEST - MLM [/home/sandp]
10. Invalid cross-device link
hard link can not be created on two different file systems:
----------------------------------------------------------------
root@sandp - TEST - MLM [/]
# touch /tmp/test1
root@sandp - TEST - MLM [/]
# ln /tmp/test1 /home/HL-test1
ln: creating hard link `/home/HL-test1' to `/tmp/test1': Invalid cross-device link
root@sandp - TEST - MLM [/]
11. You can also use the find command to search for symbolic links using the -type l find expression:
---------------------------------------------------------------------------------------------------------------------------
# find /home -type l -exec ls -l {} \; -print
lrwxrwxrwx 1 root esm 8 May 5 09:21 /home/esm/09119364/hard-soft-links/softlink-for-testfile-renamed -> testfile
/home/esm/09119364/hard-soft-links/softlink-for-testfile-renamed
lrwxrwxrwx 2 root esm 16 May 5 09:32 /home/esm/09119364/hard-soft-links/HL-file1-renamed-softlink-HL -> HL-file1-renamed
/home/esm/09119364/hard-soft-links/HL-file1-renamed-softlink-HL
lrwxrwxrwx 2 root esm 16 May 5 09:32 /home/esm/09119364/hard-soft-links/HL-file1-renamed-softlink -> HL-file1-renamed
/home/esm/09119364/hard-soft-links/HL-file1-renamed-softlink
lrwxrwxrwx 1 root esm 5 May 5 09:20 /home/esm/09119364/hard-soft-links/softlink-for-file1 -> file1
/home/esm/09119364/hard-soft-links/softlink-for-file1
lrwxrwxrwx 1 root esm 7 May 5 09:24 /home/esm/09119364/hard-soft-links/SL-for-dir1 -> SL-dir1
/home/esm/09119364/hard-soft-links/SL-for-dir1
12. on hard links, you can rename a file. hard links will be still valid:
-----------------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 16
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# mv HL-file1 HL-file1-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 16
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ls -li
total 16
229418 -rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
229418 -rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
229418 -rwxrwxrwx 4 root esm 9 May 2 23:54 file1
229418 -rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat HL-file1-renamed
hi
hello
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
1. create a softlink
-------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# touch testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# echo hello >> testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 20
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
-rw-rw---- 1 root esm 6 May 5 09:21 testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln -s testfile softlink-for-testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 20
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
2. rename a softlink file
----------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 20
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# mv softlink-for-testfile softlink-for-testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 20
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
3. rename a softlink target file
--------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# mv testfile testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 20
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# mv testfile-renamed testfile
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat softlink-for-testfile-renamed
hello
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# mv testfile testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat softlink-for-testfile-renamed
cat: softlink-for-testfile-renamed: No such file or directory
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
4. create a softlink between directories(when ll, will it have l or d as the first letter?)
-------------------------------------------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln -s SL-dir1 SL-for-dir1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 24
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
drwxrws--- 2 root esm 4096 May 5 09:24 SL-dir1
lrwxrwxrwx 1 root esm 7 May 5 09:24 SL-for-dir1 -> SL-dir1
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cd SL-dir1/
root@sandp - TEST - MLM [/home/sandp/hard-soft-links/SL-dir1]
# touch hi
root@sandp - TEST - MLM [/home/sandp/hard-soft-links/SL-dir1]
# cd -
/home/sandp/hard-soft-links
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll SL-for-dir1
lrwxrwxrwx 1 root esm 7 May 5 09:24 SL-for-dir1 -> SL-dir1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cd SL-for-dir1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links/SL-for-dir1]
# cd ..
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# cat SL-for-dir1
cat: SL-for-dir1: Is a directory
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
5. ls -H and ls -l dereference
-------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll -H
total 24
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
lrwxrwxrwx 2 root esm 16 May 5 09:32 HL-file1-renamed-softlink -> HL-file1-renamed
lrwxrwxrwx 2 root esm 16 May 5 09:32 HL-file1-renamed-softlink-HL -> HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
drwxrws--- 2 root esm 4096 May 5 09:25 SL-dir1
lrwxrwxrwx 1 root esm 7 May 5 09:24 SL-for-dir1 -> SL-dir1
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll -L
total 40
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed-softlink
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed-softlink-HL
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
drwxrws--- 2 root esm 4096 May 5 09:25 SL-dir1
drwxrws--- 2 root esm 4096 May 5 09:25 SL-for-dir1
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 softlink-for-file1
?--------- ? ? ? ? ? softlink-for-testfile-renamed
-rw-rw---- 1 root esm 6 May 5 09:21 testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 24
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
lrwxrwxrwx 2 root esm 16 May 5 09:32 HL-file1-renamed-softlink -> HL-file1-renamed
lrwxrwxrwx 2 root esm 16 May 5 09:32 HL-file1-renamed-softlink-HL -> HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
drwxrws--- 2 root esm 4096 May 5 09:25 SL-dir1
lrwxrwxrwx 1 root esm 7 May 5 09:24 SL-for-dir1 -> SL-dir1
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
6. softlink to a hardlinked file? (softlink to a hardlinked file is still a hardlinked file, not a softlink file)
-------------------------------------------------------------------------------------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln -s HL-file1-renamed HL-file1-renamed-softlink
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 24
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
lrwxrwxrwx 1 root esm 16 May 5 09:32 HL-file1-renamed-softlink -> HL-file1-renamed
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-test
drwxrws--- 2 root esm 4096 May 5 09:25 SL-dir1
lrwxrwxrwx 1 root esm 7 May 5 09:24 SL-for-dir1 -> SL-dir1
-rwxrwxrwx 4 root esm 9 May 2 23:54 file1
-rwxrwxrwx 4 root esm 9 May 2 23:54 hard-link2-for-file1
lrwxrwxrwx 1 root esm 5 May 5 09:20 softlink-for-file1 -> file1
lrwxrwxrwx 1 root esm 8 May 5 09:21 softlink-for-testfile-renamed -> testfile
-rw-rw---- 1 root esm 6 May 5 09:21 testfile-renamed
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ln HL-file1-renamed-softlink HL-file1-renamed-softlink-HL
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# ll
total 24
-rwxrwxrwx 4 root esm 9 May 2 23:54 HL-file1-renamed
lrwxrwxrwx 2 root esm 16 May 5 09:32 HL-file1-renamed-softlink -> HL-file1-renamed
lrwxrwxrwx 2 root esm 16 May 5 09:32 HL-file1-renamed-softlink-HL -> HL-file1-renamed
7. using find to locate softlinks
--------------------------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# find /home/sandp/ -type l
/home/sandp/hard-soft-links/softlink-for-testfile-renamed
/home/sandp/hard-soft-links/softlink-for-file1
/home/sandp/hard-soft-links/SL-for-dir1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# find /home/sandp/ -type l -exec ls -l {} \; -print
lrwxrwxrwx 1 root esm 8 May 5 09:21 /home/sandp/hard-soft-links/softlink-for-testfile-renamed -> testfile
/home/sandp/hard-soft-links/softlink-for-testfile-renamed
lrwxrwxrwx 1 root esm 5 May 5 09:20 /home/sandp/hard-soft-links/softlink-for-file1 -> file1
/home/sandp/hard-soft-links/softlink-for-file1
lrwxrwxrwx 1 root esm 7 May 5 09:24 /home/sandp/hard-soft-links/SL-for-dir1 -> SL-dir1
/home/sandp/hard-soft-links/SL-for-dir1
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
8. who links to me?
-----------------------
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# find /home -samefile file1
/home/sandp/hard-soft-links/hard-link2-for-file1
/home/sandp/hard-soft-links/file1
/home/sandp/hard-soft-links/HL-file1-renamed
/home/sandp/hard-soft-links/HL-file1-test
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
# find /home -inum 229418
/home/oracle/PerlLib/Mail/.svn
/home/sandp/hard-soft-links/hard-link2-for-file1
/home/sandp/hard-soft-links/file1
/home/sandp/hard-soft-links/HL-file1-renamed
/home/sandp/hard-soft-links/HL-file1-test
root@sandp - TEST - MLM [/home/sandp/hard-soft-links]
To find which files are hard links to a particular inode, you can use the find command and the -samefile option with a filename or the -inum option with an
inode number, as shown in Listing 11.
Listing 11. Finding hard links to the same file
To find which files link symbolically to a particular file, you can use the find command and the -lname option with a filename, as illustrated in Listing 12.
Links may use a relative or absolute path, so you probably want a leading asterisk in the name to find all matches.
ian@attic4:~$ find lpi104-6 -samefile lpi104-6/file1
lpi104-6/subdir/file3sub
lpi104-6/file3
lpi104-6/file1
ian@attic4:~$ ls -i lpi104-6/file1
1680103 lpi104-6/file1
ian@attic4:~$ find lpi104-6 -inum 1680103
lpi104-6/subdir/file3sub
lpi104-6/file3
lpi104-6/file1
Copying versus linking
--------------------------
Depending on what you want to accomplish, sometimes you will use links and sometimes it may be better to make a copy of a file. The major difference is that
links provide multiple names for a single file, while a copy creates two sets of identical data under two different names.
You would certainly use copies for backup and also for test purposes where you want to try out a new program without putting your operational data at risk.
You use links when you need an alias for a file (or directory), possibly to provide a more convenient or shorter path. In the next section, we'll look at
some other uses for links.
As you have seen, when you update a file, all the links to it see the update, which is not the case if you copy a file. You have also seen that symbolic
links can be broken but that subsequent write operations may create a new file. Use links with care.
the uses of softlinks:
----------------------------
1. Links, especially symbolic links, are frequently used in Linux system administration. Commands are often aliased so the user does not have to know a
version number for the current command, but can access other versions by longer names if necessary.
As shown in Listing 13, the gcc command is a symlink and there are three different names for it on my system.
ian@attic4:~$ which gcc
/usr/bin/gcc
ian@attic4:~$ ls -l /usr/bin/gcc
lrwxrwxrwx 1 root root 7 2009-12-28 23:17 /usr/bin/gcc -> gcc-4.4
ian@attic4:~$ find /usr/bin -lname "*gcc-4.4"
/usr/bin/x86_64-linux-gnu-gcc-4.4
/usr/bin/gcc
/usr/bin/x86_64-linux-gnu-gcc
Subscribe to:
Posts (Atom)