620

When copying a file using cp to a folder that may or may not exist, how do I get cp to create the folder if necessary? Here is what I have tried:

[root@file nutch-0.9]# cp -f urls-resume /nosuchdirectory/hi.txt
cp: cannot create regular file `/nosuchdirectory/hi.txt': No such file or directory
3
  • 3
    @nelaar The age of the question is a secondary concern; the quality and breadth of the answers should be the deciding factor. I don't have a strong preference either way, but I don't think it's worth the effort at this point to turn around the duplicate relationship. If you think otherwise, please offer a rationale (perhaps on meta.stackoverflow.com for proper visibility and process).
    – tripleee
    Commented Sep 28, 2015 at 17:04
  • Looked for the same thing and could not find my answer below so will post how I ended up doing this: dirname "/nosuchdirectory/hi.txt" | while read path;do mkdir -p "$path"; done && cp -f urls-resume /nosuchdirectory/hi.txt
    – Greg0ry
    Commented Nov 24, 2016 at 19:58
  • See e.g. meta.stackoverflow.com/questions/251938/…
    – tripleee
    Commented Dec 19, 2016 at 5:38

7 Answers 7

403

To expand upon Christian's answer, the only reliable way to do this would be to combine mkdir and cp:

mkdir -p /foo/bar && cp myfile "$_"

As an aside, when you only need to create a single directory in an existing hierarchy, rsync can do it in one operation. I'm quite a fan of rsync as a much more versatile cp replacement, in fact:

rsync -a myfile /foo/bar/ # works if /foo exists but /foo/bar doesn't.  bar is created.
9
  • 8
    It's mentioned in the above answer but I'd like to re-emphasize it (after a few minutes wasted because of not noticing the fine print): The rsync command above is not equivalent to the mkdir/cp command above. It just creates a single level of folder. Actually I'm not sure when it can be useful. Commented Apr 13, 2015 at 14:30
  • 14
    Also, for anyone wondering about the -p argument to mkdir, it's documented in the POSIX standard at pubs.opengroup.org/onlinepubs/9699919799/utilities/mkdir.html and causes intermediate pathname components to be created (i.e. lets you mkdir foo/bar/baz even if foo or bar don't already exist).
    – Mark Amery
    Commented Sep 13, 2015 at 21:52
  • 14
    "$_" is a bash variable - LINK FOR MORE INFO.
    – Aakash
    Commented Feb 21, 2017 at 5:00
  • 1
    Some more info about the bash variables: stackoverflow.com/a/5163260/3982562
    – 3limin4t0r
    Commented May 15, 2018 at 15:58
  • 4
    @PengheGeng : yes, this rsync command can only create one level of folders, but rsync has options to create them all, or only some. rsync: how can I configure it to create target directory on server?
    – mivk
    Commented Aug 17, 2018 at 11:30
136

I didn't know you could do that with cp.

You can do it with mkdir ..

mkdir -p /var/path/to/your/dir

EDIT See lhunath's answer for incorporating cp.

7
  • 49
    cp(1) doesn't. mkdir -p /foo/bar && cp myfile "$_" is indeed the only way to do this reliably.
    – lhunath
    Commented Jun 4, 2009 at 5:53
  • Check this answer - stackoverflow.com/a/34608146/2007944
    – SD.
    Commented Jan 5, 2016 at 9:27
  • This anwser should not be here. The question was regarding cp not mkdir ...
    – basickarl
    Commented Aug 4, 2017 at 7:58
  • Hi @KarlMorrison this is an 8 year old question & answer. I also stated that it was for mkdir and not cp.
    – Christian
    Commented Aug 6, 2017 at 7:08
  • @Christian I know, it is 2017 and this is the answer directly under the question, which it shouldn't be. I know that you stated that, however people like me actually looking for an answer regarding the cp command is met with answers like this which have nothing to do with cp prolonging my search for a correct answer. The only answer here which is accurate is stackoverflow.com/a/947971/1137669 he states that cp cannot do it. No one else (including yourself) state that it is not possible with cp.
    – basickarl
    Commented Aug 7, 2017 at 8:59
43

One can also use the command find:

find ./ -depth -print | cpio -pvd newdirpathname
1
  • 1
    this is by far the best answer because it allows you to create directories without previously needing to know their full path. Commented Oct 27, 2021 at 20:59
37
 mkdir -p `dirname /nosuchdirectory/hi.txt` && cp -r urls-resume /nosuchdirectory/hi.txt
0
23

There is no such option. What you can do is to run mkdir -p before copying the file

I made a very cool script you can use to copy files in locations that doesn't exist

#!/bin/bash
if [ ! -d "$2" ]; then
    mkdir -p "$2"
fi
cp -R "$1" "$2"

Now just save it, give it permissions and run it using

./cp-improved SOURCE DEST

I put -R option but it's just a draft, I know it can be and you will improve it in many ways. Hope it helps you

2
  • 3
    Quote your parameter expansions, please. Or you'll suffer bugs introduced by wordsplitting and pathname expansion. Put "" around all your $foo's.
    – lhunath
    Commented Jun 4, 2009 at 5:50
  • 9
    That will lead to weird results if someone tries to rename the file while coping: cp-improved /foo/file.jpg /bar/file.jpg.bak. I will end up with /bar/file.jpg.bak/file.jpg regardless if bar exists or not
    – MestreLion
    Commented Aug 5, 2011 at 23:49
9

rsync is work!

#file:
rsync -aqz _vimrc ~/.vimrc

#directory:
rsync -aqz _vim/ ~/.vim
1
  • 4
    doesnt work .. rsync will only create one directory for you.
    – mjs
    Commented Nov 7, 2019 at 13:23
-9
cp -Rvn /source/path/* /destination/path/
cp: /destination/path/any.zip: No such file or directory

It will create no existing paths in destination, if path have a source file inside. This dont create empty directories.

A moment ago i've seen xxxxxxxx: No such file or directory, because i run out of free space. without error message.

with ditto:

ditto -V /source/path/* /destination/path
ditto: /destination/path/any.zip: No space left on device

once freed space cp -Rvn /source/path/* /destination/path/ works as expected

0

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