cp won't work
Your example as it stands will not work because copy doesn't copy directory structures, it will only copy the files, hence the error message you're encountering. To do a deep copy such as this you can enlist either the tar
command and use the construct tar cvf - --files-from=... | (cd /home/tmp/test/files/; tar xvf -)
or you can just use rsync
.
rsync
If I were you I'd use rsync
to do this like so:
$ rsync -avz --files-from=abc.txt /src /home/tmp/test/files/.
If you only want the 1st 100 lines from file abc.txt
you can do this:
$ rsync -avz --files-from=<(head -n 100 abc.txt) /src /home/tmp/test/files/.
Example
Sample folder data:
$ tree /home/saml/tmp/folder*
/home/saml/tmp/folder1
`-- example.tar.gz
/home/saml/tmp/folder2
`-- example.tar.gz
/home/saml/tmp/folder3
`-- example.tar.gz
Now copy the files:
$ rsync -avz --files-from=<(head -n 3 /home/saml/tmp/abc.txt) \
/home/saml/tmp/. /home/saml/tmp/test/files/.
building file list ... done
./
folder1/
folder1/example.tar.gz
folder2/
folder2/example.tar.gz
folder3/
folder3/example.tar.gz
sent 3147093 bytes received 81 bytes 6294348.00 bytes/sec
total size is 3145728 speedup is 1.00
Confirm they were copied:
$ tree /home/saml/tmp/test/files
/home/saml/tmp/test/files
|-- folder1
| `-- example.tar.gz
|-- folder2
| `-- example.tar.gz
`-- folder3
`-- example.tar.gz
tar
If you interested here's how you do it using just tar
.
$ cd /home/saml/tmp
$ tar cvf - --files-from=<(head -n 3 /home/saml/tmp/abc.txt) | (cd /home/saml/tmp/test/files/; tar xvf -)
./folder1/example.tar.gz
./folder1/example.tar.gz
./folder2/example.tar.gz
./folder2/example.tar.gz
./folder3/example.tar.gz
./folder3/example.tar.gz
Confirm that it copied:
$ tree /home/saml/tmp/test/files
/home/saml/tmp/test/files
|-- folder1
| `-- example.tar.gz
|-- folder2
| `-- example.tar.gz
`-- folder3
`-- example.tar.gz
Best Answer
Use the
-T
option to cp (GNUcp
):If you use
rsync
for this (which is probably what you want since it will avoid copying files which haven't changed), you can append a/
to the source directory so that specifically the contents are copied rather than the directory itself. Eg: