How to Copy Files in Bash

Aashish Sunuwar Feb 02, 2024
  1. Copy a File From Directory Through Bash
  2. Copy an Entire Directory to Another Directory
How to Copy Files in Bash

The article will show how to copy a file from one directory to another through Bash.

Copy a File From Directory Through Bash

You can copy a specific file to a new directory through Bash, followed by the name of the file you want to copy and the directory you want to copy the file into.

Command:

cp source_file destination_dirname

We have to copy hello.txt in the folder directory, and then we can execute the following command.

Example:

cp hello.txt folder

The original copy of the file will remain in the original directory, so you’ll have two versions of the hello.txt file, one in the home directory and the other in another directory folder.

Copy an Entire Directory to Another Directory

Similarly, you can copy an entire directory to another directory by typing cp -r, followed by the name of the directory you want to copy and the name of the directory you want to transfer it to.

Command:

cp -r directory_name_1 directory_name_2

The -r in cp -r tells Bash that it has to recourse (or repeat) the command over a list of all files and subdirectories within the parent directory, similar to rm -r.

Example:

cp -r /c/Temp /c/folder

The original copy of the directory is still present in the original directory.

Related Article - Bash File