0

I have a text file called "list.txt" that contain all the directories of the files that need to be copied to a new folder (dir_newfolder). I wrote the code like below:

for file in $(cat list.txt); do cp ${file} dir_newfolder; done

I got list of errors: cp:"file_name":No such file or directory. The file_names are the lines pulled out from the "list.txt". But when I copy each file_names from the error message and use cp to copy to the new folder. There is no error.

I am using mac os terminal.

Thanks in advance.

Catherine
  • 161
  • 6
  • The two things that come to mind are weird characters in the file (maybe spaces, DOS/Windows line endings, something like that). Run `set -x` before the `for` loop to turn on execution tracing, and see if the `cp` commands it's executing match what you expect. The other one is things being in the wrong directory -- are the files (the ones to be copied) in the same directory you're "in" when you run this loop? – Gordon Davisson Jul 13 '21 at 08:04
  • Hello, Thank you for the suggestions. I am new to bash comments. I have tried the ``set -x`` command, but it gives me a bunch of output that I don't know. Could you specify how I should use ``set -x`` and what should I see from this? – Catherine Jul 13 '21 at 16:58
  • By the way, thank you for point out the directory issue. I am using the absolute directory. I guess in this case, it does not matter whether the files and scripts are in the same folder, right? – Catherine Jul 13 '21 at 17:10
  • You need to run the `set -x` command in the same shell context as what you want to trace. So if the `for file ...` loop is in a shell script, put `set -x` in the script (before the `for file ...` loop). If you're just typing that `for file ...` loop in an interactive shell, type in `set -x` before it (and `set +x` afterward, to turn tracing off). In an interactive shell, you may see a lot of other things happening as well, like updating the shell prompt; you'll have to look through the output for the relevant bits (the `cp ...` commands). – Gordon Davisson Jul 13 '21 at 18:43
  • I have tried the ``set -x``, which is helpful. I found out the file directory in the list.txt is not set as ``/Home/dir/file_name``. It was ``~/dir/file_name?``. Thank you for helping me! – Catherine Jul 14 '21 at 19:11
  • See [this question](https://stackoverflow.com/questions/3963716/how-to-manually-expand-a-special-variable-ex-tilde-in-bash) for options to expand `~` to the home directory path (but don't use any of the ones that involve `eval` -- it's likely to cause other problems). – Gordon Davisson Jul 15 '21 at 05:03

1 Answers1

0

Copy a file or folder locally

In the Terminal app on your Mac, use the cp command to make a copy of a file.

For example, to copy a folder named Expenses in your Documents folder to another volume named Data:

% cp -R ~/Documents/Expenses /Volumes/Data/Expenses

The -R flag causes cp to copy the folder and its contents. Note that the folder name does not end with a slash, which would change how cp copies the folder.

in your case:

make sure you are providing correct path list.txt and the correct path for destiny folder, also i mentioned how to access file variable in double quotes , try this code it's working for me

for file in $(cat ~/Documents/list.txt);  do cp "$file" ~/dir_newfolder;  done 
saqib kafeel
  • 296
  • 3
  • 8
  • Thank you. I think I might need the ``-R``, as the "list.txt" contain the list of directories for file names. They are not folder names. But correct me if I am wrong. – Catherine Jul 13 '21 at 17:05
  • i updated answer as per your requirement please check – saqib kafeel Jul 13 '21 at 18:17
  • I found out the file directory in the list.txt is not set as ``/Home/dir/file_name``. It was ``~/dir/file_name?``. Thank you for helping me! – Catherine Jul 14 '21 at 19:09