如何复制使用grep找到的文件 [英] How to copy files found with grep

查看:218
本文介绍了如何复制使用grep找到的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行此命令来查找我的所有文件(在正则表达式的帮助下)在树目录中的所有文件.

I am running this command to find all my files that contain (with help of regex)"someStrings" in a tree directory.

grep -lir '^beginString' ./ -exec cp -r {} /home/user/DestinationFolder \; 

它发现了这样的文件:

FOLDER
a.txt
-->SUBFOLDER
  a.txt
---->SUBFOLDER
     a.txt

我想将具有相同架构的所有文件和文件夹复制到目标文件夹,但是我不知道该怎么做.这是重要的复制文件和文件夹,因为找到的几个文件具有相同的名称,我需要保留它.

I want to copy all files and folder, with the same schema, to the destination folder, but i don't know how to do it. It's important copy files and folder, because several files found has the same name and I need to keep it.

推荐答案

尝试一下:

find . -type f -exec grep -q '^beginString' {} \; -exec cp -t /home/user/DestinationFolder {} +

grep -lir '^beginString' . | xargs cp -t /home/user/DestinationFolder

但是,如果要保留目录结构,可以:

But if you want to keep directory structure, you could:

grep -lir '^beginString' . | tar -T - -c | tar -xpC /home/user/DestinationFolder

或者,如果像我一样,您希望确定要存储的文件类型(仅文件,没有符号链接),则可以:

or if like myself, you prefer to be sure about kind of file you store (only file, no symlinks), you could:

find . -type f -exec grep -l '^beginString' {} + | tar -T - -c |
    tar -xpC /home/user/DestinationFolder

这篇关于如何复制使用grep找到的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆