4

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!

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

3 Answers 3

5

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
1
  • Thanks so much! That worked. I am very new to Linux so that was a great help
    – Shane B
    Mar 22, 2019 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
0

Late, but rsync is also a great way to do this:

rsync -av --include-from=trial/file1.txt --exclude="*" . ./trial/test_folder
  • The source directory is the current directory, since the lines in file1.txt are relative to the current directory.
  • The destination directory is the new folder.
  • We exclude everything, except what matches the entries in the file

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .