简体   繁体   English

查找名称与多种模式之一匹配的文件并复制到不同的目录

[英]Find files with names matching one of many patterns and copy to different directory

I have a set of files in a Linux directory.我在 Linux 目录中有一组文件。 I want to copy the files whose names match one of many patterns to a different directory;我想将名称与许多模式之一匹配的文件复制到不同的目录; I'm placing all those pattern in a text file.我将所有这些模式放在一个文本文件中。

What is a possible way I could find all the files with the pattern and copy to a destination folder?我可以找到具有该模式的所有文件并将其复制到目标文件夹的可能方法是什么?

Until now, I was able to do just keep the patterns like this:到现在为止,我只能保持这样的模式:

find . -type f \( -name "*Patt1* -o -name "*Patt2*" - o -name "*Patt3*" \
    -o -name "*Patt4*" -o -name "*Patt5* -o -name "*Patt6*" \) \
    | xargs cp {} /home/DestinationFolder/.

My pattern file looks like this:我的模式文件如下所示:

PYTH
SPYD
ISIN
CUSIP
HELD
SEDO

You can filter your filenames with grep, using the -f option to read the patterns from a file:您可以使用 grep 过滤文件名,使用-f选项从文件中读取模式:

find . -type f -print0 | grep -zFf pattern.txt \
    | xargs -0 -I {} cp {} /home/DestinationFolder

Since your patterns, according to your comment, are just fixed strings, we can add -F to the grep options to interpret the lines from the input file as fixed strings.由于根据您的评论,您的模式只是固定字符串,我们可以在 grep 选项中添加-F以将输入文件中的行解释为固定字符串。

To accommodate any possible character in filenames, I use -print0 to print the filenames null byte separated;为了适应文件名中任何可能的字符,我使用-print0来打印文件名空字节分隔; grep -z reads to and writes from a null byte separated stream; grep -z从空字节分隔的流读取和写入; and xargs -0 expects null byte separated input.并且xargs -0需要空字节分隔的输入。

I am on MacOS.我在 MacOS 上。 My problem was very similar you yours, but I wanted to use a regex to match the files to be operated on.我的问题与您的非常相似,但我想使用正则表达式来匹配要操作的文件。 The first answer pointed me in the right direction, but sadly (or not?) I am on MacOS.第一个答案为我指明了正确的方向,但遗憾的是(或不是?)我在 MacOS 上。 Evidently, BSD find doesn't like -printf , and BSD grep doesn't like -z .显然,BSD find不喜欢-printf ,BSD grep不喜欢-z So, fiddlesticks.所以,小提琴手。

On MacOS (10.13: High Sierra), and using bash (I tested the MacOS-supplied one and GNU bash 4.4.19), the following does what you need.在 MacOS(10.13:High Sierra)上,并使用 bash(我测试了 MacOS 提供的一个和 GNU bash 4.4.19),以下可以满足您的需求。 As a bonus, it's resilient with respect to filenames that contain spaces (but not all weird filenames), and allows patterns to be extended regexs instead of only simple fixed strings.作为奖励,它对于包含空格的文件名(但不是所有奇怪的文件名)具有弹性,并允许模式扩展正则表达式,而不仅仅是简单的固定字符串。 The example below includes an extended regex pattern (the first line in the file) which I actually used to do a similar operation: moving files that end in patterns like _9.mpg to a different directory:下面的示例包括一个扩展的正则表达式模式(文件中的第一行),我实际上用它来执行类似的操作:将以 _9.mpg 等模式结尾的文件移动到不同的目录:

Caveat: To make spaces play nice for files with spaces in them (and who doesn't have spaces in their filenames, eh??), I'm using this hack, which will cause bash to split pipes between commands on newlines, instead of spaces:警告:为了让空格对其中有空格的文件很好用(并且文件名中没有空格,嗯??),我正在使用这个 hack,这将导致 bash 在换行符上的命令之间拆分管道,而不是空间:

 OLD_IFS=$IFS
 IFS=$(echo -en "\n\b")

Here is a test "source" directory:这是一个测试“源”目录:

% ls -1 source
bad dog_1.mp3
bod dog.mp3
i am SEDO yeah

Here is the empty "target" directory:这是空的“目标”目录:

% ls ../target|wc -l
       0

You can make a pattern file containing whatever patterns are legal extended regexs, which includes simple strings which don't contain special regex characters.您可以制作一个包含任何合法扩展正则表达式模式的模式文件,其中包括不包含特殊正则表达式字符的简单字符串。 If you revile regexs and want just simple strings (and are therefore more sane than I), change 'grep -Ef' above to 'grep -f':如果您讨厌正则表达式并且只想要简单的字符串(因此比我更理智),请将上面的“grep -Ef”更改为“grep -f”:

% cat patterns.txt
_\d+\.m..$
PYTH
SPYD
ISIN
CUSIP
HELD
SEDO

Here is the file with the meat:这是带肉的文件:

% cat generate-commands.sh
for f in $(find ${src} -type f | grep -Ef ${pats}); do
  echo mv \"$f\" \"${trg}/$f\"
done

And here we go!我们开始了! First, cd into the source dir, then run the generate-commands file with the required params:首先, cd 进入源目录,然后使用所需的参数运行 generate-commands 文件:

% cd source
% src=. trg=../target pats=../patterns.txt . ../generate-commands.sh
mv "./i am SEDO yeah" "../target/./i am SEDO yeah"
mv "./bad dog_1.m4v" "../target/./bad dog_1.m4v"

Then, ever so carefully, thoughtfully consider actually executing the commands that were output by the generate-commands.sh script.然后,仔细、深思熟虑地考虑实际执行 generate-commands.sh 脚本输出的命令。

Finally, unless you like surprises, it's probably a good idea to either either kill your current shell, or do this:最后,除非你喜欢惊喜,否则最好要么杀死当前的 shell,要么这样做:

IFS=$OLD_IFS

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在文件linux中找到匹配的模式 - find matching patterns in files linux 在目录中查找所有匹配模式的文件并复制找到的子目录和文件 - Find all files matching pattern in directory and copy subdirectories and files found 在Linux中,如何查找相同目录下具有相同扩展名的文件并将其复制到一个目录中? - How does one find and copy files of the same extension, in different directories, to a single directory in linux? 仅在下面的目录中查找和同名文件,然后复制到其他目录 - Find and files of same name only in directory below and copy to a different directory 一个目录中的许多文件? - Many files in one directory? touch命令在一个目录下创建多个文件(不同名称) - touch command create multiple files (different names) under one directory linux查找文件并复制到目录 - linux find files and copy to directory 如何在给定目录下找到具有位置的文件名列表,并使用 linux 命令进行递归搜索与精确模式匹配 - How to find list of files names with locations under given directory with exact pattern matching with recursive search with linux command 复制文件和一个目录 - Copy files and one directory up 如何在bash中使用相同的名称但扩展名相同的目录复制多个文件? - How can I copy multiple files in the same directory with different names but same extension in bash?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM