1

How do I copy files from Folder A to Folder B based on Text File C with bash?

  • folderA: source folder with image files only, e.g. image01.png, image02.png etc. There are no space in the image names, only dash or underscore
  • folderB: destination folder with nothing inside.
  • C.txt: is a UTF16 encoded raw text file, inside the text file stored the file names that I want to copy from folderA and these file names are line by line

    image01.png
    image02.png
    image04.png
    

Specific paths are

  • ~/Documents/C.txt
  • ~/Documents/folderA/
  • ~/Documents/folderB/
4
  • 3
    What do you mean "based on text file C"? Can you provide an example of how you expect this to work?
    – Allan
    Commented Jan 15, 2019 at 14:38
  • 1
    I'm glad that you found a solution but the solution doesn't match your question- "How do I copy files from one directory to another based on a text file with filenames"
    – fd0
    Commented Jan 16, 2019 at 16:40
  • I don't think so fd0, I do exactly this : I changed directory to folderA then 'cp argument1 argument2' where argument 1 is a chain of file names that I want and separated by a space; argument2 is the destination. Talking is unreal, test it in your local host.
    – gcplau
    Commented Jan 16, 2019 at 16:44
  • Please don‘t add the answer to the Question, Post it as an answer below instead. It might also help to make sure the formatting is correct, and to show how you use C.txt in your solution.
    – nohillside
    Commented Jan 16, 2019 at 16:49

1 Answer 1

2

Assuming your file C contains a list of file names to copy:

A="~/Documents/folderA"
B="~/Documents/folderB"
while IFS= read -r file; do
    cp "${A}/${file}" "${B}/"
done < ~/Documents/c.txt

should do the trick.

4
  • cd ~/ to change director cp (paste chain of file names here now) (then, type a space) then type your destination folder path/name (hit return)
    – gcplau
    Commented Jan 16, 2019 at 15:59
  • this link will help any newbies : openclassrooms.com/en/courses/…
    – gcplau
    Commented Jan 16, 2019 at 16:01
  • Tried many times, this script doesn't work for me.
    – gcplau
    Commented Jan 19, 2019 at 16:28
  • @gcplau What exactly doesn‘t work?
    – nohillside
    Commented Jan 20, 2019 at 10:42

You must log in to answer this question.

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