I want to create an empty directory with some permissions and owned by user apache:apache. I could just mkdir it, chown it and chmod it, but in directory where I want to create it I have a directory with just the permissions and owner I need.

So I want to do sth like: cp old_dir new_dir but with permissions and owners. To preserve the permissions option -p is used, so the command should be cp -p old_dir new_dir. But that one returns the error: cp: omitting directory ‘field’.

It would work with options -rp or better -a but old_dir contains some dirs and files and I just want to copy the empty directory.

I also tried using --parents option (cp -p --parents old_dir new_dir) which creates any missing intermediate directories but then I get this error: cp: with --parents, the destination must be a directory.

How do I do that? (bonus points for not using ; and &&)

  • 1
    cp -p does what you want, it omits the underlying directories. so just the files in that directory are copied over. I have a feeling this is not an answer to you question, but your question is unclear. Do you just want to copy the dir structure with original permissions? – aairey Jul 5 '16 at 9:52
  • Adding -T could help – Keyshov Borate Jul 5 '16 at 9:53
  • @aairey I know cp -p does what it's supposed to but not what I want. I want to clone old_dir with all permissions and name new_dir. – user1 Jul 5 '16 at 10:09
  • @Niranjan It doesn't. Same errors. – user1 Jul 5 '16 at 10:10
  • 1
    @Hans-MartinMosner I obviously did mkdir+chmod+chown in the first place but just after that asked this question which took me 10 mins. Now I'll know how to do that in the future using only one command. :-) – user1 Jul 5 '16 at 15:03

You could use rsync for that job.

rsync -av --exclude=/<old_dir>/* /<old_dir> /<new_dir>
| improve this answer | |

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.