Sunday, November 27, 2016

command line - How to copy file from a list to a new folder?




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!



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

No comments:

Post a Comment

11.10 - Can&#39;t boot from USB after installing Ubuntu

I bought a Samsung series 5 notebook and a very strange thing happened: I installed Ubuntu 11.10 from a usb pen drive but when I restarted (...