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

What to do

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.

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:

ls /mnt/c/Users/YOUR_USER_NAME_GOES_HERE/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:

echo "the moose was hanging out with the elk" > ungulate-pals.txt

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

ls cd directory cd .. pwd mkdir directory rmdir directory

Examining files

ls ls -a ls -l ls -al cat filename less filename head -3 filename tail -3 filename wc filename wc -l filename

Simple file manipulation

rm filename cp filename newfilename mv filename newfilename

 

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:

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.