In the last Terminal Tutorial, I went over the two commands cd and ls, which just allow you to view your files. In this segment of Terminal Tutorials, I will show you how to do some operations with your files. This article will cover 4 commands, allowing you to copy, move and delete files and directories.
Creating directories is very easy with the command mkdir. The syntax for this command is
mkdir <path of new directory>. For example, if I wanted to create a directory called new inside of the beta directory, I would enter
ibook:~/beta Alex$ mkdir new
And if I wanted to make a new directory somewhere else,
ibook:~/beta Alex$ mkdir /path/to/new/directory
Copying and Moving Files
The commands cp and mv, for copy and move, respectively, have very similar syntax, so we will cover them together. Copying is like copy and paste while moving is like cut and paste. For both cp and mv, the syntax is
cp <file or directory to be copied/moved> <destination of file or directory>. If I wanted to copy the file test.txt to my desktop as test2.txt, I would use
ibook:~/beta Alex$ cp test.txt /Users/Alex/Desktop/test2.txt
If I wanted to move the file test.txt to the directory above it, I would use
ibook:~/beta Alex$ mv test.txt ..
Removing, or deleting files can be done with the rm command. The syntax for rm is just
rm <file to be deleted>. You have to be very careful with rm, because this command doesn’t send files to the Trash, where files can be easily recovered, it removes them completely. You’ve been warned!
The asterisk (*) can be used with many unix commands to signify a wildcard. For example, to delete all the files in the beta directory, I would enter
ibook:~/beta/beta Alex$ rm *
The asterisk can be used in any command in the context of files and directories. I defined the double period (..) in the last tutorial, which signifies the directory above. Another similar shortcut is the single period (.). The single period denotes the current directory, so if I wanted to copy the file test.txt to my current directory, I would enter
ibook:/ Alex$ cp /Users/Alex/Desktop/test.txt .
Sometimes when you are trying to delete, copy, or move a file or directory, you will get a permission denied error. This means that you were trying to do an operation on a file or directory that did not belong to your specific user. If this happens, and you are sure you still want to complete the command, just preface your command with sudo. Sudo will complete the command with the root user. Root is the administrator account that has full control of your system. When you use sudo to run a command, it will ask you for a password, which will be your admin password on your Mac.
Watch for the next Terminal Tutorial in about a week.