用于字符串的grep文件并将目录复制到另一个目录 [英] grep file for string and copy directory to another directory

查看:651
本文介绍了用于字符串的grep文件并将目录复制到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在每个目录中只有一个文件的大量目录 - index.html。我希望使用grep来查找文件中的模式,然后将目录和文件一起复制到另一个目录中。



复制文件的示例,我已经看到了,但是我想将目录与文件复制到另一个目录中。



所以说下面是使用目录的匹配文件列表

  grep -rlstringsource_dir 


d1 / index.htmk
d2 / index.html
d3 / index.html

...
...
a long list。



现在想要复制到dest-dir,所以dest_dir看起来像

 
..
d1 / index.html
d2 / index.html
d3 / index.html
...
...



TIA

解决方案

<要保留目录结构,请使用 cpio 处于传递模式。 cpio 大约与 tar ,并且过去有更多的优点,但它有一种滑入默默无闻。我是新手,主要遵循古老的 Linux Journal cpio指南来构建这个命令:

  mkdir dest_dir 
cd source_dir
grep -Zlrstring 。 | cpio -p0dmv ../dest_dir

通过以null结尾的 * 符合条件的文件列表一个直接到 cpio 中的管道,它被设计为以这种方式获取文件列表,然后存档或复制(pass-through, -p )。我们在这里做后者,保留目录结构( -d )以及修改时间( -m )。我将其设置为verbose( -v ),以便您可以观察进度。如果您通过 ssh 进行连接,您可能不希望这样做,因为通过网络渲染每个文件名可能会降低处理速度。



* 关于空白终止: 我用 grep -Zl cpio -0 来解决包含换行符的文件名的问题(不要 ); grep -Zl 列出了由空字符分隔的所有匹配文件(路径唯一无效字符)和 cpio -0 预期以空值终止的输入(如 xargs -0 )。

 



我最初建议 tar 来创建一个临时存档,并再次将 tar 解压缩到新的位置。这使用 xargs 将文件列表转换为参数,因为 tar 无法接受文件列表在另一个文件(或标准输入,如 cpio )中,但 xargs 将太长的命令拆分为多个调用和 tar 无法提取连接的输出 **

  mkdir dest_dir 
cd source_dir
grep -Zlrstring。 | xargs -0 tar -pc | tar -pxi --directory = .. / dest_dir

你的目的地目录,进入源目录,然后用 -Zl (以null结尾的文件列表 * )和运行grep, -r (递归)。 xargs -0 将该列表变成 tar 的参数,这些参数归档它们。另一个 tar 实例然后将它们提取到目标目录中。
$ b ** xargs 默认为 - max-procs = 1 并且应该一次运行一个进程,导致多个压缩包连接在一起一起。 tar格式应该能够处理这个问题,尽管进一步阅读提出了一个简单的解决方案。要添加 -i 忽略零))来解压缩 tar 来解决这个问题。我将它添加到上面的代码中,但没有测试它。


I have large number of directories with just one file -- index.html -- in each directory. I would want to use grep to look for pattern in file and then copy the directory along with the file to another directory.

Example for copying files, I have seen, but I would want to copy the directory with the file in to another directory.

so say following are the list of matching files with directory using

grep -rl "string" source_dir


d1/index.htmk
d2/index.html
d3/index.html

... ... a long list.

Now would want to copy to dest-dir so dest_dir looks like

.
..
d1/index.html
d2/index.html
d3/index.html
...
...

TIA

解决方案

To preserve the directory structure, use cpio in pass-through mode. cpio is about as old as tar and used to have more advantages, but it has kind of slipped into obscurity. I'm new to it and mostly followed an ancient Linux Journal cpio guide to build this command:

mkdir dest_dir
cd source_dir
grep -Zlr "string" . |cpio -p0dmv ../dest_dir

This passes a null-terminated* list of files matching your criteria through a pipeline directly into cpio, which is designed to take a list of files in this manner and then either archive or copy ("pass-through," -p). We do the latter here, preserving the directory structure (-d) as well as modification times (-m). I've set this to verbose (-v) so you can watch the progress. If you're connecting via ssh, you might not want that since rendering each filename over the network can slow down the process.

* Regarding null termination:  I used grep -Zl with cpio -0 to work around the issue of file names containing newlines (don't do that!); grep -Zl lists all matching files delimited by null characters (the only invalid character for a path) and cpio -0 expects null-terminated inputs (as does xargs -0).

 

I originally recommended tar to create a temporary archive and tar again to extract it into the new location. This used xargs to convert the file list into arguments since tar doesn't have the ability to accept its list of files within another file (or standard input, as cpio does), but xargs splits commands that are too long into multiple calls and tar can't extract the concatenated output**.

mkdir dest_dir
cd source_dir
grep -Zlr "string" . |xargs -0 tar -pc |tar -pxi --directory=../dest_dir

This makes your destination directory, enters the source directory, and runs grep with -Zl (null-terminated file list*) and -r (recursive). xargs -0 turns that list into arguments for tar, which archives them. Another tar instance then extracts them into the destination directory.

** xargs defaults to --max-procs=1 and should run one process at a time, resulting in multiple tarballs that are concatenated together. The tar format is supposed to be able to handle this, though further reading suggested a simple solution is to add a -i (ignore zeros) to the extracting tar to solve that problem. I added it to the above code but have not tested it.

这篇关于用于字符串的grep文件并将目录复制到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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