I want Linux to copy a large number of files while maintaining a directory structure.

Asked 1 years ago, Updated 1 years ago, 101 views

To copy a file named *.x under source_dir (with the exception of files with different extensions such as *.y in the same directory hierarchy) to destination_dir while maintaining the directory structure,

cd source_dir/
cp --parent*/*/*.x destination_dir/

You can achieve this by doing the following:

However, if you have a lot of *.x, you get an error when you oversize the command line.In such a case, what should I do?

For example,

cd source_dir/
find.-name "*.x" | xargstar cf- | tar xf - -C destination_dir /

It seems that you can do it like this.

linux command-line

2022-09-30 21:42

3 Answers

If you want to copy files in a relative path, you may want to extract the name of the file you want to copy with find (1), archive it with cpio (1), and deploy it with cpio (1).

Example:

$(
    cd/path/to/src/dir&find.-type f-name'*.x'-print0 | cpio-o0
  ) \
  |(
    cd/path/to/dst/dir&&cpio-id
  )


2022-09-30 21:42

The comment says `rsync exclude option, but I think it was a mistake to include.Therefore, rsync can be used to narrow down the scope of the include option.However, when copying directories recursively, it is a bit complicated.

rsync-a --include='*/' --include='*.x' -- exclude='*'src/dst

You must specify it like this

You can use xargs in tar, but you can use the --files-from option to specify a list of paths in a file.

cd source_dir/
find.-name "*.x" | tar cf -- files-from- | tar xf -- C destination_dir /

If you have enough files to require xargs, I think it would be more efficient to have tar read the list directly.


2022-09-30 21:42

What about the GNU install command? Similar to the cp command, the -D option automatically creates the directory hierarchy from which you want to move.

$findsource_dir-name\*.x | xargs-I'{}'install-vD'{}' destination_dir/{}'

The above command example does not consider copying file attributes (update date, permissions, etc.).Add options as appropriate


2022-09-30 21:42

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.