I have a working grep
command that selects files meeting a certain condition. How can I take the selected files from the grep
command and pipe it into a cp
command?
我有一个工作的grep命令,它选择符合一定条件的文件。如何从grep命令中获取所选文件并将其导入到cp命令中?
The following attempts have failed on the cp
end:
以下尝试在cp端失败:
grep -r "TWL" --exclude=*.csv* | cp ~/data/lidar/tmp-ajp2/
cp: missing destination file operand after ‘/home/ubuntu/data/lidar/tmp-ajp2/’ Try 'cp --help' for more information.
cp:在' /home/ubuntu/data/lidar/tmp-ajp2/ ' Try 'cp -help'之后丢失目标文件操作符。
cp `grep -r "TWL" --exclude=*.csv*` ~/data/lidar/tmp-ajp2/
cp: invalid option -- '7'
cp:无效选项——“7”
4 个解决方案
#1
23
grep -l -r "TWL" --exclude=*.csv* | xargs cp -t ~/data/lidar/tmp-ajp2/
Explanation:
解释:
- grep
-l
option to output file names only - 只对输出文件名进行grep -l选项。
- xargs to convert file list from the standard input to command line arguments
- xargs将文件列表从标准输入转换为命令行参数。
- cp
-t
option to specify target directory (and avoid using placeholders) - cp -t选项指定目标目录(并避免使用占位符)
#2
12
you need xargs with the placeholder option:
您需要带有占位符选项的xargs:
grep -r "TWL" --exclude=*.csv* | xargs -I '{}' cp '{}' ~/data/lidar/tmp-ajp2/
normally if you use xargs
, it will put the output after the command, with the placeholder ('{}'
in this case), you can choose the location where it is inserted, even multiple times.
通常,如果您使用xargs,它会将输出放在命令之后,使用占位符(在本例中为“{}”),您可以选择插入它的位置,甚至可以多次插入。
#3
0
To copy files to grep found directories, use -printf to output directories and -i to place the command argument from xarg (after pipe)
要将文件复制到grep找到的目录,使用-printf输出目录,使用-i从xarg中放置命令参数(在管道之后)
find ./ -name 'filename.*' -print '%h\n' | xargs -i cp copyFile.txt {}
this copies copyFile.txt to all directories (in ./) containing "filename"
这个副本拷贝文件。txt到包含“文件名”的所有目录(in ./)
#4
0
grep -rl '/directory/' -e 'pattern' | xargs cp -t /directory
grep -rl '/directory/' -e 'pattern' | xargs cp -t /directory
#1
23
grep -l -r "TWL" --exclude=*.csv* | xargs cp -t ~/data/lidar/tmp-ajp2/
Explanation:
解释:
- grep
-l
option to output file names only - 只对输出文件名进行grep -l选项。
- xargs to convert file list from the standard input to command line arguments
- xargs将文件列表从标准输入转换为命令行参数。
- cp
-t
option to specify target directory (and avoid using placeholders) - cp -t选项指定目标目录(并避免使用占位符)
#2
12
you need xargs with the placeholder option:
您需要带有占位符选项的xargs:
grep -r "TWL" --exclude=*.csv* | xargs -I '{}' cp '{}' ~/data/lidar/tmp-ajp2/
normally if you use xargs
, it will put the output after the command, with the placeholder ('{}'
in this case), you can choose the location where it is inserted, even multiple times.
通常,如果您使用xargs,它会将输出放在命令之后,使用占位符(在本例中为“{}”),您可以选择插入它的位置,甚至可以多次插入。
#3
0
To copy files to grep found directories, use -printf to output directories and -i to place the command argument from xarg (after pipe)
要将文件复制到grep找到的目录,使用-printf输出目录,使用-i从xarg中放置命令参数(在管道之后)
find ./ -name 'filename.*' -print '%h\n' | xargs -i cp copyFile.txt {}
this copies copyFile.txt to all directories (in ./) containing "filename"
这个副本拷贝文件。txt到包含“文件名”的所有目录(in ./)
#4
0
grep -rl '/directory/' -e 'pattern' | xargs cp -t /directory
grep -rl '/directory/' -e 'pattern' | xargs cp -t /directory