oezlem
oezlem

Reputation: 247

finding and copying files with directory names

I have a folder with a lot of subdirectories. I want to find html.txt files and move them into a new directory with the same structure. I´m using

find . -name "*.html.txt" | cp -t /home/....

but it is not keeping the name and directory structure. Is there anyway to do this?

Upvotes: 0

Views: 88

Answers (1)

Casperrw
Casperrw

Reputation: 531

Try:

find . -name '*.html.txt' | xargs cp -t /home --parents

xargs is often useful to turn various lists into a list of arguments, especially with the output of 'find'. You can probably also do this with an exec command from within find, but I always find that harder to quickly get right.

--parents does the trick of creating all the parent directories as they are in the list of files.

I'm using this type of command in csh, and it should work in bash as well.

Mostly answered here already: https://serverfault.com/questions/180853/how-to-copy-file-preserving-directory-path-in-linux

Upvotes: 1

Related Questions