Are you new to Linux? If so, you’ve probably found the command line can be a bit intimidating. Don’t worry–it is for everyone at the beginning. That’s why I’m here to guide 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 GUI-less Linux server and need to make a backup of a configuration file or copy a data directory.
Trust me, at some point you’re going to need to be able to do this. Let’s find out how.
SEE: Linux: The 7 best distributions for new users (free PDF) (TechRepublic)
First we’ll copy a file. Let’s say you’re about to make changes to the Samba configuration file, smb.conf and you want a backup copy just in case something goes wrong. To copy that file, use the cp command to copy the source to the destination like so:
cp /etc/samba/smb.conf /etc/samba/smb.conf.bak
You’ve probably already encountered your first problem. Because the smb.conf file is in /etc/, you’ll need to use sudo privileges to make the copy. So the correct command is:
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 might want to preserve the file attributes (such as directory and file mode, ownership, and timestamps) during the copy. For that we use the -a option as in:
sudo cp -a /etc/samba/smb.conf /etc/samba/smb.conf.bak
Copying a directory is done in the same way, only you use the -R option, for recursive. Let’s say you want to make a backup of the entire /etc/samba directory and you want to copy it to your home directory. That command would be:
sudo cp -R /etc/samba ~/samba.bak
To preserve the attributes, while copying the directory, the command would be:
sudo cp -aR /etc/samba ~/samba.bak
And that’s all there is to it. You’ve just copied your first files and directories from the Linux command line. Now, go out and celebrate this victory, you’ve earned it.
Subscribe to TechRepublic’s How To Make Tech Work on YouTube for all the latest tech advice for business pros from Jack Wallen.