Jaggu
Jaggu

Reputation: 23

UNIX: copy entire directory structure and only specific file type

How i can recreate entire directory structure and copy only files with .txt extension if present. I would like to copy the directory or sub-directory even if its empty.

Upvotes: 1

Views: 926

Answers (3)

choroba
choroba

Reputation: 241848

To create the directory structure:

cd source-dir
find . -type d -exec mkdir target-dir/{} \;

You can ignore the message that . cannot be created.

Then, copy the txt files into it:

find . -type f -name '*.txt' -exec cp {} target-dir/{} \;

Upvotes: 0

ris
ris

Reputation: 1

cd source-dir
find . -type d -mindepth 1 -exec mkdir target-dir/{} \;

to avoid creating directory "." and error "No such file or directory" respectively.

Upvotes: 0

Jaggu
Jaggu

Reputation: 23

here is one line command :)

rsync -vapEtogR --progress --stats --log-file="log_file_name.txt" --include '*/' --include '*.txt' --exclude '*' source_path/ destination_path/

Use rsync --help to know about the options used.

Upvotes: 1

Related Questions