How To Copy A Directory In CentOS?

Published August 10, 2024

Problem: Copying Directories in CentOS

Copying directories is a common task in CentOS, but it can be tricky for new users. Knowing the right commands and options helps you copy folders and their contents while keeping file permissions and attributes intact.

Using the 'cp' Command with '-r' Option

The 'cp' command in CentOS copies files and directories. To copy directories, use the '-r' option, which means recursive. This option tells 'cp' to copy the directory and all its contents, including subdirectories and files.

Here's how to copy a directory using the 'cp' command with the '-r' option:

  1. Open the terminal on your CentOS system.

  2. Use this command structure:

    cp -r /source/directory /destination/directory
  3. For the specific example:

    cp -r /home/server/folder/test /home/server/
  4. Press Enter to run the command.

  5. Wait for the copying process to finish. The time depends on the directory size.

  6. Check the destination folder to confirm the copy was successful.

If you're copying to a location that needs higher permissions, you may need to use 'sudo' before the command.

Using 'cp -r' copies entire directory structures while keeping the original file permissions and attributes.

Tip: Preserve Timestamps

To preserve the original timestamps of the files and directories during the copy process, use the '-p' option along with '-r'. The command would look like this:

cp -rp /source/directory /destination/directory

This ensures that the copied files and directories maintain their original creation and modification dates.

Alternative Methods for Directory Copying

Using the 'rsync' Command

The 'rsync' command is a tool for copying and synchronizing files and directories in CentOS. It works well for copying large directories or for remote file transfers.

To copy a directory using 'rsync', use this command:

rsync -av /source/directory/ /destination/directory/

The '-a' option keeps attributes, while '-v' shows detailed output.

Benefits of using 'rsync' for directory copying:

  • It's faster than 'cp' for large directories
  • It can resume stopped transfers
  • It only copies changed files, saving time and bandwidth

Tip: Exclude Files with rsync

You can exclude specific files or directories when using rsync. Use the --exclude option:

rsync -av --exclude='*.tmp' /source/directory/ /destination/directory/

This command will copy all files except those with the .tmp extension.

Using the 'tar' Command

The 'tar' command in CentOS is mainly used for archiving files, but it can also copy directories. It keeps file permissions and can handle special files.

To copy a directory using 'tar':

  1. Create an archive of the source directory:

    tar -cvf archive.tar /source/directory
  2. Extract the archive in the destination:

    tar -xvf archive.tar -C /destination/directory
  3. Remove the temporary archive file:

    rm archive.tar

The 'tar' method is useful when you need to keep exact file permissions or when working with special files.