linux sed i 大文件,sed -i 修改链接文件注意事项 破坏了原文件
因為sed -i /etc/sysconfig/selinux(selinux文件是/etc/selinux/config的軟鏈接)配置文件重啟SELINUX沒有關閉,才發現原來sed -i是不能直接修改軟鏈接文件的,如下我修改之后的后果:
[root@node1 ~]# ll /etc/sysconfig/selinux
lrwxrwxrwx. 1 root root 19 2月 20 20:34 /etc/sysconfig/selinux -> /etc/selinux/config
[root@node1 ~]# sed -i “s/SELINUX=enforcing/SELINUX=disabled/g” /etc/sysconfig/selinux
[root@node1 ~]# ll /etc/sysconfig/selinux
-rw-r–r– 1 root root 457 2月 20 22:50 /etc/sysconfig/selinux
[root@node1 ~]#
我們發現鏈接文件不再是鏈接文件了,后來查看sed?man選項時發現如下選項說明
–follow-symlinks
follow symlinks when processing in place; hard links will still be broken.
-i[SUFFIX], –in-place[=SUFFIX]
edit? files? in? place (makes backup if extension supplied).??The default operation mode is to
break symbolic and hard links.? This can be changed with –follow-symlinks and –copy.
-c, –copy
use copy instead of rename when shuffling files in -i mode.? While this? will? avoid? breaking
links? (symbolic? or hard), the resulting editing operation is not atomic.? This is rarely the
desired mode; –follow-symlinks is usually enough, and it is both faster and more secure.
以上說明就不作過多解釋了,說的很明顯,看下面實例
[root@node1 ~]# echo “test” >>test
[root@node1 ~]# ln -s ~/test ~/test_soft
[root@node1 ~]# ln ~/test ~/test_hard
[root@node1 ~]# ll -i test*
271653 -rw-r–r– 2 root root 5 2月 20 23:04 test
271653 -rw-r–r– 2 root root 5 2月 20 23:04 test_hard
271655 lrwxrwxrwx 1 root root 10 2月 20 23:04 test_soft -> /root/test
[root@node1 ~]# sed -i “s/test/hard/g” test_hard
[root@node1 ~]# sed -i “s/test/soft/g” test_soft
[root@node1 ~]# ll -i test*
271653 -rw-r–r– 1 root root 5 2月 20 23:04 test
271656 -rw-r–r– 1 root root 5 2月 20 23:05 test_hard
271657 -rw-r–r– 1 root root 5 2月 20 23:06 test_soft
[root@node1 ~]#
很明顯如man中所說-i選項對軟鏈接和硬鏈接都會使受到破壞
[root@node1 ~]# rm -rf test*
[root@node1 ~]# echo “test” >>test
[root@node1 ~]# ln -s ~/test ~/test_soft
[root@node1 ~]# ln ~/test ~/test_hard
[root@node1 ~]# ll -i test*
271653 -rw-r–r– 2 root root 5 2月 20 23:08 test
271653 -rw-r–r– 2 root root 5 2月 20 23:08 test_hard
271655 lrwxrwxrwx 1 root root 10 2月 20 23:08 test_soft -> /root/test
[root@node1 ~]# sed -i -c “s/test/soft/g” test_soft
[root@node1 ~]# sed -i -c “s/test/soft/g” test_hard
[root@node1 ~]# ll -i test*
271653 -rw-r–r– 2 root root 5 2月 20 23:11 test
271653 -rw-r–r– 2 root root 5 2月 20 23:11 test_hard
271655 lrwxrwxrwx 1 root root 10 2月 20 23:08 test_soft -> /root/test
–follow-symlinks選項只對軟鏈接有效,硬鏈接還是會被破壞,建議使用-c選項,這里就不舉例了
后來朋友在RHEL5上運行相同的操作居然沒有出現類似的現象,運行結果如下:
$ echo “test” >> test
$ ln -s ~/test ~/test1
$ ll ~/test1
lrwxrwxrwx 1 sxkj sxkj 15 02-21 13:26 /home/sxkj/test1 -> /home/sxkj/test
$ sed -i “s/test/test1/g” ~/test1
$ ll ~/test1
lrwxrwxrwx 1 sxkj sxkj 15 02-21 13:26 /home/sxkj/test1 -> /home/sxkj/test
經查是sed的版本不同造成的影響,RHEL5系列的還是使用老版本的sed,沒有–follow-symlinks類似的選項,筆者之前實驗的版本是RHEL6.3,所以出現之前的一系列問題了
總結
以上是生活随笔為你收集整理的linux sed i 大文件,sed -i 修改链接文件注意事项 破坏了原文件的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 127.0.0.1/dokuwiki/i
- 下一篇: linux 简单Shell程序设计,利用