Unix Tutorial #2: Copying and Removing Files
Topics covered: File manipulation; arguments; options; copying, deleting, and renaming files
Commands used: cp, mv, rm
Now that you have experience getting around your directories with
ls, let’s expand our repertoire with the commands
rm. These are file manipulation commands, analogous to right clicking on a file and copying and pasting it, renaming or moving it to a folder, or moving it to the trash bin. The cp and mv commands are more complex than the commands in the previous tutorial, since they require two inputs, or arguments. We will also use these commands to introduce the concept of options, or flags, which give your commands greater flexibility.
To see the similarities between using the graphical interface and using the command line, create a text file in an editor of your choice, such as TextEdit. Save the file to your Desktop - in this example, I’ll call it myFile.txt - and try the following commands, remembering to press the Enter key after each line:
cp myFile.txt myFile2.txt mv myFile.txt myNewFile.txt rm myFile2.txt
The first line creates a copy of myFile.txt and labels it myFile2.txt; the second line renames myFile.txt to myNewFile.txt; and the last line removes myFile2.txt. As you type these in the command line you should see the files being copied, renamed, and deleted - just as you would if you were using your mouse with the graphical user interface.
There is one important difference between the GUI and the command line when removing files and directories. A file that is removed using the GUI will first be put it in the Trash bin, and will only be permanently deleted if you choose to empty the Trash bin. Removing a file with
rm, on the other hand, permanently deletes the file right away; you will not be able to recover the file after you remove it with
Click here to see a video walkthrough about copying and removing files. This video will also show you how to move, rename, and delete folders. When you’ve finished watching it, try these exercises to retain what you’ve learned.
Make a new directory in your home directory and either create a couple of new files in that directory, or copy files in to that directory from another directory.
Now copy the directory to the Desktop, but append a forward slash to the end of the directory tha tyou are copying. For example, if the directory is called myFileder, type cp -R myFolder/ Desktop. What happens?
man cpand find the section about the -R option. Try using one of the other options with the cp command, and observe what happens.