Linux: copy and create destination dir if it does not exist
Asked Answered
P

29

625

I want a command (or probably an option to cp) that creates the destination directory if it does not exist.

Example:

cp -? file /path/to/copy/file/to/is/very/deep/there
Procure answered 7/10, 2009 at 6:55 Comment(2)
possible duplicate of How to have the cp command create any necessary folders for copying a file to a destinationMoony
That looks like a nice feature request for cp. Any chance we could get such a command line switch in the near future?Jeroldjeroma
D
623
mkdir -p "$d" && cp file "$d"

(there's no such option for cp).

Duenna answered 7/10, 2009 at 6:58 Comment(6)
mkdir is a builtin in BusyBox ;)Imray
@holms, $d is a variable that presumably contains the directory in question. I mean, if you have to repeat it thrice you're likely to assign it to variable first.Duenna
how about cp -r file "$d" ?Welt
@Welt won't work, will say "directory doesn't exist" if you supply trailing / or copy as an intended directory if you don't and parent directory exists. It's to recursively copy directory, not to create directories.Duenna
@MichaelKrelin-hacker what does $d equal?Sen
@noobprogrammer1987, the target directory.Duenna
N
344

If both of the following are true:

  1. You are using the GNU version of cp (and not, for instance, the Mac version), and
  2. You are copying from some existing directory structure and you just need it recreated

then you can do this with the --parents flag of cp. From the info page (viewable at http://www.gnu.org/software/coreutils/manual/html_node/cp-invocation.html#cp-invocation or with info cp or man cp):

--parents
     Form the name of each destination file by appending to the target
     directory a slash and the specified name of the source file.  The
     last argument given to `cp' must be the name of an existing
     directory.  For example, the command:

          cp --parents a/b/c existing_dir

     copies the file `a/b/c' to `existing_dir/a/b/c', creating any
     missing intermediate directories.

Example:

/tmp $ mkdir foo
/tmp $ mkdir foo/foo
/tmp $ touch foo/foo/foo.txt
/tmp $ mkdir bar
/tmp $ cp --parents foo/foo/foo.txt bar
/tmp $ ls bar/foo/foo
foo.txt
Norinenorita answered 4/1, 2012 at 6:9 Comment(11)
This doesn't work on Mac OS X, so I guess it's Linux specific.Adventure
I'd say gnu-specific. If you have macports you can install coreutils and its gcp.Duenna
Man, linux has everythingSpinner
I feel like this is the answer to a slightly different question, even though it is the answer I was looking for (and therefore the one I upvoted). I guess this is the solution assuming you want the destination parent directories to be the same as the origin parent directories, which is probably the use case that most people reading this are interesting in.Beeswax
This assumes directory 'bar' already exists, but the original question implies how to automatically create 'bar' when it's provided as the destination and it doesn't already exist. The answer to this is provided by @MichaelKrelin-hacker.Ingenerate
WOW that is a great resource, i can even copy many files at once provided the destination exists: cp --parents foo/bar/z.txt foo/baz/r.csv bar/foo/h.doc /previously/madedir/Raddle
better yet it can copy dirs with cp -R --parents my/needed/dir/ /previously/madedir/ then check it with tree !$Raddle
--parents has its uses, but it won't work if, say, you want to base the directory creation only on the destination path (e.g., copy file c in directory a/b into directory d/e/f and create the destination path if necessary).Ingenerate
The man pages for --parents is, IMHO, too brief and confusing: use full source file name under DIRECTORY. The full documentation via info cp is quite nice, though.Typewriter
The Problems is with mkdir bar part. How to implement it inside cp command.Failure
bar is the destination root in my example. You'd usually have this as the target for your copy. If you are asking to create it and it is not part of the copy parents, you'd just have to do the mkdir.Norinenorita
A
153

Short Answer

To copy myfile.txt to /foo/bar/myfile.txt, use:

mkdir -p /foo/bar && cp myfile.txt $_

How does this work?

There's a few components to this, so I'll cover all the syntax step by step.

The mkdir utility, as specified in the POSIX standard, makes directories. The -p argument, per the docs, will cause mkdir to

Create any missing intermediate pathname components

meaning that when calling mkdir -p /foo/bar, mkdir will create /foo and /foo/bar if /foo doesn't already exist. (Without -p, it will instead throw an error.

The && list operator, as documented in the POSIX standard (or the Bash manual if you prefer), has the effect that cp myfile.txt $_ only gets executed if mkdir -p /foo/bar executes successfully. This means the cp command won't try to execute if mkdir fails for one of the many reasons it might fail.

Finally, the $_ we pass as the second argument to cp is a "special parameter" which can be handy for avoiding repeating long arguments (like file paths) without having to store them in a variable. Per the Bash manual, it:

expands to the last argument to the previous command

In this case, that's the /foo/bar we passed to mkdir. So the cp command expands to cp myfile.txt /foo/bar, which copies myfile.txt into the newly created /foo/bar directory.

Note that $_ is not part of the POSIX standard, so theoretically a Unix variant might have a shell that doesn't support this construct. However, I don't know of any modern shells that don't support $_; certainly Bash, Dash, and zsh all do.


A final note: the command I've given at the start of this answer assumes that your directory names don't have spaces in. If you're dealing with names with spaces, you'll need to quote them so that the different words aren't treated as different arguments to mkdir or cp. So your command would actually look like:

mkdir -p "/my directory/name with/spaces" && cp "my filename with spaces.txt" "$_"
Aquino answered 15/9, 2015 at 22:40 Comment(9)
Aside: I'm usually disappointed in the lack of detail in questions about Bash or Unix shell commands on Stack Overflow, which frequently throw out a complicated and extremely dense one-liner that's hard to unpick if you're not already a Unix wizard. I've tried to go for the opposite extreme here and both explain every detail of the syntax and link to the appropriate places to find documentation on how this stuff works. I hope this helps at least some folks. Also, credit where due: I nicked this approach from this answer to a duplicate question: https://mcmap.net/q/64139/-how-to-have-the-cp-command-create-any-necessary-folders-for-copying-a-file-to-a-destination-duplicateAquino
I've never seen $_ before. what does it refer to? the folder used in the last command?Bozuwa
@Bozuwa But... but... there's a "How does this work?" section in my answer that literally contains multiple paragraphs specifically devoted to the question you just asked. It feels ignored and unloved. :(Aquino
Sorry about that. You're absolutely right. I already knew everything else in your answer so I just quickly scanned through it. I should have read it more carefully.Bozuwa
Wow, you really did put alot of work into that answer... Thanks for that. It was nice to learn about: $_. I can see myself using it quite a bit from now on. I wrote a script to automate the whole process and put up in this thread. Give it a go, maybe you'll like: #1530446Bozuwa
I just did mkdir -p /foo/bar && touch $_/myfile.txt to create a new file in a non existent directory. Worked like a charm.Alternation
minor detail : one could also: mkdir -p /foo/bar && cp ../from/another/dir/myfile.txt $_/my-file-with-new-nameCairo
@MarkAmery have you tried this in AIX? It does not seem to be working. Usage: cp [-fhipHILPU][-d|-e] [-r|-R] [-E{force|ignore|warn}] [--] src target or: cp [-fhipHILPU] [-d|-e] [-r|-R] [-E{force|ignore|warn}] [--] src1 ... srcN directory Cavour
For those looking to automate, the cp --parents and rarchive answers may likely be better. This is because the mkdir -p part of this answer takes just a directory, requiring you to script an extra step to parse the directory out from the full filename.Burgee
D
70

Such an old question, but maybe I can propose an alternative solution.

You can use the install programme to copy your file and create the destination path "on the fly".

install -D file /path/to/copy/file/to/is/very/deep/there/file

There are some aspects to take in consideration, though:

  1. you need to specify also the destination file name, not only the destination path
  2. the destination file will be executable (at least, as far as I saw from my tests)

You can easily amend the #2 by adding the -m option to set permissions on the destination file (example: -m 664 will create the destination file with permissions rw-rw-r--, just like creating a new file with touch).


And here it is the shameless link to the answer I was inspired by =)

Dextroglucose answered 7/4, 2015 at 10:11 Comment(2)
note that this command creates the destination files with 755 (rwxr-xr-x) permissions, which is probably not desired. you can specify something else with the -m switch, but I could not find a way to just keep the file permissions :(Roethke
to ensure proper file ownership/mask: install -D /path/to/source /path/to/dest/dir -o some-user -m 0754Sesquicentennial
G
29

Shell function that does what you want, calling it a "bury" copy because it digs a hole for the file to live in:

bury_copy() { mkdir -p `dirname $2` && cp "$1" "$2"; }
Grow answered 7/10, 2009 at 22:4 Comment(6)
You should have quotation marks around the dirname $2 tooHem
@Kalmi, for proper quotation you'd also want to quote $2 as an argument to dirname. Like mkdir -p "$(dirname "$2")". Without this quoting $2 in cp is useless ;)Duenna
Note that this answer assumes $2 is not already the target directory, otherwise you'd just want "mkdir -p "$2" && cp "$1" "$2" as the function bodyTestee
I think this has a bug, as @Testee points out. I don't think this is fixable, as $2 is ambiguous between the target dir and the target file in this scenario. I have posted an alternative answer which addresses this.Francenefrances
@Rich, I disagree that this is not fixable. The following works just fine for both files and directories: mkdir -p dirname "$2" && cp -r "$1" "$2"; Note that the backticks around dirname $2 don't show up because SO parses them as code markers.Tradesman
@Yury, what I mean is that if you do "cp x a/b" and there is not yet a directory "b", then the invocation is ambiguous between renaming the file to "b" inside directory "a", or moving the file to a new directory "b" inside "a" (i.e. "a/b/x"). See my answer for more info.Francenefrances
C
13

Here's one way to do it:

mkdir -p `dirname /path/to/copy/file/to/is/very/deep/there` \
   && cp -r file /path/to/copy/file/to/is/very/deep/there

dirname will give you the parent of the destination directory or file. mkdir -p `dirname ...` will then create that directory ensuring that when you call cp -r the correct base directory is in place.

The advantage of this over --parents is that it works for the case where the last element in the destination path is a filename.

And it'll work on OS X.

Calais answered 9/2, 2014 at 15:23 Comment(1)
Is this part creating 2 directories ` dirname /path/to/copy/file/to/is/very/deep/there `?Cavour
U
10

This is very late but it may help a rookie somewhere. If you need to AUTO create folders rsync should be your best friend.

rsync /path/to/sourcefile /path/to/tragetdir/thatdoestexist/
Unmade answered 16/4, 2020 at 23:45 Comment(0)
K
9

with all my respect for answers above, I prefer to use rsync as follow:

$  rsync -a directory_name /path_where_to_inject_your_directory/

example:

$ rsync -a test /usr/local/lib/
Keelykeen answered 22/9, 2017 at 16:56 Comment(4)
I tried it and go this result: rsync -a bull /some/hoop/ti/doop/ploop RESULT rsync: mkdir "/some/hoop/ti/doop/ploop" failed: No such file or directory (2) rsync error: error in file IO (code 11) at main.c(675) [Receiver=3.1.2]Bozuwa
@Bozuwa the you have to check the path with pwd command (look at :[link ] (access.redhat.com/documentation/en-US/Red_Hat_Enterprise_Linux/… )) and insert correctly it into your instructionKeelykeen
If I understand your suggestion correctly, you're proposing that I specify a complete path to the source aka use: "$(pwd)/bull" in my command instead of just the filename: bull? The thing is, the error is with rsync creating destination dir, not the source (bull). Seems to me rsync uses a simple mkdir and not a mkdir -p.Bozuwa
rsync is more predictable than cp. For example, if I wish to cp /from/somedir /to-dir then cp behaves differently if /to-dir exists already: If /to-dir exists, then cp will create /to-dir/some-dir, otherwise just the contents of /from/somedir are placed into /to-dir . However, rsync behaves the same in case, i.e, /to-dir/some-dir will always be created.Arthur
T
8

install -D file -m 644 -t /path/to/copy/file/to/is/very/deep/there

Thurgau answered 27/10, 2016 at 22:41 Comment(2)
actually, it's not the same at all. his requires you to pass the filename twice (hence the '-t' flag) and it set the execute bits on the file (hence the '-m' example). his should not be the top answer, since it doesn't actually answer the question - whereas mine does.Thurgau
This works for a single file. Just take into account that there is the final file and not the final subdirectory.Jacquijacquie
G
8

This does it for me

cp -vaR ./from ./to
Garate answered 3/2, 2018 at 15:41 Comment(1)
Agree. In man cp: -R If source_file designates a directory, cp copies the directory and the entire subtree connected at that point.Ethology
G
8

As suggested above by help_asap and spongeman you can use the 'install' command to copy files to existing directories or create create new destination directories if they don't already exist.

Option 1 install -D filename some/deep/directory/filename
copies file to a new or existing directory and gives filename default 755 permissions

Option 2 install -D filename -m640 some/deep/directory/filename
as per Option 1 but gives filename 640 permissions.

Option 3 install -D filename -m640 -t some/deep/directory/
as per Option 2 but targets filename into target directory so filename does not need to be written in both source and target.

Option 4 install -D filena* -m640 -t some/deep/directory/
as per Option 3 but uses a wildcard for multiple files.

It works nicely in Ubuntu and combines two steps (directory creation then file copy) into one single step.

Gilley answered 23/6, 2019 at 22:24 Comment(0)
E
6

Simply add the following in your .bashrc, tweak if you need. Works in Ubuntu.

mkcp() {
    test -d "$2" || mkdir -p "$2"
    cp -r "$1" "$2"
}

E.g If you want to copy 'test' file to destination directory 'd' Use,

mkcp test a/b/c/d

mkcp will first check if destination directory exists or not, if not then make it and copy source file/directory.

Egregious answered 5/1, 2016 at 9:26 Comment(1)
As others commented on the other answer you don’t need to test if the directory exists before calling mkdir -p. It’ll succeed even if it already exists.Average
O
6

Just to resume and give a complete working solution, in one line. Be careful if you want to rename your file, you should include a way to provide a clean dir path to mkdir. $fdst can be file or dir. Next code should work in any case.

fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p $(dirname ${fdst}) && cp -p ${fsrc} ${fdst}

or bash specific

fsrc=/tmp/myfile.unk
fdst=/tmp/dir1/dir2/dir3/myfile.txt
mkdir -p ${fdst%/*} && cp -p ${fsrc} ${fdst}
Outfield answered 21/8, 2018 at 8:50 Comment(1)
Thanks! This is what I needed. In my case I don't know whether the destination has directory or not and whether the directory exists or not so this code gives me the parent directory which then I can create it safely if it doesn't existGotthelf
G
6

Simply without creating script and with simple command ...

mkdir -p /destination-folder/ && cp file-name /destination-folder/
Globin answered 14/10, 2021 at 13:25 Comment(0)
F
4

cp has multiple usages:

$ cp --help
Usage: cp [OPTION]... [-T] SOURCE DEST
  or:  cp [OPTION]... SOURCE... DIRECTORY
  or:  cp [OPTION]... -t DIRECTORY SOURCE...
Copy SOURCE to DEST, or multiple SOURCE(s) to DIRECTORY.

@AndyRoss's answer works for the

cp SOURCE DEST

style of cp, but does the wrong thing if you use the

cp SOURCE... DIRECTORY/

style of cp.

I think that "DEST" is ambiguous without a trailing slash in this usage (i.e. where the target directory doesn't yet exist), which is perhaps why cp has never added an option for this.

So here's my version of this function which enforces a trailing slash on the dest dir:

cp-p() {
  last=${@: -1}

  if [[ $# -ge 2 && "$last" == */ ]] ; then
    # cp SOURCE... DEST/
    mkdir -p "$last" && cp "$@"
  else
    echo "cp-p: (copy, creating parent dirs)"
    echo "cp-p: Usage: cp-p SOURCE... DEST/"
  fi
}
Francenefrances answered 16/11, 2015 at 9:51 Comment(1)
Only looking at the last argument and then doing cp "$@" is a great idea, that way other options passed to the function are passed to cp. And you're right that without trailing slash it's ambiguous, but you could treat it as a dir with trailing slash and as a file name without, i.e. in the else branch use mkdir -p "$(dirname "$last")" && cp "$@".Hippocampus
B
4

I wrote a support script for cp, called CP (note capital letters) that's intended to do exactly this. Script will check for errors in the path you've put in (except the last one which is the destination) and if all is well, it will do an mkdir -p step to create the destination path before starting the copy. At this point the regular cp utility takes over and any switches you use with CP (like -r, -p, -rpL gets piped directly to cp). Before you use my script, there are a few things you need to understand.

  • all the info here can be accessed by doing CP --help. CP --help-all include's cp's switches.
  • regular cp won't do the copy if it doesn't find the destination path. You don't have such a safety net for typos with CP. You're destination will be created, so if you misspell your destination as /usrr/share/icons or /usr/share/icon well that's what's going to be created.
  • regular cp tends to model it's behavior on the existing path: cp /a/b /c/d will vary on whether d exists or not. if d is an existing folder, cp will copy b into it, making /c/d/b. If d doesn't exist, b will be copied into c and renamed to d. If d exists but is a file and b is a file, it will be overwritten by b's copy. If c doesn't exist, cp doesn't do the copy and exits.

CP doesn't have the luxury of taking cues from existing paths, so it has to have some very firm behavior patterns. CP assumes that the item you're copying is being dropped in the destination path and is not the destination itself (aka, a renamed copy of the source file/folder). Meaning:

  • "CP /a/b /c/d" will result in /c/d/b if d is a folder
  • "CP /a/b /c/b" will result in /c/b/b if b in /c/b is a folder.
  • If both b and d are files: CP /a/b /c/d will result in /c/d (where d is a copy of b). Same for CP /a/b /c/b in the same circumstance.

This default CP behavior can be changed with the "--rename" switch. In this case, it's assumed that

  • "CP --rename /a/b /c/d" is copying b into /c and renaming the copy to d.

A few closing notes: Like with cp, CP can copy multiple items at a time with the last path being listed assumed to be the destination. It can also handle paths with spaces as long as you use quotation marks.

CP will check the paths you put in and make sure they exist before doing the copy. In strict mode (available through --strict switch), all files/folders being copied must exist or no copy takes place. In relaxed mode (--relaxed), copy will continue if at least one of the items you listed exists. Relaxed mode is the default, you can change the mode temporarily via the switches or permanently by setting the variable easy_going at the beginning of the script.

Here's how to install it:

In a non-root terminal, do:

sudo echo > /usr/bin/CP; sudo chmod +x /usr/bin/CP; sudo touch /usr/bin/CP
gedit admin:///usr/bin/CP 

In gedit, paste CP utility and save:

#!/bin/bash
#Regular cp works with the assumption that the destination path exists and if it doesn't, it will verify that it's parent directory does.

#eg: cp /a/b /c/d will give /c/d/b if folder path /c/d already exists but will give /c/d (where d is renamed copy of b) if /c/d doesn't exists but /c does.

#CP works differently, provided that d in /c/d isn't an existing file, it assumes that you're copying item into a folder path called /c/d and will create it if it doesn't exist. so CP /a/b /c/d will always give /c/d/b unless d is an existing file. If you put the --rename switch, it will assume that you're copying into /c and renaming the singl item you're copying from b to d at the destination. Again, if /c doesn't exist, it will be created. So CP --rename /a/b /c/d will give a /c/d and if there already a folder called /c/d, contents of b will be merged into d. 

#cp+ $source $destination
#mkdir -p /foo/bar && cp myfile "$_"

err=0 # error count
i=0 #item counter, doesn't include destination (starts at 1, ex. item1, item2 etc)
m=0 #cp switch counter (starts at 1, switch 1, switch2, etc)
n=1 # argument counter (aka the arguments inputed into script, those include both switches and items, aka: $1 $2 $3 $4 $5)
count_s=0
count_i=0
easy_going=true #determines how you deal with bad pathes in your copy, true will allow copy to continue provided one of the items being copied exists, false will exit script for one bad path. this setting can also be changed via the custom switches: --strict and --not-strict
verbal="-v"


  help="===============================================================================\
    \n         CREATIVE COPY SCRIPT (CP) -- written by thebunnyrules\
    \n===============================================================================\n
    \n This script (CP, note capital letters) is intended to supplement \
    \n your system's regular cp command (note uncapped letters). \n
    \n Script's function is to check if the destination path exists \
    \n before starting the copy. If it doesn't it will be created.\n    
    \n To make this happen, CP assumes that the item you're copying is \
    \n being dropped in the destination path and is not the destination\
    \n itself (aka, a renamed copy of the source file/folder). Meaning:\n 
    \n * \"CP /a/b /c/d\" will result in /c/d/b \
    \n * even if you write \"CP /a/b /c/b\", CP will create the path /a/b, \
    \n   resulting in /c/b/b. \n
    \n Of course, if /c/b or /c/d are existing files and /a/b is also a\
    \n file, the existing destination file will simply be overwritten. \
    \n This behavior can be changed with the \"--rename\" switch. In this\
    \n case, it's assumed that \"CP --rename /a/b /c/d\" is copying b into /c  \
    \n and renaming the copy to d.\n
    \n===============================================================================\
    \n        CP specific help: Switches and their Usages \
    \n===============================================================================\n
    \
    \n  --rename\tSee above. Ignored if copying more than one item. \n
    \n  --quiet\tCP is verbose by default. This quiets it.\n
    \n  --strict\tIf one+ of your files was not found, CP exits if\
    \n\t\tyou use --rename switch with multiple items, CP \
    \n\t\texits.\n
    \n  --relaxed\tIgnores bad paths unless they're all bad but warns\
    \n\t\tyou about them. Ignores in-appropriate rename switch\
    \n\t\twithout exiting. This is default behavior. You can \
    \n\t\tmake strict the default behavior by editing the \
    \n\t\tCP script and setting: \n
    \n\t\teasy_going=false.\n
    \n  --help-all\tShows help specific to cp (in addition to CP)."

cp_hlp="\n\nRegular cp command's switches will still work when using CP.\
    \nHere is the help out of the original cp command... \
    \n\n===============================================================================\
    \n          cp specific help: \
    \n===============================================================================\n"

outro1="\n******************************************************************************\
    \n******************************************************************************\
    \n******************************************************************************\
    \n        USE THIS SCRIPT WITH CARE, TYPOS WILL GIVE YOU PROBLEMS...\
    \n******************************************************************************\
    \n******************************* HIT q TO EXIT ********************************\
    \n******************************************************************************"


#count and classify arguments that were inputed into script, output help message if needed
while true; do
    eval input="\$$n"
    in_=${input::1}

    if [ -z "$input" -a $n = 1 ]; then input="--help"; fi 

    if [ "$input" = "-h" -o "$input" = "--help" -o "$input" = "-?" -o "$input" = "--help-all" ]; then
        if [ "$input" = "--help-all" ]; then 
            echo -e "$help"$cp_hlp > /tmp/cp.hlp 
            cp --help >> /tmp/cp.hlp
            echo -e "$outro1" >> /tmp/cp.hlp
            cat /tmp/cp.hlp|less
            cat /tmp/cp.hlp
            rm /tmp/cp.hlp
        else
            echo -e "$help" "$outro1"|less
            echo -e "$help" "$outro1"
        fi
        exit
    fi

    if [ -z "$input" ]; then
        count_i=$(expr $count_i - 1 ) # remember, last item is destination and it's not included in cound
        break 
    elif [ "$in_" = "-" ]; then
        count_s=$(expr $count_s + 1 )
    else
        count_i=$(expr $count_i + 1 )
    fi
    n=$(expr $n + 1)
done

#error condition: no items to copy or no destination
    if [ $count_i -lt 0 ]; then 
            echo "Error: You haven't listed any items for copying. Exiting." # you didn't put any items for copying
    elif [ $count_i -lt 1 ]; then
            echo "Error: Copying usually involves a destination. Exiting." # you put one item and no destination
    fi

#reset the counter and grab content of arguments, aka: switches and item paths
n=1
while true; do
        eval input="\$$n" #input=$1,$2,$3,etc...
        in_=${input::1} #first letter of $input

        if [ "$in_" = "-" ]; then
            if [ "$input" = "--rename" ]; then 
                rename=true #my custom switches
            elif [ "$input" = "--strict" ]; then 
                easy_going=false #exit script if even one of the non-destinations item is not found
            elif [ "$input" = "--relaxed" ]; then 
                easy_going=true #continue script if at least one of the non-destination items is found
            elif [ "$input" = "--quiet" ]; then 
                verbal=""
            else
                #m=$(expr $m + 1);eval switch$m="$input" #input is a switch, if it's not one of the above, assume it belongs to cp.
                switch_list="$switch_list \"$input\""
            fi                                  
        elif ! [ -z "$input" ]; then #if it's not a switch and input is not empty, it's a path
                i=$(expr $i + 1)
                if [ ! -f "$input" -a ! -d "$input" -a "$i" -le "$count_i" ]; then 
                    err=$(expr $err + 1 ); error_list="$error_list\npath does not exit: \"b\""
                else
                    if [ "$i" -le "$count_i" ]; then 
                        eval item$i="$input" 
                        item_list="$item_list \"$input\""
                    else
                        destination="$input" #destination is last items entered
                    fi
                fi
        else
            i=0
            m=0
            n=1                     
            break
        fi      
        n=$(expr $n + 1)
done

#error condition: some or all item(s) being copied don't exist. easy_going: continue if at least one item exists, warn about rest, not easy_going: exit.
#echo "err=$err count_i=$count_i"
if [ "$easy_going" != true -a $err -gt 0 -a $err != $count_i ]; then 
    echo "Some of the paths you entered are incorrect. Script is running in strict mode and will therefore exit."
    echo -e "Bad Paths: $err $error_list"
    exit
fi

if [ $err = $count_i ]; then
    echo "ALL THE PATHS you have entered are incorrect! Exiting."
    echo -e "Bad Paths: $err $error_list"
fi

#one item to one destination:
#------------------------------
#assumes that destination is folder, it does't exist, it will create it. (so copying /a/b/c/d/firefox to /e/f/firefox will result in /e/f/firefox/firefox
#if -rename switch is given, will assume that the top element of destination path is the new name for the the item being given.

#multi-item to single destination:
#------------------------------
#assumes destination is a folder, gives error if it exists and it's a file. -rename switch will be ignored.

#ERROR CONDITIONS: 
# - multiple items being sent to a destination and it's a file.
# - if -rename switch was given and multiple items are being copied, rename switch will be ignored (easy_going). if not easy_going, exit.
# - rename option but source is folder, destination is file, exit.
# - rename option but source is file and destination is folder. easy_going: option ignored.

if [ -f "$destination" ]; then
    if [ $count_i -gt 1 ]; then 
        echo "Error: You've selected a single file as a destination and are copying multiple items to it. Exiting."; exit
    elif [ -d "$item1" ]; then
        echo "Error: Your destination is a file but your source is a folder. Exiting."; exit
    fi
fi
if [ "$rename" = true ]; then
    if [ $count_i -gt 1 ]; then
        if [ $easy_going = true ]; then
            echo "Warning: you choose the rename option but are copying multiple items. Ignoring Rename option. Continuing."
        else
            echo "Error: you choose the rename option but are copying multiple items. Script running in strict mode. Exiting."; exit
        fi
    elif [ -d "$destination" -a -f "$item1" ]; then
        echo -n "Warning: you choose the rename option but source is a file and destination is a folder with the same name. "
        if [ $easy_going = true ]; then
            echo "Ignoring Rename option. Continuing."
        else
            echo "Script running in strict mode. Exiting."; exit
        fi
    else
        dest_jr=$(dirname "$destination")
        if [ -d "$destination" ]; then item_list="$item1/*";fi
        mkdir -p "$dest_jr"
    fi
else
    mkdir -p "$destination"
fi

eval cp $switch_list $verbal $item_list "$destination"

cp_err="$?"
if [ "$cp_err" != 0 ]; then 
    echo -e "Something went wrong with the copy operation. \nExit Status: $cp_err"
else 
    echo "Copy operation exited with no errors."
fi

exit
Bozuwa answered 25/2, 2018 at 6:1 Comment(2)
This is perhaps an overkill, but pretty efficient, works as expected, thank you good sir.Viceroy
Overengineering at it's finest.Cowry
M
4

Just had the same issue. My approach was to just tar the files into an archive like so:

tar cf your_archive.tar file1 /path/to/file2 path/to/even/deeper/file3

tar automatically stores the files in the appropriate structure within the archive. If you run

tar xf your_archive.tar

the files are extracted into the desired directory structure.

Meldameldoh answered 6/2, 2019 at 8:2 Comment(0)
W
4

i strongly suggest ditto. just works.

ditto my/location/poop.txt this/doesnt/exist/yet/poop.txt

Wunderlich answered 9/9, 2020 at 15:46 Comment(2)
Should be pointed out that ditto does not ship with most Linux distributions.Beardless
I don't know why this response has only 3 votes atm, rather than 500+ thank you @ulf-gjerdingen for the simple and straight forward answer.Mercator
P
3

Copy from source to an non existing path

mkdir –p /destination && cp –r /source/ $_

NOTE: this command copies all the files

cp –r for copying all folders and its content

$_ work as destination which is created in last command

Peterkin answered 26/9, 2016 at 4:40 Comment(0)
S
1

Oneliner to create a small script that can be used as subcommand, in find for instance:

set +H; echo -e "#!/bin/sh\nmkdir -p \$(dirname \"\$2\"); cp \"\$1\" \"$2\"\;" > ~/local/bin/cpmkdir; chmod +x ~/local/bin/cpmkdir

You can then use it like:

find -name files_you_re_lookin_for.* -exec cpmkdir {} ../extracted_copy/{} \;

Superhighway answered 16/1, 2022 at 20:31 Comment(0)
S
0
rsync file /path/to/copy/file/to/is/very/deep/there

This might work, if you have the right kind of rsync.

Scharf answered 2/11, 2015 at 21:32 Comment(1)
This doesn't work for me -- I got "No such file or directory" on the dest dirFrancenefrances
M
0

You can use find with Perl. Command will be like this:

find file | perl -lne '$t = "/path/to/copy/file/to/is/very/deep/there/"; /^(.+)\/.+$/; `mkdir -p $t$1` unless(-d "$t$1"); `cp $_ $t$_` unless(-f "$t$_");'

This command will create directory $t if it doesn't exist. And than copy file into $t only unless file exists inside $t.

Mesothorax answered 24/1, 2021 at 7:30 Comment(0)
U
0

This works on GNU /bin/bash version 3.2 on MacOS (tested on both Catalina and Big Sur)

cp -Rv <existing-source-folder>/   <non-existing-2becreated-destination-folder>

the "v" option is for verbose.

And I think of the "-R" option as "Recursive".

man's full description of -R is:

If source_file designates a directory, cp copies the directory and the entire subtree connected at that point. If the source_file ends in a /, the contents of the directory are copied rather than the directory itself. This option also causes symbolic links to be copied, rather than indirected through, and for cp to create special files rather than copying them as normal files. Created directories have the same mode as the corresponding source directory, unmodified by the process' umask.

In -R mode, cp will continue copying even if errors are detected.

Note that cp copies hard-linked files as separate files. If you need to preserve hard links, consider using tar(1), cpio(1), or pax(1) instead.

In the example below, I'm using a "/" at the end of existingfolder so that it copies all the contents of existingfolder (and not the folder itself) into newfolder:

cp -Rv existingfolder/  newfolder

Try it.

Utu answered 1/7, 2021 at 2:1 Comment(0)
P
0

Many of the other solutions don't work on files or folders which need escaping. Here is a solution which works for files and folders, and escapes spaces and other special characters. Tested in a busybox ash shell which doesn't have access to some of the fancier options.

export file="annoying folder/bar.txt"
export new_parent="/tmp/"

# Creates /tmp/annoying folder/
mkdir -p "$(dirname "$new_folder/$file")"

# Copies file to /tmp/annoying folder/bar.txt
cp -r "$file" "$new_folder/$file"

This should also work if you omit bar.txt if you need the recursive copy of a whole folder.

Penton answered 8/8, 2022 at 14:51 Comment(0)
F
-1

Let's say you are doing something like

cp file1.txt A/B/C/D/file.txt

where A/B/C/D are directories which do not exist yet

A possible solution is as follows

DIR=$(dirname A/B/C/D/file.txt)
# DIR= "A/B/C/D"
mkdir -p $DIR
cp file1.txt A/B/C/D/file.txt

hope that helps!

Fallacy answered 28/2, 2017 at 22:36 Comment(0)
P
-1

Only for macOS

rsync -R <source file path> destination_folder

For macOS --parents option of cp doesn't work

Presnell answered 11/10, 2021 at 11:22 Comment(0)
C
-1

(If my answer doesn't get blocked here...) couldn't this help in rsync:

rsync --mkpath sourcedir/ destdir

The destdir is created if non-existent.

Cabriolet answered 25/5, 2023 at 10:56 Comment(2)
Why exactly do you say "...doesn't get blocked here"? Anyway, please add supporting information, whit docs and/or useful explanations. Thank you!Hornbeck
There is no such option in rsync from what I can see: linux.die.net/man/1/rsyncMinstrel
A
-2

Copy files from ./assets/ to other dir with current date

rsync -avd ./assets/ ~/backup/project_x/assets/$(date '+%Y-%m-%d')

-a                          Copy files from sub-dir of source dir
-d, --dirs                  Also copy directories
-u, --update                skip files that are newer on the receiver
    --inplace               update destination files in-place
    --append                append data onto shorter files
-l, --links                 copy symlinks as symlinks

More Information:

rsync [Options] [Source] [Destination]

Copy content of x to y directory x/a.txt y/a.txt :

rsync -av x/ y

Copy a.txt and b.txt only from x to y dir :

rsync -av 'x/a.txt x/b.txt' y

Local to remote :

rsync [OPTION]... SRC [SRC]... rsync://[USER@]HOST[:PORT]/DEST

Remote to local :

rsync [OPTION]... rsync://[USER@]HOST[:PORT]/SRC [DEST]

Ignore from a file :

rsync -avd --delete --exclude-from=.resyncignore
  • man rsync - To list all the available options for rsync
  • man date - for more information on other date format
Anastomosis answered 21/6, 2023 at 22:54 Comment(0)
R
-6

Simple

cp -a * /path/to/dst/

should do the trick.

Rocca answered 7/5, 2019 at 12:30 Comment(2)
It doesn't. The '/path/to/dst/' must exist beforehand.Astrogate
This is bad practice and should not be use.Delitescent

© 2022 - 2024 — McMap. All rights reserved.