Copy entire directory using Terminal Command [Linux, Mac, Bash]


In order to copy a directory entirely from one location to another in Linux/Unix/Mac/Bash you can make use of the below command,

Syntax:
cp -r source-dir destination-directory-location

cp : This is the copy utility used to copy file or files from source to a target file/files.

-r : This option is used if the source is a directory, when used with cp - it copies the directory and the entire subtree of the directory to the destination, note this will also copy the symbolic links.

source-dir: location of the source directory. Note that it should be a directory.

destination-directory-location: the target location where the directory (folder) needs to be copied.

Examples:

Let's say I have a directory myApp on my Desktop location and I want to copy it to /Documents

cp -r myApp /Users/code2care/Documents

Note that if you do not provide -r parameter you will get an error saying cannot copy as the source is a directory.

Copy directory command example
Copy directory command example
code2care@mac Desktop % cp myApp /Users/code2care/Documents   
cp: myApp is a directory (not copied).
srouce directory
srouce directory
target location
target location

All the files and subfolders will get copied!

Advertisements
Try Out Code2care Dev Tools:

Advertisements



Android Java Linux Microsoft Google Python macOS Notepad++ Microsoft Teams CSS PHP SharePoint Html Linux C Programs Bootstrap jQuery Sublime Android Studio Facebook Eclipse WhatsApp News MySQL Json HowTos JavaScript FTP S3


Advertisements


Code2care is an initiative to publish and share varied knowledge in programming and technical areas gathered during day-to-day learnings and development activities.

Students and Software Developers can leverage this portal to find solutions to their various queries without re-inventing the wheel by referring to our easy to understand posts. Technical posts might include Learnings, Tutorials, Trouble-Shooting steps, Video Tutorials, Code Snippets, How Tos, Blogs, Articles, etc.
🎉 We are celebrating the 10th years of Code2care! Thank you for all your support! We hope we made a difference.
We strong support Gender Equality & Diversity.

We stand in solidarity with Ukraine - Make a donation to UNHCR - https://donate.unhcr.org/in/en-in/ukraine-emergency