简体   繁体   English

如何将特定文件从目录复制到新目录 zsh

[英]How to copy specific files from directory into new directory zsh

I have a list of file names contained within a text file (a.txt).我有一个包含在文本文件 (a.txt) 中的文件名列表。 I want to extract from a directory (b) the files listed in a.txt to a new directory (c).我想从目录 (b) 中提取a.txt中列出的文件到新目录 (c)。 The syntax of the filenames in a.txt and b match. a.txtb中文件名的语法匹配。 The files in a.txt are empty and the files in b contain the json message of interest. a.txt中的文件为空, b中的文件包含感兴趣的 json 消息。

For example, the contents of a.txt look like:例如, a.txt的内容如下所示:

ML3DBHCN___005.json
OCO2_L2_Standard___10r.json
GPM_3IMERGM___06.json 

and b :b

b/ML3DBHCN___005.json
b/OCO2_L2_Standard___10r.json
b/GPM_3IMERGM___06.json 

Do i need to write a small .sh file that iterates through a.txt and extracts from b or can this be completed at once via command line?我是否需要编写一个小的 .sh 文件来遍历a.txt并从b中提取,或者这可以通过命令行一次完成吗?

If you know the filenames don't contain whitespace or wildcard characters, you can do it as a simple one-liner:如果您知道文件名不包含空格或通配符,则可以将其作为简单的单行符:

cp $(<a.txt) b/

If they can contain special characters, you can read them into an array:如果它们可以包含特殊字符,则可以将它们读入数组:

readarray files <a.txt
cp "${files[@]}" b/

如果您想将a.txt中命名的文件从b移动到c并且它们没有空格或通配符):

(cd /path/to/b && mv $(< /path/to/a.txt) /path/to/c/)

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

相关问题 从* .txt搜索并将文件复制到新目录 - Search from a *.txt and copy files to new directory 卡住尝试根据特定准则将文件复制到新目录 - Stuck trying to copy files to a new directory under specific guidelines 如何在父目录linux的子目录中查找和复制文件 - how to find and copy files in a sub directory from parent directory linux 从特定目录开始复制文件路径 - Bash - Copy files path from a specific directory onwards - Bash 使用 Linux 将特定数量的文件从一个目录复制到另一个 - Copy specific number of files from on directory to another using Linux 在目录中查找文件副本到新文件名 - Find files in a directory copy to a new file name 使用文本文件(包含文件名)将文件从当前目录复制到新目录 - Use a text file (containing file names) to copy files from current directory to new directory 如何使用 scp 从远程服务器复制目录,不包括特定的子目录 - How to copy directory from remote server with scp, excluding a specific subdirectory 如果文件不在另一个目录中,则将文件复制到目录 - Copy files to directory if they are not in another directory 如何在Linux中将具有特定扩展名的文件从一个目录复制到另一个目录 - How to copy files with a particular extension from one directory to another in linux
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM