简体   繁体   English

搜索多个文件并将它们复制到同一目录中

[英]Search for multiple files and copy them into the same directory

I'm working on a mac, and have an excel spreadsheet with a list of around 400 file names.我在 Mac 上工作,有一个 excel 电子表格,其中包含大约 400 个文件名的列表。 They are all on my hard disc somewhere, but could be anywhere within a large archive directory.它们都在我的硬盘上的某个地方,但可能位于大型存档目录中的任何位置。 Ideally I'd like to avoid having to search for each one individually, copy and then paste them into a new directory together in order to do some downstream analysis.理想情况下,我希望避免单独搜索每个,复制然后将它们一起粘贴到一个新目录中,以便进行一些下游分析。

I was thinking there must be some clever way in terminal to grep the file path for a list of file names, then cp those files to a new directory of my choice.我在想终端中必须有一些聪明的方法来grep文件名列表的文件路径,然后将这些文件cp到我选择的新目录。 I don't really know where to start though.我真的不知道从哪里开始。 Any help would be much appreciated!任何帮助将非常感激!

Here's a sample of filenames from my spreadsheet:这是我的电子表格中的文件名示例:

E12_JH_260919_E12_2019-09-26_1.fsa
F01_JH_260919_F01_2019-09-26_1.fsa
F02_JH_260919_F02_2019-09-26_1.fsa
F03_JH_260919_F03_2019-09-26_1.fsa
D10_JH_181019.3_D10_2019-10-18_1.fsa
D11_JH_181019.3_D11_2019-10-18_1.fsa
D12_JH_181019.3_D12_2019-10-18_1.fsa
E01_JH_181019.3_E01_2019-10-18_1.fsa
E02_JH_181019.3_E02_2019-10-18_1.fsa
E03_JH_181019.3_E03_2019-10-18_1.fsa

Thanks!谢谢!

You can use find tool for that purpose.为此,您可以使用find工具。 Let's say you have a directory like the following:假设您有一个如下所示的目录:

~/archive$ tree
├── ASDF.txt
├── destination_folder
├── DFGH.txt
└── spreadsheet.txt

1 directory, 3 files

And a spreadsheet like this:和这样的spreadsheet

~/archive$ cat spreadsheet.txt 
ASDF.txt
DFGH.txt

You can read the file line by line, find each file in the current directory, and copy that into another directory:您可以逐行读取文件,找到当前目录中的每个文件,并将其复制到另一个目录中:

while read name; do
find . $name -maxdepth 0 -type f -exec cp {} ./destination_folder \;
done < spreadsheet.txt

And the result should look like this:结果应该是这样的:

    ls a
ASDF.txt DFGH.tx

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM