We usually copy files from one location to another existing location using commands such
scp etc. If the target location doesn't exist, we first create it and then the copy file to that newly created location. Up until now, this is how I usually copy files from one location to another from commandline. Did you know that we can copy a file and create the destination directory automatically if it not exists? No? No problem! In this guide, we will see how to copy files and create target directories at the same time, with a single command, in Linux.
Please note that I am not talking about combining two commands. For example, anyone could do this by combining two commands like below:
$ mkdir -p destination && cp source_file destination
But, this is not a single command. Here we've used two commands, i.e.
cp. It is actually a one-liner command. Now let me show how to do the same task using only one command.
Copy files and create target directories at the same time in Linux
We can do this using in three different methods. First we will see how to do it using
Method 1 - using
install command copies the files to any destination of your choice in Linux. It is specially used for this very purpose by build systems like
automake. It is part of GNU coreutils, so you don't need to install it.
To copy a file and create the destination directory at the same using
install command, simply run:
$ install -D source-file destination-directory
The following command copies a file named
sk.txt to a target Directory named
ostechnix. Please note that the target directory doesn't exist. We are going to copy the file and create destination directory at the same time with
install command like below:
$ install -D sk.txt ostechnix/sk.txt
Verify if the file has been copied to the
ostechnix directory by listing its contents with
$ ls ostechnix/ sk.txt
See? I didn't create the target directory before. The install command created the directory automatically and saved the file in it.
Handy, right? Indeed! This is one of the useful feature of the
install command. One can use this in shell scripts.
There are a few limitations in this method. You must mention the source file name at the end of the destination location. If you don't specify the filename,
install command will not copy it and display the following error:
$ install -D sk.txt ostechnix/ install: target 'ostechnix/' is not a directory: No such file or directory
You can copy one file at a time. There is no way to copy multiple files to multiple destinations using
We can also copy files and change the ownership, permissions at the same time using
install command as described in the following guide.
For more details about install command, refer its maual page.
$ man install
Method 2 - using
Generally, we use
cp command to copy files from one place to another. The
cp command has an option named
--parents which enables us to copy entire directory structure of the source file and save it in the destination.
For instance, let us say, the
sk.txt file is saved in
~/Downloads directory and you wanted to copy it to
~/Documents directory. If you copy the file using
cp command from
~/Documents, the file will be saved in directory structure like this ->
Let us do it in real time and see what happens.
$ cp --parents Downloads/sk.txt ~/Documents/
Now run the
tree command to check
~/Documents directory's contents:
$ tree Documents/ Documents/ └── Downloads └── sk.txt 1 directory, 1 file
Remember we didn't create any directories inside the
~/Documents location. The
--parents option of the
cp command preserved the source file's directory structure.
install command, you don't need to specify the file name at the end of the destination directory.
This is not directly related to our topic. However, the
cp command created the target directories automatically i.e. preserved the source file's directory structure.
Method 3 - using
Rsync is an ideal solution to copy and backup data between local and remote directories. To copy files and create target directories at the time using rsync command, run:
$ rsync sk.txt ostechnix/
Where the directory called
ostechnix doesn't have to exist. Rsync will create it automatically and save the source file inside it.
Among these three commands, I prefer the
install command. Because, I can create nested directories (directory inside another directory) using
install command. The
rsync commands can only create one directory, as far as I know. Please correct me if I am wrong.