[英]move all files from folder A to B whenever a certain string is present
I am trying to move files from folder A to folder B whenever a file contains a certain string: 我想在文件包含某个字符串时将文件从文件夹A移动到文件夹B:
grep -Rli '22/05/2018' ads/ | awk -F "//" '{print $2}' | xargs cp $0 projection/$1
cp: illegal option -- b
usage: cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file target_file
cp [-R [-H | -L | -P]] [-fi | -n] [-apvX] source_file ... target_directory
how to fix this? 怎么解决这个问题?
Following may help you here. 以下可能对您有帮助。
grep -L -Z -r 'your_string_to_be_checked' . | xargs -0 -I{} mv {} target_directory/
Explanation: 说明:
grep -L
: Means leave those files which are NOT having that specific string which we are searching for. grep -L
:表示保留那些没有我们正在搜索的特定字符串的文件。
-Z
: means output should have \\0
in file names so space is not being used as delimiter. -Z
:表示文件名中的输出应为\\0
,因此空格不用作分隔符。
-I{} mv {} new_directory/
means replace {}
with file names and make it like mv filename new_directory
and run it to move the files to new place. -I{} mv {} new_directory/
表示用文件名替换{}
并使其像mv filename new_directory
一样运行它以将文件移动到新的位置。
I am using mv
here you could use cp too here. 我在这里使用mv
你也可以在这里使用cp。
Once you have the list of filenames you want to copy you could just use xargs -I: 获得要复制的文件名列表后,可以使用xargs -I:
$ cat listOfFileNames | xargs -I{} cp {} $destFolder
the -I
option gives a name to the list of arguments, allowing you to place them wherever you want. -I
选项为参数列表提供名称,允许您将它们放在任何您想要的位置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.