[英]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.txt和b中文件名的语法匹配。 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.