Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i copy only header files in an entire nested directory to another directory keeping the same hierarchy after copying to new folder

Tags:

linux

shell

cp

I have a directory which has a lot of header files(.h) and other .o and .c files and other files. This directory has many nested directories inside. I want to copy only header files to a separate directory preserving the same structure in the new directory.

cp -rf oldDirectory newDirectory will copy all files. I want to copy only header files.

like image 520
avimonk Avatar asked Apr 16 '12 15:04

avimonk


People also ask

How to copy folders & subfolders from one location to another?

If you want to also copy folders & subfolders from one location to another then you need to use -R option for recursive copying. Here is an example to copy all files & folders present in /home/ubuntu to /home/data Copying files & folders is a common task in system administration and it is always convenient to have a shell script for it.

How do I copy a file from one directory to another?

The “ cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command. The syntax of the “ cp ” command is: $ cp [ options] [source …] [ destination…]

How to copy and paste header and footer at same time?

Press Ctrl + C keys together to copy the page. 5. Create a new Word document, and press Ctrl + V keys together to paste the page. And now you will see the specified page and its header and footer are copied to the target document at the same time.

How to copy files and folders in Linux?

The “ cp” command is one of the commonly used commands to perform the copy operation. You can copy files or folders from source to destination, i-e, one directory through this command.


2 Answers

(cd src && find . -name '*.h' -print | tar --create --files-from -) | (cd dst && tar xvfp -)

You can do something similar with cpio if you just want to hard link the files instead of copying them, but it's likely to require a little mv'ing afterward. If you have lots of data and don't mind (or need!) sharing, this can be much faster. It gets confused if dst needs to have a src in it - this is, if it isn't just a side effect:

  1. find src -name '*.h' -print | cpio -pdlv dst
  2. mv dst/src/* dst/.
  3. rmdir dst/src
like image 162
user1277476 Avatar answered Nov 15 '22 08:11

user1277476


this one worked for me:

rsync -avm --include='*.h' -f 'hide,! */' . /destination_dir

from here

like image 30
Ben Avatar answered Nov 15 '22 07:11

Ben