Copy a directory in Linux, from the command line, with one easy command

learlinuxAs an operating system, Linux is extremely popular with hard core technical enthusiasts. Those who work on it, swear by it. It is robust, safe, and scalable. However, there are folks who’ve gotten used to a Windows UI who tend to shy away from it. In this tutorial, you’ll get to see how simple and powerful Linux can be – if you just learn a few basic commands and practice them. We will walk you through one such command today – copying directories

Linux – an introduction

Linux is free operating system. By free, what we mean is that the source code of Linux comes free. This means that the code of the operating system is available to all and you can modify and add to it according to your need. Thus, since its inception Linux has gone through many changes and there is a strong and growing community of Linux programmers who keep on adding to its functionalities. If you do not have prior experience with Linux, we highly recommend you first try out this Linux crash course to get familiar with it. This will help set the framework for the terms we use in the rest of the article.

The link system in Linux

To understand how files and directories are created in Linux and how to work with them, you need to understand links first. The concepts of files and folders are similar to other operating systems.

For example, you may have worked with folders in Windows. Yes, folders are the places where we store our files. What folders are to Windows, directories are to Linux. Let us discuss how files and directories are created in Linux and then we will see how we can copy directories.

How directories are created in Linux

When you create a file in Linux, several types of information gets linked to it internally – the user who created it, the permissions, time of creation, linkages. So, when you create a file or directory in Linux, an inode number is assigned to the directory data as stored in the disk and the directory name points to the inode number.

The copy command in Linux

The command cp, an abbreviation of ‘copy’, is used to copy files and directories in Linux.

The default syntax for cp is

>cp source  destination

where

source is the source file or directory you want to copy

destination is the new place – file or directory – where you want to copy it to

Here is a simple example copying a file from one location to another

>cp  myfile.txt newdir/.

This copies the file myfie.txt from the current directory to the newdir/ directory

These were some simple copy examples. Let’s now look at how to copy an entire directory.

How to copy directories with the cp command

It’s actually pretty simple. Just specify your source and destination directories as arguments to the copy command!

> cp dir1/ dir2/

This copies everything from dir1/ into dir2/. Well, not quite everything. If dir1/ has any sub directories, or soft links, they are not copied over by default. Linux can’t quite guess your intentions. You have to specifically tell it if you want to copy over all subdirectories, hidden files, or links. The copy command has specific options to cover all of those, and much more. Let’s go through some of the important options.

Copying recursively

In order to copy all the subdirectories of a parent directory, you need to give the ‘-r’ option. Note that options are not case sensitive. You can use ‘-r’ or ‘-R’ interchangeably. Here’s what it looks like:

> cp –r dir1/ dir2/

Now you need to be careful with this option. If your directory structure is quite nested, this may take a long time, or may get stuck in between – if there are broken files or subdirectories hidden a few layers below.

Note that if the destination directory, dir2/ already has files or subdirectories of the same name, your files or subdirectories will not be copied over.

Force a copy

You may run into situations where you want to force a copy, for example in the situation mentioned above, where the destination already has files with the same name. The Linux copy command gives you an easy way out. Here’s how:

> cp –rf dir1/ dir2/

Note the additional ‘f’ along with our ‘-r’ option. This tells Linux, to forcefully copy over files and subdirectories from the source directory.

Creating an archive

If you want to save your directory as an archive, you can use

> cp –a dir1/ dir2/

The – a option helps to preserve the structure and attributes of the original directory into the new directory.

                    The cp command when not coping recursively follows the symbolic links. This is a default option and it can be overridden by specifying options like ‘-d’ which means “–no-dereference”.

Copying links

To copy over symbolic links, you can use ‘-d’ or ‘- – no- dereference ‘ option. The cp command does not copy the files that the links point to but copies the symbolic links only. It also preserves the hard links between the source files in the copies also.

> cp –d dir1/ dir2/

Note that by default, copy does not let you copy a file over to itself ie the source and the destination have to be different.

Of course, this is just the tip of the iceberg. The copy command has many more options and we cannot cover them all here. If you need more details, just use the ‘man’ command and Linux will list out all the options, what they mean, and how to use them.

> man cp

We hope this tutorial gave you a fair idea of how to copy directories in Linux. As you can see, Linux copy gives you a much finer level of control than Windows drag and drop. With a very wide range of options to specify exactly what and how you want to do things, it’s no wonder Linux is widely liked in the technical community.