-1

I'm trying to iterate over every line in a file, where each line is a path I need to copy. I used the echo command to print the paths and tried copying with the following command:

while read p; do
  cp echo "$p" echo "$p1"
done <file_name.txt

The error message was:

cp: target ‘’ is not a directory

Is there any bash command that can be used for my purpose?

Michael Schnerring
  • 3,584
  • 4
  • 23
  • 53
Ziv
  • 109
  • 10
  • 1
    why are you using echo here ? also you never defined `$p1` in your code sample – Aserre Sep 17 '20 at 09:13
  • from [`man cp`](https://linux.die.net/man/1/cp) : "Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.". Here, you are trying to copy the files named `echo`, the content of `$p`, another file named `echo` to the folder defined in your `$p1` variable (which is empty) – Aserre Sep 17 '20 at 09:16
  • Regarding your `\r` problem : I assume you are working on windows or using an input file that was edited on windows. Please refer to [this question](https://stackoverflow.com/questions/2613800/how-to-convert-dos-windows-newline-crlf-to-unix-newline-lf-in-a-bash-script) or read about the `dos2unix` program – Aserre Sep 17 '20 at 09:41

2 Answers2

1

You have not declared what p1 variable is.

The cp command works as follows:

cp "${Source}" "${destination}"

Declare the source and destination in the program before the while loop.

Modified code:

while read p; do
cp "${Source}" "${destination}"
done < file_name.txt
Vani Gupta
  • 505
  • 6
  • 13
  • I tried that and for some reason the string `\r` was added to every path, so error messages were printed because the paths are incorrect. I looking for a way to remove these strings. – Ziv Sep 17 '20 at 09:28
0

this works for me:

#!/bin/bash

destination=/tmp/result/
while read line; do
  cp "${line}" "${destination}"
done <file_name.txt
Maik
  • 310
  • 1
  • 11