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.
cp source_file destination_dirname
We have to copy
hello.txt in the
folder directory, and then we can execute the following command.
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
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.
cp -r directory_name_1 directory_name_2
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
cp -r /c/Temp /c/folder
The original copy of the directory is still present in the original directory.
- Write to a File in Bash
- Bash Overwrite File
- Open HTML File Using Bash
- Search for Files With a Filename Beginning With a Specified String in Bash
- Check if a File Is Empty in Bash
- Read File Into Variable in Bash