有没有办法告诉sed忽略符号链接?

时间:2012-12-12 15:06:48

标签: linux bash shell unix command-line

我有一个目录A,其中包含一些.xml扩展名的文件,我需要在其上运行搜索和替换。 A中有几个符号链接(也带有.xml扩展名),链接到A中的某些文件。我尝试运行sed -i 's/search_regexp/replacement_string/' *.xml但是当它遇到符号链接时,它会失败

 sed: ck_follow_symlink: couldn't lstat file.xml: No such file or directory

解决方案是循环我实际想要修改的文件并在每个文件上调用sed,但有没有办法告诉sed忽略符号链接?或者只是按照它们修改链接文件?

2 个答案:

答案 0 :(得分:4)

@piokuc已经为下面的符号链接命名了选项,以下是find首先忽略它们的方法:

find /path/to/dir/ -type f -name "*.xml" ! -type l -exec sed -i 's/search_regexp/replacement_string/' {} \;

或稍高效:

find /path/to/dir/ -type f -name "*.xml" ! -type l | xargs sed -i 's/search_regexp/replacement_string/'

! -type l部分表示“不是任何符号链接”

答案 1 :(得分:2)

有一个选项(man sed):

       --follow-symlinks

          follow symlinks when processing in place

我正在使用的sed版本是4.1.5:

ariadne{/tmp}:310 --> sed --help | grep follow
  --follow-symlinks
                 follow symlinks when processing in place
ariadne{/tmp}:311 --> sed --version           
GNU sed version 4.1.5
Copyright (C) 2003 Free Software Foundation, Inc.
This is free software; see the source for copying conditions.  There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE,
to the extent permitted by law.
ariadne{/tmp}:312 --> uname -a
Linux ariadne 2.6.34.10-0.6-desktop #1 SMP PREEMPT 2011-12-13 18:27:38 +0100 x86_64 x86_64 x86_64 GNU/Linux
ariadne{/tmp}:313 --> 
相关问题