tags:

views:

107

answers:

3
bash-3.2$ sed -i.bakkk -e "s#/sa/#/he/#g" .*
sed: .: in-place editing only works for regular files

I try to replace every /sa/ with /he/ in every dot-file in a folder. How can I get it working?

+1  A: 

The glob pattern .* includes the special directories . and .., which you probably didn't mean to include in your pattern. I can't think of an elegant way to exclude them, so here's an inelegant way:

sed -i.bakkk -e "s$/sa/#/he/#g" $(ls -d .* | grep -v '^\.\|\.\.$')
Adam Rosenfield
+1. Recommend find/xargs: find . -type f -maxdepth 1 -name '.*' | xargs sed ...
pilcrow
+2  A: 

Try this one:

sed -i.bakkk -e "s#/sa/#/he/#g"  `find .* -type f -maxdepth 0 -print`

This should ignore all directories (e.g., .elm, .pine, .mozilla) and not just . and .. which I think the other solutions don't catch.

Chris J
Nice. mine failed that way and I didn't notice.
dmckee
+1 for the very good point
Masi
+2  A: 
John Kugelman
+ at the end? is it the same as ";" ?
Masi
";" will run the command once per file, so 10 seds for 10 files. "+" will run it once passing *all* of the files, to 1 sed for 10 files.
John Kugelman
You mean commands such as "rm .*" are unsafe to remove dotfiles? Should you prefer: find . -name ".*" -exec rm '{}' \; ?
Masi
+1 for the sound critique
Masi
If I understood right, ";" is inefficient, so + is preferred with a big number of files.
Masi