I have a directory A with a bunch of files with .xml
extension that I need to run a search-and-replace on. There are a couple of symlinks (also with .xml
extension) in A that link to certain files in A. I tried running sed -i 's/search_regexp/replacement_string/' *.xml
but when it hits a symlink it fails with
我有一个目录A,其中包含一堆扩展名为.xml的文件,我需要运行搜索和替换。在A中有几个符号链接(也带有.xml扩展名),链接到A中的某些文件。我尝试运行sed -i的/ search_regexp / replacement_string /'*。xml但是当它遇到符号链接时它失败了
sed: ck_follow_symlink: couldn't lstat file.xml: No such file or directory
A solution would be to loop around the files that I actually want to modify and call sed on each file, but is there a way to tell sed to ignore symlinks? or just follow them and modify the linked file?
一个解决方案是循环我实际想要修改的文件并在每个文件上调用sed,但有没有办法告诉sed忽略符号链接?或者只是按照它们修改链接文件?
2 个解决方案
#1
4
@piokuc already named the option for following symlinks, here's how you can ignore them with find
first:
@piokuc已经为下面的符号链接命名了选项,以下是如何使用find来忽略它们的方法:
find /path/to/dir/ -type f -name "*.xml" ! -type l -exec sed -i 's/search_regexp/replacement_string/' {} \;
or, slightly more efficient:
或者,效率稍高:
find /path/to/dir/ -type f -name "*.xml" ! -type l | xargs sed -i 's/search_regexp/replacement_string/'
The ! -type l
part means "not anything that is a symlink"
的! -type l part表示“不是任何符号链接”
#2
2
There is an option for that (man sed
):
有一个选项(man sed):
--follow-symlinks
follow symlinks when processing in place
The version of sed I'm using is 4.1.5:
我使用的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 -->
#1
4
@piokuc already named the option for following symlinks, here's how you can ignore them with find
first:
@piokuc已经为下面的符号链接命名了选项,以下是如何使用find来忽略它们的方法:
find /path/to/dir/ -type f -name "*.xml" ! -type l -exec sed -i 's/search_regexp/replacement_string/' {} \;
or, slightly more efficient:
或者,效率稍高:
find /path/to/dir/ -type f -name "*.xml" ! -type l | xargs sed -i 's/search_regexp/replacement_string/'
The ! -type l
part means "not anything that is a symlink"
的! -type l part表示“不是任何符号链接”
#2
2
There is an option for that (man sed
):
有一个选项(man sed):
--follow-symlinks
follow symlinks when processing in place
The version of sed I'm using is 4.1.5:
我使用的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 -->