通常要search和replaceselect的文件中的string,我会发现:
grep -rl 'pattern' .
这工作正常,除非你有很多比赛。
无论如何,我环顾四周,发现了一个使用find的build议:
find /root/path -type f -exec grep -l 'FIND' {} +
这工作正常,但是当我尝试将它传递到Perl来做replace,我仍然得到错误:
perl -p -i -e 's|FIND|REPLACE|g' `find /root/path -type f -exec grep -l 'FIND' {} +` -bash: /usr/local/bin/perl: Argument list too long
有没有办法解决?
正如人们所build议的,我需要写一个脚本来为我做魔术:)
对我来说,我有很多文件 – 所以为了加快速度(并使服务器上的find更好一点),我已经把我的文件分成了多个文件夹。 所以脚本是:
#!/usr/bin/perl my $find = q|string-to-find|; my $replace = q|replace-with|; my @paths = split /\n/, q|/home/user/folder1 /home/user/folder2 /home/user/folder3 /home/user/folder4|; my $debug = 1; foreach my $path (@paths) { my @files = split /\n/, `find $path -type f -exec grep -l '$find' {} +`; foreach (@files) { chomp; if (-f $_) { print qq|Doing $_\n| if $debug > 0; `sed -i 's/$find/$replace/g' $_` } } }
然后从SSH运行它:
perl script-name.cgi