Ubuntu – How to copy file from a list to a new folder

bashcommand linecopy

I have a file file1.txt located in a trial folder containing the location of image files. I want to read the list, and copy the image files to a new folder, test_folder.

The entries in file1.txt look like:

./trial/data/image1.jpg
./trial/data/image2.jpg

etc.

I tried to use a similar question to solve the problem: Copy/move a list of files to a new directory

Attempt

while read file; do cp "$file" /trial/test_folder; done < /trial/file1.txt

I get the error "bash: /trial/file1.txt: No such file or directory". Any help would be great!

Best Answer

The error you are getting is because you are reading /trial/file1.txt and not ./trial/file1.txt. That means the shell is trying to find a directory called trial which is under the root directory (/). If you want a path that is relative to your current directory, you can just use:

while read file; do cp "$file" trial/test_folder; done < trial/file1.txt

Or,

while read file; do cp "$file" ./trial/test_folder; done < ./trial/file1.txt

Or, you can use the full path:

while read file; do cp "$file" /home/shane/trial/test_folder; done < /home/shane/trial/file1.txt
Related Question