sed 未按预期工作,但仅适用于目录深度大于 1
我试图在系统上的所有文件中查找字符串的所有实例,直到指定的目录深度。然后我想用另一个字符串替换它们,并且我通过将一个字符串连接到另一个字符串来使用“find”和“sed”。
这适用于我使用基本路径作为 cd /home/../.. 或任何其他不是“/”的目录。它也仅在我选择目录深度为 1 时才有效(因此 /test.txt 已更改,但 /home/test.txt 未更改)如果我不更改任何其他内容并使用深度为 2 或 3,则 /test 都不会.txt 和 /home/text.txt 都被更改。在前者中,不会出现警告,而在后者中,结果如下(并且两个文件中都没有替换任何字符串)。
令人担忧的是,它确实突然起作用了,但我不知道如何起作用,也无法重新创建结果。我应该说我知道使用根目录中的这些命令的风险,并且下面的程序的具体使用是有意的,所以我不是在寻找替代方法,只是一个关于它如何不起作用的线索,也许关于如何修复它的建议。
cd /;find . -maxdepth 3 -type f -print0 | xargs -0 sed -i 's/teststring123/itworked/gI'
sed: couldn't open temporary file ./sys/kernel/sedoPGqGB: No such file or directory
sed: couldn't open temporary file ./proc/878/sedtqayiq: No such file or directory
正如您所看到的,有警告,但无论我期望它如何工作,命令看起来都很好,我错过了什么吗?
I am trying to find all instances of a string in all files on my system up to a specified directory depth. I then want to replace these with another string and I am using 'find' and 'sed' by piping one into the other.
This works where I use the base path as cd /home/../.. or any other directory which isn't "/". It also only works if I select a directory depth of 1 (so /test.txt is changed, but /home/test.txt isn't) If I change nothing else and used say a depth of 2 or 3, neither /test.txt nor /home/text.txt are changed. In the former, no warnings appear, and in the latter, the results below (And no strings are replaced in either of the files).
Worryingly, it did work once out of the blue, but I have no idea how and I can't recreate the results. I should say I know the risks of using these commands with root from base directory, and the specific use of the programs below is intentional so I am not looking for an alternative way, just a clue as to how this isn't working and perhaps a suggestion on how to fix it.
cd /;find . -maxdepth 3 -type f -print0 | xargs -0 sed -i 's/teststring123/itworked/gI'
sed: couldn't open temporary file ./sys/kernel/sedoPGqGB: No such file or directory
sed: couldn't open temporary file ./proc/878/sedtqayiq: No such file or directory
As you see, there are warnings, but nether the less I would expect it to work, the commands appear good, anything I am missing folks?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该是:
尽管更改
/
下面的所有文件确实让我觉得是一个非常糟糕的主意(我希望你不是以 root 身份运行!)。“无法打开临时文件 ./[...]”错误可能是因为以您的用户身份运行的
sed
无权在/< 中创建文件/代码>。
我的版本从您当前的工作目录运行,我假设您的
${HOME}
,您将能够在其中创建临时文件,但您仍然不太可能能够替换那些重要的文件以便您的操作系统继续运行。This should be:
Although changing all files below
/
strikes me as a very bad idea indeed (I hope you're not running as root!).The "couldn't open temporary file ./[...]" errors are likely to be because
sed
, running as your user, doesn't have permission to create files in/
.My version runs from your current working directory, I assume your
${HOME}
, where you'll be able to create the temporary file, but you're still unlikely to be able to replace those files vital to the continued running of your operating system.