简体   繁体   English

从文本文件中指定的目录中复制几个文件(Linux)

[英]copy few files from a directory that are specified in a text file (linux)

I have a directory called images and it contains many images. 我有一个名为images的目录,它包含许多图像。 For example: 例如:

images/
    imag001.png
    imag002.png
    imag003.png
    imag004.png

And I have a text file that has the files that I want to copy somewhere else. 我有一个文本文件,其中包含要复制到其他地方的文件。 Say the test.txt file has 说test.txt文件有

img001.png
img003.png

How do I copy the files specified in test.txt from the images folder to some other place? 如何将test.txt中指定的文件从images文件夹复制到其他地方?

尝试在您的images目录下使用此一线:

awk '{print "cp "$0" /target/path"}' test.txt|sh

I think in the bash shell you can do: 我认为您可以在bash shell中执行以下操作:

for image in $(cat copylist.txt); do
    cp $image destination
done

You can write: 你可以写:

while IFS= read -r image_filename ; do
    cp images/"$image_filename" some_other_place/
done < test.txt

There are probably many solutions to this problem. 这个问题可能有很多解决方案。 I would do it by using xargs: 我可以通过使用xargs来做到这一点:

cd images/
cat path/to/test.txt | xargs -I FILES cp FILES path/to/dest/
 cat copylist.txt  | xargs -n1 -I %  echo cp   % destination/.
 # remove echo after testing

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

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