From the course: Red Hat Certified System Administrator (EX200) Cert Prep: 1 Deploy, Configure, and Manage (2021)

Copy files and directories

- [Instructor] Everyday file management requires us to copy files and directories. We can do these tasks from the GUI but they're usually faster on the command line. The command we use to copy files on Linux is cp. The syntax for cp is cp space options space source space destination. The options can take the form of single letters preceded by a hyphen or complete words preceded by two hyphens. If they are single letters they can be combined into one sequence. For instance dash pf. The source file is the path to the file you want to make a copy of. If you want to copy multiple files just separate them with spaces. You can also use file globbing covered in this course to match multiple files. The destination is where you want the file copied to. Both source and destination paths can either be relative paths or absolute paths. Relative paths are relative to where you are. Absolute paths start with a forward slash and are from the root or top level directory. You can combine relative and absolute paths for source and destination files. However, there can only be one destination path. In my example, I've used the tilde shortcut to indicate that I want to copy the file to my home directory. In a terminal let's create a directory to create files in using mkdir and change into it. Type in mkdir copyexercise and hit enter. Now cd into copyexercise and hit enter again. You can verify your path by using the print working directory command. Type in pwd to make sure that you're in the copy exercise directory. Now let's create some files to copy using touch. Touch`s job is to change timestamps, but if the file we want to change doesn't exist, It creates an empty file, and that's all we need for this exercise. Type in touch space file one dot txt and hit enter. Now let's make a copy of this file. Type in cp space file one dot txt space file one dash copy dot txt and hit enter. You won't see output on the screen, if it was successful. Verify this by typing it ls space dash l and hit enter. We can see that we have both file one dot txt and file one dash copy dot txt. We can specify a directory as a destination as well. Let's make a director using mkdir. Type in mkdir space dir one and hit enter. Now let's copy file one dot txt two dir one. Type in cp space file one dot txt space dir one and hit enter. Verify with the tree command. Tree may not be installed by default so we may have to install it using the yum command and elevating our privileges with sudo. Type in sudo space yum space install space dash y space tree and hit enter. Type in your password and hit enter again. Tree is now installed and I'm going to type in clear to clear my screen. Now type in tree and hit enter. Tree gives us a somewhat graphical visualization of the file structure. We can see that a copy of file one dot txt is in dir one because we didn't specify the name the copy kept the original name of file one dot txt. To change the name while copying let's specify it. Type in cp space file one dot txt space dir one forward slash file one dash copy dot txt and hit enter. And then we'll verify again with tree and we can see that inside dir one we have a file one dash copied at txt as well. If you run the exact same line again it will overwrite the destination file without a warning. Use your up arrow key to get back to your copy line and hit enter. If we want Linux to warn us we can add the dash I option for interactivity. Use your up arrow key again move over to cp and insert a dash I. So your line should say cp space dash I space file one dot txt space dir one slash file one dash copy dot txt. Now hit enter. When it comes to copying a directory, things are different. Let's make a copy of our dir one directory and call it dir two. Type in cp space dir one space dir two and hit enter. We get an error message saying cp is omitting dir one. This is not a very helpful message. What it's saying is that we didn't tell cp to enter the directory and copy the contents. To do so we need to add the recursive option. Type in cp space dash capital r space dir one space dir two and hit enter. And verify with the tree command. Type in tree and hit enter again. You'll see that we now have a copy of dir one named dir two. I'm not going to cover every option of the cp command but a couple of options that I find useful are dash a for archive which preserves all metadata. This includes timestamps ownership and permissions. Dash b to backup destination files, dash n to not overwrite destination files if they exist, and dash u to only copy files if they're newer than the destination. For more options check the man page to cp by typing in man space cp.

Contents