软连接ln

软连接ln

[root@even tmp]# ln --help
Usage: ln [OPTION]... [-T] TARGET LINK_NAME (1st form)
or: ln [OPTION]... TARGET (2nd form)
or: ln [OPTION]... TARGET... DIRECTORY (3rd form)
or: ln [OPTION]... -t DIRECTORY TARGET... (4th form)
In the 1st form, create a link to TARGET with the name LINK_NAME.
In the 2nd form, create a link to TARGET in the current directory.
In the 3rd and 4th forms, create links to each TARGET in DIRECTORY.
Create hard links by default, symbolic links with --symbolic.
When creating hard links, each TARGET must exist.

Mandatory arguments to long options are mandatory for short options too.
--backup[=CONTROL] make a backup of each existing destination file
-b like --backup but does not accept an argument
-d, -F, --directory allow the superuser to attempt to hard link
directories (note: will probably fail due to
system restrictions, even for the superuser)
-f, --force remove existing destination files
-n, --no-dereference treat destination that is a symlink to a
directory as if it were a normal file
-i, --interactive prompt whether to remove destinations
-s, --symbolic make symbolic links instead of hard links
-S, --suffix=SUFFIX override the usual backup suffix
-t, --target-directory=DIRECTORY specify the DIRECTORY in which to create
the links
-T, --no-target-directory treat LINK_NAME as a normal file
-v, --verbose print name of each file before linking
--help display this help and exit
--version output version information and exit

The backup suffix is~’, unless set with --suffix or SIMPLE_BACKUP_SUFFIX.The version control method may be selected via the --backup option or throughthe VERSION_CONTROL environment variable. Here are the values:`

none, off never make backups (even if --backup is given)
numbered, t make numbered backups
existing, nil numbered if numbered backups exist, simple otherwise
simple, never always make simple backups

Report bugs to <bug-coreutils@gnu.org>.

创建

ln -s [含有访问目标的文件或者目录] [自定义的软连接名称,可以是目录也可以是文件]

[oracle@even testmount]$ pwd
/u01/testmount
[oracle@even testmount]$ ls -l
total 8
-rw-r--r-- 1 oracle oinstall 9 Jun 3 15:13 1.sh
-rw-r--r-- 1 oracle oinstall 6 Jun 3 15:13 2.sql
-rw-r--r-- 1 oracle oinstall 0 Jun 3 15:13 3.txt

[oracle@even testmount]$ **ln -s /u01/testmount /tmp/ln_access**
[oracle@even testmount]$ ll
total 8
-rw-r--r-- 1 oracle oinstall 9 Jun 3 15:13 1.sh
-rw-r--r-- 1 oracle oinstall 6 Jun 3 15:13 2.sql
-rw-r--r-- 1 oracle oinstall 0 Jun 3 15:13 3.txt
[oracle@even testmount]$ cd /tmp
[oracle@even tmp]$ ll
lrwxrwxrwx 1 oracle oinstall 14 Jun 3 16:16 ln_access -> /u01/testmount
[oracle@even tmp]$ cd ln_access
[oracle@even ln_access]$ ll
total 8
-rw-r--r-- 1 oracle oinstall 9 Jun 3 15:13 1.sh
-rw-r--r-- 1 oracle oinstall 6 Jun 3 15:13 2.sql
-rw-r--r-- 1 oracle oinstall 0 Jun 3 15:13 3.txt

压缩和解压

tar 打包默认不会备份目录下软连接指向的内容,需要加h参数,但要估算软连接指向的目录和文件的数据量。

如果tar没有加h参数后,利用tar包恢复前还删除了原来引用的目录,即使恢复了tar包,软连接的参照文件已经恢复不回来了。

定期对/tmp目录进行打包压缩然后清理

[root@even home]# tar -czvf tmp.tar.gz /tmp
tar: Removing leading `/’ from member names

[root@even home]# tar -tf tmp.tar.gz 发现安装包里没有软连接的内容
tmp/
tmp/hsperfdata_oracle/
tmp/hsperfdata_oracle/15778
tmp/hsperfdata_oracle/25244
tmp/hsperfdata_oracle/15849
tmp/hsperfdata_oracle/15713
tmp/.font-unix/
tmp/ln_access 软连接本身还存在的,恢复tar包后软连接还在,但是否有效取决于引用的目录是否还在
tmp/.gdmDDWXH0
tmp/.X11-unix/
tmp/.X0-lock
tmp/gconfd-root/
tmp/gconfd-root/lock/
tmp/gconfd-root/lock/ior
tmp/.ICE-unix/
tmp/keyring-UyqNLP/
tmp/orbit-root/
tmp/orbit-root/bonobo-activation-register.lock
tmp/orbit-root/bonobo-activation-server-ior
tmp/keyring-UGeWOK/
tmp/ssh-yWwGqo3814/

模拟引用的源目录被人清理了,试图利用tar 解压恢复备份,tar 中的-P参数表示允许保留绝对路径,即允许备份数据中含有根目录 ‘/’,不管是打包还是解包都可以。

应对如下报错情况

[oracle@even ~]$ tar -xvf tmp.tar
/tmp/
tar: Removing leading `/' from member names
`[oracle@even u01]$ rm -rf    /u01/testmount`

`[oracle@even ~]$ tar -xvPf tmp.tar`
`/tmp/`
`/tmp/hsperfdata_oracle/`
`/tmp/hsperfdata_oracle/15778`
`/tmp/hsperfdata_oracle/25244`
`/tmp/hsperfdata_oracle/15849`
`/tmp/hsperfdata_oracle/15713`
`/tmp/test/`
`/tmp/test/ln_access`
`/tmp/test/test.sh`
`/tmp/gconfd-root/`
`/tmp/gconfd-root/lock/`
`/tmp/gconfd-root/lock/ior`
`/tmp/gconfd-root/hsperfdata_oracle/`
`/tmp/gconfd-root/hsperfdata_oracle/15778`
`/tmp/gconfd-root/hsperfdata_oracle/25244`
`/tmp/gconfd-root/hsperfdata_oracle/15849`
`/tmp/gconfd-root/hsperfdata_oracle/15713`
`/tmp/gconfd-root/ln_access`
`/tmp/gconfd-root/keyring-UyqNLP/`
`/tmp/gconfd-root/orbit-root/`
`/tmp/gconfd-root/orbit-root/bonobo-activation-register.lock`
`/tmp/gconfd-root/orbit-root/bonobo-activation-server-ior`
`/tmp/gconfd-root/keyring-UGeWOK/`
`/tmp/gconfd-root/tmp/`
`/tmp/gconfd-root/ssh-yWwGqo3814/`
`/tmp/keyring-UyqNLP/`
`/tmp/orbit-root/`
`/tmp/orbit-root/bonobo-activation-register.lock`
`/tmp/orbit-root/bonobo-activation-server-ior`
`/tmp/keyring-UGeWOK/`
`/tmp/ssh-yWwGqo3814/`
`[oracle@even ~]$ cd /tmp/test`
`[oracle@even test]$ ll`
`total 4`
`lrwxrwxrwx 1 oracle oinstall 14 Jun  3 18:03 ln_access -> /u01/testmount`
`-rw-r--r-- 1 oracle oinstall  5 Jun  3 17:49 test.sh

这时候发现即使恢复了软连接,但是这个软连接处于一直闪烁的状态,表明已经失效了,那么tar最初备份的软连接指向的内容最终还是丢失了

打包时加上h参数解决软连接没有复制的情况,这种应对于防止软连接指向的原来目录被人清空

但如果软连接的目录是个海量目录就不建议了!!!

[root@even home]# tar -chzvf tmp.tar.gz /tmp

[root@even home]# tar -tf tmp.tar.gz
tmp/
tmp/hsperfdata_oracle/
tmp/hsperfdata_oracle/15778
tmp/hsperfdata_oracle/25244
tmp/hsperfdata_oracle/15849
tmp/hsperfdata_oracle/15713
tmp/.font-unix/
tmp/ln_access/
tmp/ln_access/2.sql
tmp/ln_access/1.sh
tmp/ln_access/3.txt
tmp/.gdmDDWXH0
tmp/.X11-unix/
tmp/.X0-lock
tmp/gconfd-root/
tmp/gconfd-root/lock/
tmp/gconfd-root/lock/ior
tmp/.ICE-unix/
tmp/keyring-UyqNLP/
tmp/orbit-root/
tmp/orbit-root/bonobo-activation-register.lock
tmp/orbit-root/bonobo-activation-server-ior
tmp/keyring-UGeWOK/
tmp/ssh-yWwGqo3814/

查找软连接

[root@even tmp]# ls -l|grep ^l
lrwxrwxrwx 1 oracle oinstall 14 Jun 3 16:16 ln_access -> /u01/testmount

[root@even /]# find /tmp -type l
/tmp/ln_access

删除软连接

进入查找到软连接的目录,利用rm -f当作文件删除即可

cd /tmp

rm -f ln_access

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值