开发者

grepping patterns and deleting files

I have an external file that contains a list of patterns (pattern per line).

pattern1
foo bar
pattern_n
bar
bar foo

开发者_开发技巧I would like to grep all files including the ones within sub-folders using those patterns, if the pattern matches, copy the file to some /tmp/mybackup/ and then delete it. What would be a good way of doing this?


If I understand your problem correctly, you need the following switches to grep:

  • -R to scan recursively
  • -l to print only matching filenames
  • -f to read the patterns from a file
  • -I to ignore binary files

so:

grep -RlIf patterns-file *

then feed this result to some other utility to perform the backup, eg xargs:

grep -RlIf patterns-file * | xargs -I {} mv {} /tmp/backup

or with a loop:

for afile in `grep -RlIf patterns-file *`; do
   mv $afile /tmp/backup
done


Try

for x in `fgrep -f patternfile.txt -l -r .`; do cp $x /tmp/mybackup; rm $x; done
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜