how to copy a folder in linux using the command line

Often times you need to copy a folder or file to another location or into another folder. A copy is a process where a duplicate of the original file or folder is created at another location. After the end of the copy process you will have two identical copies of the same file at two different locations.

This process differs from a move process where the original file is deleted after the file is recreated at the new location, leaving just one copy of the file. All file managers in the desktop environments makes it as easy as a drag and drop, so we are going to just look at the command line option.

In Linux or Unix operating systems, the command to copy a folder or file is cp, commonly known as the cp command. This command is used to copy either a file or a folder or both. cp is a simple command that takes two arguments: the source and the destination.

The most simplistic and generic format of the command to copy a directory is

cp -r <source dir> <destination dir>

where source is the location of the original copy of the file or folder and destination is the final location of the file or folder. The option -r (-R, -r or –recursive) specifies that it is a recursive copy.

When copying directories, it does not make sense to copy them with out copying its contents as well, such as files and sub-folders. So, in order to copy directories, the –recursive (or -r) is a mandatory option. Also, you can copy a directory only into another existing directory.

There are several options available while copying a folder. We will go through some of the popular and most commonly used options.

  • –interactive (-i): This option will prompt you before it overwrites any files or folders, so that you can choose to overwrite or not on a file to file basis.
  • –verbose (-v): This will provide some extra information in the console during the copy, like the files that are being copied or overwritten etc.
  • –update (-u): Specifying this option will ensure that only updated files are copied. Files that already exists at the target will not needlessly be rewritten unless it has been changed at the source.
  • –preserve=mode,ownership,timestamps (-p): The -p option will preserve the default metadata of the files such as the mode, ownership and timestamps. You can choose which metadata field you like to preserve by specifying it with the –preserve option.
linux cp command to copy folders

As with almost every Linux command argument, you can use wildcards when specifying the folder locations. This allows you to select files and folder in groups or batches rather than having to specify each and every folder name separately. In order to copy all the log files, that has a file extension of .log, from one folder to another, you can use something like

cp /var/log/application/*.log /var/backup/logs/

This will copy all the files with a .log extension in the folder /var/log/application/ into another folder /var/backups/logs/. Note that the -r option is not required here as you are copying only the files. To copy all the folders and files in the application/ folder into the logs/ folder, you would use

cp -v /var/log/application/* /var/backup/logs/

If you like to copy the application/ folder itself, along with all its files and sub folders into the backup folder, then use

cp -rv /var/log/application /var/backup

In order for the copy to happen the destination folder should exist. The cp command will not create non-existent parent directories or folders at the target location. For example, in the above command the backup folder should exist prior to the execution of the cp command.