2

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!

  • I have updated the question to include that - thanks! – Shane B Mar 22 at 10:16
4

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
  • Thanks so much! That worked. I am very new to Linux so that was a great help – Shane B Mar 22 at 10:23
2

Another way is to use xargs and cp --target-directory=... thusly:

xargs -r <trial/file1.txt cp --target-directory=./trial/test_folder

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.