Back to course

22. Copying Directories: The `cp -r` Command (Part 2)

Linux Basics: From Zero to CLI Hero

Copying Folders

Linux views directories and files differently. If you try to copy a directory without the appropriate option, the command will fail.

The Recursive Option (-r or -R)

To copy a directory and all its contents (subdirectories and files), you must use the recursive option.

Example: Copying the entire project_A directory and its contents to project_backup.

bash $ mkdir project_backup $ cp -r project_A project_backup/

$ ls -R project_backup project_backup/project_A: file1.txt subfolder/

Common Pitfall

If you forget the -r option when trying to copy a directory:

bash $ cp project_A project_backup cp: omitting directory 'project_A'

Remember: -r is mandatory for directories.