Linux 101: How to copy files and directories from the command line



Jack Wallen continues his Linux 101 series, with an introduction on how to copy files and directories from the command line.

Are you new to Linux? If so, you’ve probably found the command line to be a little intimidating. Don’t worry, it’s for everyone at the start. That’s why I’m here to walk you through the process, and today I’m going to show you how to copy files and folders from the command line.

Why would you need to copy files and folders this way? You might find yourself on a Linux server without a GUI and need to back up a configuration file or copy a data directory.

Believe me, at some point you will have to be able to do this. Let’s see how.

SEE: Linux: The 7 Best Distributions for New Users (Free PDF) (TechRepublic)

We will first copy a file. Let’s say you’re about to make changes to Samba’s configuration file, smb.conf, and you want a backup copy in case something goes wrong. To copy this file, use the cp command to copy source to destination like this:

 cp /etc/samba/smb.conf /etc/samba/smb.conf.bak

You’ve probably already encountered your first problem. Since the smb.conf file is located in / etc /, you will need to use sudo privileges to perform the copy. The correct order is therefore:

sudo cp /etc/samba/smb.conf /etc/samba/smb.conf.bak 

In this example, smb.conf is our source and smb.conf.bak is our destination. You may want to retain file attributes (such as directory and file mode, ownership, and timestamps) while copying. For this we use the -a option as in:

sudo cp -a /etc/samba/smb.conf /etc/samba/smb.conf.bak

Copying a directory is done the same way, only you use the -R option, for recursive. Suppose you want to make a backup of the entire / etc / samba directory and want to copy it to your home directory. This command would be:

sudo cp -R /etc/samba ~/samba.bak

To keep the attributes, when copying the directory, the command would be:

sudo cp -aR /etc/samba ~/samba.bak

And that’s all. You have just copied your first files and directories from the Linux command line. Now go out there and celebrate this victory, you have earned it.

Subscribe to TechRepublic How to Make Technology Work on YouTube for all the latest technical advice for business professionals from Jack Wallen.

Also look


Image: iStockPhoto / TimArbaev


Leave A Reply

Your email address will not be published.