31

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:

SYNOPSIS

   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
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
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.

3 Answers 3

20

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

Manpage:

-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.

2
  • 9
    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, 2015 at 20:11
  • 8
    Does the install command have an equivalent to cp -r?
    – Alexander
    Jul 8, 2016 at 10:53
15

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
  • 4
    Basically, install -d is the same as mkdir except that you can specify the mode, owner, SELinux context, and group all at once. Aug 28, 2017 at 19:40
  • 2
    I would say install -d is like mkdir -p Dec 17, 2019 at 6:58
4

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/{} \;
4
  • Wy not hust cp -r? Mar 28, 2021 at 9:59
  • With install you can control mode and there are more options that cp doesn't
    – geckos
    Mar 30, 2021 at 3:37
  • With the command as-is, a SOURCE/foo.txt file will be copied to THERE/SOURCE/foo.txt, which may not be what you want?
    – baltakatei
    Apr 15 at 9:05
  • This command can be made to behave like cp -r if you change the working directory to SOURCE/ before running find on it and make THERE/ an absolute path: SOURCE=some_dir/; THERE=some_other_dir/; THERE="$(readlink -f "$THERE")"; pushd "$SOURCE"; find ./ -type f -exec install -vDm 0755 {} "$THERE"/{} \; ; popd;.
    – baltakatei
    Apr 15 at 9:32

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.