Useful UNIX Commands


Are we expected to learn all of these UNIX commands and all of their flags and all of their intricacies? Yes, you have until Monday, good luck.

Nah, of course not. It's in your best interest to learn about a lot of commands and memorize how to use the most common ones, and some of the most common flags, but no one remembers all the details. We refer to the documentation when we need to. The documentation is accessible through a command called man.

Is man sexist? No, it stands for "manual".

To open the manpage for a command like ls, enter man ls. It will open the documentation for ls, including what it does and a listing of the flags. You can scroll to move around, and you quit by typing q.

man is a command too, so it's valid to enter man man if you want to learn how to use man. But it'll open man so...🤷‍♂️.

man tells you about how to use a command you already know about. If you don't know what command might be useful in a situation, take a look at the CS 107 Unix Guide.


mkdir is how you "MaKe DIRectories". To create a directory in the current directory, enter mkdir name_of_new_directory.


rmdir is how you "ReMove DIRectories". Turns out it only works for empty directories. To delete a directory, enter rmdir path_to_directory_to_delete.


rm is how you "ReMove" files (not a BTS member). You can delete a file with rm path_to_file_to_delete. You can also use this to delete non-empty directories using the -r flag: rm -r path_to_directory_to_delete.

Be very careful with using rm and rmdir. There's no Recycle Bin here. Once you delete something, it's gone forever. Whenever I type rm, I stop myself and examine my command very carefully to ensure I'm absolutely sure I'm deleting what I'm intending to.


mv is how you "MoVe" files and directories. It's tricky because it can also be used to rename files/directories. The syntax is mv path_to_current_file_or_directory new_location. If new_location is the path to an existing directory, then it'll be moved there, but if new_location is a path to a file or a nonexistent directory, it'll be moved there and renamed to match that. If you need to move a non-empty directory, pass the -r flag.

Be very careful with using mv. When used to rename, and the new name matches something that already exists, you will replace the old file which effectively deletes it.


cp is how you "CoPy" files and directories. It's identical to mv except that it copies instead of moves. Think of the relationship between "cut" and "copy" in a word processor. The same stipulations about deletion and -r apply here too.


cat is how you "conCATenate" files. Ok, so that doesn't really explain anything. cat outputs a file's contents to the display so you can view it. Enter something like cat myfile.c.


grep is how you "Globally search for a Regular Expression and Print matching lines". In other words, it lets you search a file for a word or expression. For example, if you want to find all instances of "under the hood" in a file called cs107_lecture_notes.txt, you'd do grep "under the hood" cs107_lecture_notes.txt. This will print out every line of the file that contains the phrase "under the hood".

vim and emacs

You've probably realized to your disappointment that you can't use your favorite text editors like Sublime Text, VS Code, or (yikes) Qt Creator. You're going to have to learn how to use a command-line text editor. Yes, they exist, they're surprisingly powerful, and some programmers (like me) prefer them over graphical ones!

CS 107 supports the use of two text editors: vim and emacs. Both are reasonably popular in the real world, though vim is slightly more so. Both Chris Gregg and I prefer vim, but Nick Troccoli prefers emacs, so you'll find that most of the rest of the course staff knows emacs better.

You're free to choose either. Pick one and start using it as much as possible so it becomes more natural to your fingers.

To use vim as an example (though replace vim with emacs and everything should still work): you can create a new file with vim name_of_new_file.c, and edit an existing file with vim my_existing_file.c.