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

In Linux, the cp command is used for copying the file to another directory. For copying any file or folder to another directory, ensure that the destination exists in the operating system otherwise, it will display an error “no such file or directory”. Is there any method that will create a destination directory automatically? Yes, there are 3 ways to do this job. This post will demonstrate the methods to automatically create the destination directory while copying any file if it does not exist. The content for the post is as follows:

Let’s start with the first method.

Method 1: Copy and Create a Destination Directory Using mkdir Command

The first method to create the destination directory automatically is using your cp command with the “mkdir” command and the AND (&&) operator between these two commands. Let’s implement it in the example.

Example: Combining cp with mkdir Command

First, write the “mkdir” command for creating the directory with the “p” option to preserve the privileges. The type AND operator and type your cp command for copying the file:

$ mkdir -p Henry/backup && cp itslinuxfoss.txt Henry/backup

The execution of the command creates the directory “Henry/backup,” and the cp command will copy the file into that directory. Here && operator is used so that if the first command executes successfully, then execute the second command.

The destination directory will be created, which can be seen in the below image:

Note: you can also use the $HOME Variable, which contains the home Directory Address:

$ mkdir -p $HOME/Henry/backupfile && cp $HOME/itslinuxfoss.txt $HOME/Henry/backupfile

Let’s move towards the second method.

Method 2: Copy and Create a Destination Directory Using Bash Script

The second method to create the destination directory is to use the bash script. The bash script is the most useful trick for this; you can define the function and structure with a combination of “mkdir” and the cp command. The main benefit of using the function is that you can just type the function name (object-oriented approach) to invoke that function and execute it automatically. Let’s check how this method works.

Create the bash script file and paste the following script into that file:

CP() {
    mkdir -p $(dirname "$2") && cp "$1" "$2"

Save the file by pressing “Ctrl+O” and exit from the file by pressing “Ctrl+X”.

By running the “chmod” command give the execute permission to the file:

$ chmod +x

The file is executable now.

Now, run this file using the source command for the script we have defined:

$ source

Let’s understand the working of this script. Users will use the “CP” with the source file and destination file path (along with the destination file name) as per the syntax of the cp command. As “CP” is the function name that will be invoked, the source file path will be stored in the “$1” variable, and the destination file path will be stored in the “$2” variable. These variables will be put in the script of the “CP” function, and it will work like method 1. Check the below execution of command

$ CP index.html NewFile/backup.html

The “index.html” file is copied in the “NewFile” Directory with the name of “backup.html”.

To check it, jump into the NewFile Directory and hit the “ls” command:

$ cd NewFile
$ ls

Note: For a permanent solution, you can copy this function into the .bashrc file which will load automatically when you log in.

Let’s move toward method 3.

Method 3: Copy and Create a Destination Directory Using install Command

Another method to create the automatically created destination directory, use the install utility in the terminal. The install command is also used for copying the files; it is the alternate command of cp. The install utility will check the existence of the destination file; if it does not, it will create it.

Let’s understand this by running the command in the terminal.

Use the “D” option with the install command to create the target directory:

$ install -D c-program.c Programming/backup/c-program.c

The install command will copy the file into the newly created directory.

To verify this, let’s run the “cd” and “ls” commands to check it:

$ cd Programming/backup
$ ls

But here, the point is executable permission will be given to the directory. But why? Because the default value of umask is 002, and the directory permission value is 777. So permission will be (777-002 = 775), which is “-rwxr-xr-x”:

To avoid this, you can use the additional “m” option in the install command to specify the permission for the file:

$ install -Dm 644 c-program.c Programming/backup/c-program.c

Let’s check the permission of the “Programming/backup/c-program.c” file:

$ cd Programming/backup
$ ls

Only read and write permission is given to the file.

These are the methods for copying and creating the destination directory.


In Linux, to automatically create the destination directory, use the “mkdir” command with cp, use a bash script function or copy the file with the install utility. This post has briefly demonstrated the three methods to copy and create the target directory if it does not exist.