Copying Files & Folders via the Linux Terminal

I recently started  a series of short posts explaining some Linux command line basics for people who new to the Linux terminal, and just want to get in, do a job, and get out, without having to spend hours reading up on every nuance and arcane switch.

In today’s installment we look at the basics you need to know to copy files. 

 

To copy a directory with files, including subfolders (i.e., recursively), preserving all ownership & permissions, do something like this:

cp -pdR /source/path /destination/path

(If you’d rather not copy recursively, then leave off the “R” switch.)

 

To copy all files and folders recursively with ownership/permissions, and including hidden files, you must enter both of these commands:

cp -pdR /source/path/.* /destination/path 
cp -pdR /source/path/* /destination/path

The reason you must do both is because Linux considers files as “hidden” when there’s nothing to the left of the period. You may have seen file names like “.htaccess” on a web server – that would be considered hidden, because it begins with a period. So, if you don’t explicitly tell it “.*” then it will skip those files.

For info about what the above switches mean, along with a list of a lot more you can use, here’s one of many resources you might check.

Steve