Lab: Unix basics
Feel free to work alone or with a partner on this assignment. If you work with a partner, definitely make sure both people get some time at the keyboard. This stuff takes practice, and you want to get some of the basic commands comfortable under your own fingers. Don't hesitate to take advantage of our Slack #questions channel if you get stuck or find yourself wondering about something.
Goals
- Learn a handful of the most commonly used Unix commands.
- Learn a little bit about Unix file permissions.
- Learn a little bit about the vim text editor.
What to do
- Scan the list in the section Commands to learn below.
- Try a bunch of experiments with those commands to get to know how they work. Play around, try weird stuff, ask questions, see what happens.
- Learn enough vim to be able to perform the tasks listed in the vim section below.
- Answer the questions at this survey.
Do you need to be careful?
The main command to worry about is rm, which removes/deletes files.
Unix systems are generally not set up by default to use any kind of recycle bin or trash folder,
so if you do rm important-stuff.txt, you are likely out of luck. There
are ways to mitigate this danger, but for now, just go slowly enough that you can
think about what you're doing when you execute rm.
This particular lab does not introduce you to the redirection operator >,
which presents a danger similar to that of rm. We'll get to that soon.
Online resources
If you like to have more of a guided tour, here are a few nice options that will take you through the commands listed below, among other things.
- This is an old but still excellent tutorial from the University of Surrey that we've used at Carleton for a couple decades. (In fact, as I search the internet for more tutorials, I am discovering that this one tutorial has been adapted by dozens of schools. Guess I wasn't wrong to like it the first time I found it back in the day.)
- Even so, some schools offer homegrown tutorials to their students. Here's one from Swarthmore.
- It looks like there are some video options if that's your preference. I haven't found one yet that I'm ready to recommend. If you find a video intro that you like, please post a link to #general on Slack.
Special note for WSL
If you're using Windows Subsystem for Linux (WSL), then your WSL home directory bears a weird relationship to your Windows home folder.
Suppose you want to list the contents of your Windows Desktop. Let's assume that your top-level Windows drive is the C drive (which it is by default on nearly all consumer-grade Windows computers). Then you could use this Unix command to see what's on the desktop:
That is, from the WSL command line, the C drive is named /mnt/c. Here, "mnt"
is short for "mount", which is the verb Unix systems use to refer to attaching a storage
drive to a file system.
One neat trick
Sometimes you just want a little file to play with when you're learning Unix commands. You can make such files like so:
Now you have a file named ungulate-pals.txt in your current directory. Could be handy
for practicing cp, mv, rm, wc, etc.
Commands to learn
Documentation
The command man nameofcommand will give you technical documentation for the
specified command. So, to learn more about ls, you would do
man ls and start reading.
The man command uses a common user interface in Unix systems, where you can
go forward one page by hitting [spacebar], backward one page with b,
up or down one line with the up-arrow and down-arrow, and quit with q.
Try it!
Directories/folders
Examining files
Simple file manipulation
Learn a little vim
vi (pronounced VEE-EYE) dates back to 1976, and has been my editor of choice since 1980. Its modern versions (where "modern" means anything since 1991) are usually referred to as "vim" (for "vi improved", pronounced VIM). There are tons of tutorials, including a vim-learning game, a kind of gamelike tutorial, and lots of short introductions.
When you're working in a Unix command-line environment, it doesn't take long for you to find yourself confronted by editing in vi, sometimes by accident, because it's often the default editor used by other programs. Even just knowing how to recognize that you're in vi, and how to save and quit or quit without saving, will help you out of some unexpected situations. Of course, once you get the hang of it, vi is also extremely powerful and extremely convenient.
Learn enough vi that you can do the following:
- create a new text file named something.txt
- add a few lines of text to it
- save
- quit
- go back in
- delete a line
- delete a word
- delete a character
- add a word in the middle of a line
- search for a word
- quit
- go back in, make a change, and quit without saving
Once you can do those things, you're on your way and you won't be afraid to edit a file at the command line.