H
H
homm2011-07-30 18:19:37
rsync
homm, 2011-07-30 18:19:37

How to copy a list of files in Linux while preserving the directory tree?

There is a text file, each line in which is the path to the file to be copied. The difficulty is that the path contains subdirectories. I.e.:
a/a/file1.txt
a/a/file2.txt
a/b/file1.txt
a/c/file1.txt
etc.
If I do it through xargs and cp, then it turns out that all files are added to one destination folder, but you need to save the directory structure.
If I do it via rsync --include-from, then it requires that all directory names be specified along with the file names, but they are not in the file. Besides, I sincerely believe that this is redundant information.
You can't make hard links through rsync either, which can come in handy.

Answer the question

In order to leave comments, you need to log in

3 answer(s)
H
homm, 2011-07-30
@homm

I answer myself:
The cp command has the --parents parameter, which is why the result of the
cp --parents a/b/file1.txt ./files command
will be copying the file file1.txt to ./files/a/b/ It, the cp command, has the wonderful --link option, which creates hard links instead of copying.

O
ob1, 2011-07-30
@ob1

You can also put the files into a tar archive and then unpack it in the right place. This will be a more generic solution than the GNU cp options.

S
ShamblerR, 2015-02-18
@ShamblerR

find . -regex ".*\.\(jpg\|jpeg\|gif\|png\|JPG\|JPEG\|GIF\|PNG\)" -print0 | xargs -0 cp --parents --target-directory ./tmp/backup

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question