简体   繁体   English

只要存在某个字符串,就将所有文件从文件夹A移动到B.

[英]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。

Not sure why you're using awk. 不知道你为什么使用awk。

Solution: 解:

grep -lir '22/05/2018' ~/ads/* | xargs cp -t $DEST_FOLDER

Source 资源

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.

相关问题 每当将某种类型的文件添加到 macOS 上的文件夹 A 时,自动将它们从文件夹 A 移动到文件夹 B - Automatically move files of a certain type from folder A to B whenever they are added to folder A on macOS 移动和删除文件夹中所有与grep匹配的文件 - Move and delete all files matching grep in folder Bash - 移动包含特定字符串的文件 - Bash - Move files that contain certain string 将所有 *.mp4 文件从所有子文件夹移动到另一个指定文件夹 - Move all *.mp4 files from all subfolders to another specifcied folder 使用文件夹中存在的文件名创建文件夹,然后在每个文件夹中移动这些文件 - Create folders with names of files present in folder then move those files in each of that folder 如何在Mac上列出文件夹中具有特定字符串的所有文件? - How to list all the files in a folder that have a certain string in their file name on Mac? Bash:复制根/当前文件夹中具有相同扩展名的所有文件,但文件名包含特定字符串的文件除外 - Bash: Copy all files with same extension in root/current folder, except those whose filename contains certain string 如何使用grep在当前目录中搜索具有给定字符串的所有文件,然后将这些文件移动到新文件夹? - How do I use grep to search the current directory for all files having a given string and then move these files to a new folder? 如何 git 添加除特定文件夹之外的所有文件,从回购中的任何地方? - How to git add all files excluding a certain folder, from anywhere in a repo? 将多个文件从文件夹移动到目录列表(撤消移动命令) - Move multiple files from a folder to list of directories (Undo a move command)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM