How do I create a copy of a directory in Unix/Linux?

 

To create a copy of a directory in Unix/Linux, you can use the cp command with the -r or -R option (both are equivalent) to recursively copy the directory and its contents. Here's how to do it with an example:

Syntax:

bash
cp -r source_directory destination_directory
  • source_directory: Replace this with the path to the directory you want to copy.
  • destination_directory: Replace this with the path where you want to create the copy.

Example: Let's say you have a directory named myfolder located in your home directory (/home/user) and you want to create a copy of it named myfolder_copy in the same directory.

bash
cp -r /home/user/myfolder /home/user/myfolder_copy

In this example:

  • cp -r is used to recursively copy the contents of myfolder to myfolder_copy.
  • /home/user/myfolder is the source directory.
  • /home/user/myfolder_copy is the destination directory where the copy will be created.

After running this command, a copy of the myfolder directory, including all its files and subdirectories, will be created as myfolder_copy in the same location.

You can also specify different source and destination directories if you want to copy the directory to a different location:

bash
cp -r /path/to/source_directory /path/to/destination_directory

Just replace /path/to/source_directory and /path/to/destination_directory with the actual paths you want to use for the source and destination directories.

Comments