5

我有一个目录 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 忽略符号链接?或者只是跟随他们并修改链接的文件?

4

2 回答 2

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部分的意思是“不是任何符号链接”

于 2012-12-12T15:25:50.167 回答
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 --> 
于 2012-12-12T15:10:55.213 回答