I'm trying to write a makefile rule to copy a directory, maintaining its structure, and since all the other rules in our makefiles use install, I wanted to be consistent.

In the manpage, it says:


   install [OPTION]... [-T] SOURCE DEST
   install [OPTION]... SOURCE... DIRECTORY
   install [OPTION]... -t DIRECTORY SOURCE...
   install [OPTION]... -d DIRECTORY...

   -d, --directory
          treat all arguments as directory names; create all components of
          the specified directories

OK, that sounds like what I need... but the flags don't make sense. How do you specify the destination directory to install to?

I tried doing a basic test by making an arbitrary directory structure on my local hard disk:

~>tree test
├── a
│   └── b
│       └── c
│           └── e.txt
└── d

4 directories, 1 file

And then running install -d and looking at what was created:

~>install -d test test2
~>tree test2

0 directories, 0 files

Nothing happened!

Can anyone point me in the right direction? Googling "gnu install -d flag" isn't bringing me much.


It looks like the install -D command is actually what I want.


-D create all leading components of DEST except the last, then copy SOURCE to DEST

Works great, except you have to specify every file individually.

  • 8
    I was excited right up until the end, "you have to specify every file individually". Guess I'll stick with cp -r – dtmland Jul 8 '15 at 20:11
  • 7
    Does the install command have an equivalent to cp -r? – Alexander Jul 8 '16 at 10:53

install -d is just used to create directories. You told it to create two directories, test and test2. test already existed, so all it needed to do was make test2. I don't think install supports copying entire directory trees; it's normally used on files. You probably need to use cp

  • 2
    Basically, install -d is the same as mkdir except that you can specify the mode, owner, SELinux context, and group all at once. – ZiggyTheHamster Aug 28 '17 at 19:40
  • I would say install -d is like mkdir -p – Felipe Alvarez Dec 17 '19 at 6:58

Usually what you want is to install files at right folder, without repeating your self. You can use find and install to help to keep your installation scripts more DRY

find SOURCE/ -type f -exec install -vDm 755 {} THERE/{} \;

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.