Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to tell sed to ignore symlinks?

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

 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?

like image 569
fo_x86 Avatar asked Dec 12 '12 15:12

fo_x86


1 Answers

@piokuc already named the option for following symlinks, here's how you can ignore them with find first:

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"

like image 104
sampson-chen Avatar answered Oct 20 '22 04:10

sampson-chen