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
Copying a Directory: Suppose you have a directory named
my_folderthat you want to copy to a new location calledbackup_folder.cp -r my_folder backup_folderThis command will create a copy of
my_folderand all its contents insidebackup_folder.Copying to a Different Path: You can also specify an absolute path for the destination. For example, to copy
my_folderto/home/user/backup/:cp -r my_folder /home/user/backup/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!
