使用 bash,如何查找包含特定字符串的所有文件并将其替换为现有文件?
我使用的是 Linux,并且想用现有文件 /home/user/offblack.png 替换所有包含字符串 000000 的文件,但保留现有文件名。我已经用 -exec 和 xargs 的各种组合来解决这个问题了一段时间,但没有运气。到目前为止,我已经:
find | grep 000000
其中列出了我想要更改的所有文件。如何复制这些文件并将其替换为现有的 offblack.png 文件?
I am using Linux and would like to replace all files containing the string 000000 with an existing file /home/user/offblack.png but keep the existing filename. I've been working at this for a while with various combinations of -exec and xargs but no luck. So far I have:
find | grep 000000
Which does list all the files I want to change fine. How do I copy and replace these files with my existing offblack.png file?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这是我将使用的内容:
展开,找到您感兴趣的所有文件,在其中查找字符串“000000”(将 /dev/null 添加到文件列表中,以防生成的
fgrep< /code>s 最终只有一个文件名 - 它确保输出始终格式化为“文件名:<行包含'000000'>”),仅删除文件名,然后将 offblack.png 逐一复制到这些文件上。请注意,我在其中插入了
/bin/echo
。这就是你的预演。删除回声以使其真正运行。如果您的意思是文件名包含“000000”:
更简单。 :-) 查找当前目录下名称包含您的字符串的每个文件,并在其上执行 offblack.png 的副本。再说一次,我给你的是一次演练。消除现场消防演习的回声。 :-)
Here's what I would use:
Expanding, find all the files you're interested in, grep inside of them for the string '000000' (adding /dev/null to the list of files in case one of the generated
fgrep
s ended up with only one filename - it ensures the output is always formatted as "filename: <line containing '000000'>"), strip out only the filenames, then one-by-one, copy in offblack.png over those files. Note that I inserted a/bin/echo
in there. That's your dry-run. Remove the echo to get it to run for real.If what you mean is that the filenames contain "000000":
Much simpler. :-) Find every file under the current directory with a name containing your string and exec the copy of offblack.png over it. Again, what I've given you there is a dry-run. Remove the echo for your live fire drill. :-)
让我们尝试更多地使用 Bash:
需要搜索的手册页更少!
Let's try and use Bash a bit more:
Fewer man pages to search!