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
mansexist? No, it stands for "manual".
To open the manpage for a command like
man ls. It will open the
ls, including what it does and a listing of the flags. You
can scroll to move around, and you quit by typing
manis a command too, so it's valid to enter
man manif you want to learn how to use
man. But it'll open
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
mkdir is how you "MaKe DIRectories". To create a directory in the current
rmdir is how you "ReMove DIRectories". Turns out it only works for empty
directories. To delete a directory, enter
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
rm -r path_to_directory_to_delete.
Be very careful with using
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
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
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
cat is how you "conCATenate" files. Ok, so that doesn't really explain
cat outputs a file's contents to the display so you can view it.
Enter something like
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".
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:
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
You're free to choose either. Pick one and start using it as much as possible so it becomes more natural to your fingers.
vim as an example (though replace
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