How to copy directories recursively?

QuestionsQuestions8 SkillsProBasic Files OperationsNov, 24 2025
0263

To copy directories recursively in Linux, you can use the cp command with the -r (or --recursive) option. This allows you to copy an entire directory along with all its contents, including subdirectories and files.

Syntax

cp -r source_directory destination_directory

Example

  1. Copying a Directory: Suppose you have a directory named my_folder that you want to copy to a new location called backup_folder.

    cp -r my_folder backup_folder
    

    This command will create a copy of my_folder and all its contents inside backup_folder.

  2. Copying to a Different Path: You can also specify an absolute path for the destination. For example, to copy my_folder to /home/user/backup/:

    cp -r my_folder /home/user/backup/
    
  3. Using Wildcards: If you want to copy multiple directories that match a pattern, you can use wildcards. For example, to copy all directories that start with project_:

    cp -r project_* /home/user/backup/
    

Important Options

  • -v (verbose): Use this option to see the files being copied.

    cp -rv my_folder backup_folder
    
  • -i (interactive): This option prompts you before overwriting files in the destination.

    cp -ri my_folder backup_folder
    

Summary

Using the cp -r command is a straightforward way to copy directories and their contents recursively in Linux. This command is essential for tasks like backing up data or organizing files into different directories.

If you have any further questions or need clarification, feel free to ask!

0 Comments

no data
Be the first to share your comment!