0

I would like to copy many files. I have their location as a set of symbolic links whose names are stored as lines in a txt file. So I am using cp and need to combine the readlink and cat/pipe functions. I haven't seen other questions deal with this combination.

The command:

cp $(readlink < /path/list.txt) -t /path/targetdir

A simpler example works where the symlink names are used explicitly in the argument of the readlink (but I have too many file names to do this manually), e.g.:

cp $(readlink symlink1 symlink2) -t /path/targetdir

The issue is in how I am combining the 'readlink' and the pipe, but I can't figure out what.

EDIT: here's an example of what's in the first lines of the list.txt file:

symlink_13243

symlink_39184

symlink_83204

each row is a unique symlink name.

4
  • Can you provide an example of a few of your file entries please. In your question, not here as a comment
    – roaima
    Mar 29, 2021 at 22:40
  • Use xargs when an argument list is too long. What's wrong with the first command? Mar 29, 2021 at 23:10
  • The first command gives two errors: readlink: missing operand and cp: missing operand
    – clchi21
    Mar 30, 2021 at 0:53
  • no, neither spaces nor hyphens. They are fasta file names: e.g. file.1234.fasta
    – clchi21
    Mar 30, 2021 at 1:20

2 Answers 2

0

Using GNU xargs and readlink:

</path/list.txt xargs -d'\n' readlink -z | xargs -0 cp -t /path/targetdir

With one symbolic link per line in list.txt, choose newline (\n) as delimiter for the first xargs and pipe readlink's output to another xargs, this time using a null character to terminate the paths (-z and -0).

1
  • thanks! but it only copies the last file of the list. Anything I should add or try?
    – clchi21
    Mar 30, 2021 at 2:00
0

It worked by using a loop and cp -L instead of readlink. I ran the following command in the folder that contained the symlinks:

for item in $(cat /path/dir/list.txt); do cp -L $item /path/targetdir; done

also I had encoding issues with the txt file (related to building it in R but on a Windows machine).

You must log in to answer this question.

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