Copy Directory in linux computer system can be performed via terminal using a simple command line. While directory also can be created, renamed, modified, grown, and shrunk (truncated). In most cases, software programs that are executed on the computer handle these operations, but the user of a computer can also modify directory if necessary.

For instance, directories are normally copied by program in response to user commands, but the user can also copy these directories directly by using a file manager program such as Windows Explorer (on Windows computers) or via linux terminal with command line (CLI).

How To Copy Directory in Linux?

You can easily copy directory in linux via terminal on your system using the command line with the following methods Command, and Information.

Linux Command To Copy Directory

“cp” command is used to copy files or group of files or directory (folder) from the current directory to another different directory. This is one of the most common administrative tasks you can perform when working on terminal with the linux command line.

To copy directory in linux:

  1. Open “Terminal” on your linux system.
  2. Keep on the current working directory or go to the specific directory (use “cd” to change directory) that you want to copy the directory inside it.
  3. Use “cp” command with option and specify the “directory-name” or path.
  4. The syntax for using the “cp” command as shown below.
    cp [option] [source] [destination]
    • Example: Copy a directory and all its contains with option (-r) in the current directory to another directory.
      cp -r Pictures /home/username/Documents

      This will copy “Pictures” folder with all files and sub-directories within to directory called “Documents”. “-r” option means copy directories recursively.

    • Another Example: Copy a directory to another directory.
      cp -r /home/username/Musics /home/username/MyDocuments

      This will copy “Musics” folder with all files and sub-directories to directory called “MyDocuments”.

    • Example: Copy multiple directories in the current working directory to another directory.
      cp -r Pictures Musics Docs /home/username/Documents

      This will copy “Pictures”, “Musics”, “Docs” directories and all its contains to directory called “Documents”.

    • Use the “cp” with the “-r” and “-v” option to display verbose output before copying it.
      cp -rv Pictures /home/username/Documents
  5. Done, now you’ve already copied the directories.

More Information

“cp” command related information can be obtained with “–help”. Mandatory arguments to long options are mandatory for short options too.

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.

By default, sparse SOURCE files are detected by a crude heuristic and the corresponding DEST file is made sparse as well. That is the behavior selected by –sparse=auto. Specify –sparse=always to create a sparse DEST file whenever the SOURCE file contains a long enough sequence of zero bytes. Use –sparse=never to inhibit creation of sparse files.

When –reflink[=always] is specified, perform a lightweight copy, where the data blocks are copied only when modified. If this is not possible the copy fails, or if –reflink=auto is specified, fall back to a standard copy. Use –reflink=never to ensure a standard copy is performed.

The backup suffix is ‘~’, unless set with –suffix or SIMPLE_BACKUP_SUFFIX. The version control method may be selected via the –backup option or through the VERSION_CONTROL environment variable. Here are the values:

  • none, off never make backups (even if –backup is given)
  • numbered, t make numbered backups
  • existing, nil numbered if numbered backups exist, simple otherwise
  • simple, never always make simple backups

As a special case, cp makes a backup of SOURCE when the force and backup options are given and SOURCE and DEST are the same name for an existing, regular file.

In general, a directory can contain either a list of files or a list of links to files. Within this definition, it is of paramount importance that the term “file” includes “directories”. This permits the existence of directory hierarchies, i.e., directories containing sub-directories.

License: CC-BY-SA Creative Commons License
☝️