Linux Basics

Shell Basics

“Shell” is a command-line interpreter usually found in Unix and Unix-like systems (including Linux and Mac OS). It is what you get when you access a Ubuntu system via ssh.

When the shell is ready for your commands, it displays a shell prompt. On our VM, it has the form USER@HOSTNAME:current_path$, where current_path tells you where you currently are in the directory (folder) hierarchy. For example, the prompt may be vagrant@xenial64:~$; here, ~ stands for your home directory. You type in a command, hit [RETURN], and the command will execute. When the command finishes, the shell gives your another prompt. If a command hangs and you want to stop it, [CTRL]-c will usually do the trick.

If you are new to shell, please check out this tutorial for beginners. At least go through the sections “Navigation,” “Looking Around,” and “Manipulating Files.” We highly recommend going through the entire tutorial; it is not that long.

Shell provides access to a wide range of useful commands with text interfaces. We sample several of them below.

Viewing a File in Shell

To see what’s in a text file named myfile, simply use the command:
less myfile
It enters a “paging” mode, which lets you view/scroll/search the file inside your terminal. When less is running, you can use the following key strokes/sequences:
[SPACE] or f goes forward one page, and b goes back one page.
[UP] scrolls up one line, and [DOWN] scrolls down one line.
g goes to the beginning of the file, and G goes to the end of the file. More generally, type in a number and then g, and you will go to that line of the file.
/ lets you search for a string, and n goes to the next match in the output.
? lets you search in the reverse direction.
h lets you view the help page.
q quits less and brings you back to the shell.

Editing a File in Shell

Both nano and vim come pre-installed on your VM and lets you edit text files within a shell. nano is very easy to learn, and we recommend that if you are new to shell. To edit a (new or existing) text file named myfile, simply use the command:
nano myfile
The rest is pretty self-explanatory. The bottom of the screen always show the list of commands. ^x means [CTRL]-x.

vim is very powerful but can take quite a while to get used to. To learn vim, we recommend a nice interactive tutorial.

Searching Through Big Files or Lots of Files in Shell

If you have big files or many files to search through, use grep. Here are some common usage examples:
grep 'foo' file finds lines in file containing the string foo (anywhere within the line).
grep -n 'foo' file also give you the line numbers.
grep 'foo' *.sql searches for foo in all files in the current directory whose name ends with .sql.
grep -r 'foo' dir/ recursively searches for foo in all files found anywhere under the directory dir/.

Working with Files Produced under other Operating Systems

In some setups (such as running your Linux VM on a Windows host), you may end up using an application from another operating system to generate/edit a text file, which is shared with or transferred to Linux. When you use this text file in Linux, you might run into various issues depending on what non-Linux applications you used:

  1. Windows and Unix/Linux use different characters to represent a line break in a text file, and programs on these two platforms will have a difficult time working with files produced on the other platform. We recommend that you use Windows applications that are aware of this difference and can produce output in the desired format. For example, for editing text files, sublime text and notepad++ both have options that you can set for line endings—make sure you select “Unix.” (Luckily, Mac users don’t need to worry about this problem as Mac and Unix/Linux use the same format.)
  2. Some text editors under Windows and Mac (such as TextEdit) try to act “smart” and will automatically convert ASCII quote characters ' and " into fancier begin- and end-quote versions. These fancier quotes are a problem for source code (e.g., SQL, Python) files. If you use such text editors, make sure you turn off the “smart quote” feature.

Linux Desktop Basics

Assuming you have followed the instructions to obtain GUI access to your VM, you will be presented with the a familiar desktop interface (in this case, a lightweight desktop by Lubuntu). Here is a list of useful GUI-based programs already installed on your VM. You can find them by clicking on the icon located at the lower-left corner of the VM desktop.

  • LXTerminal (under System Tools) gives you a VM shell. You might find it convenient to run multiple shells for multitasking. If the default font is too small, you can always change LXTerminal’s preferences.
  • File Manager PCManFM (under Accessories) gives you a GUI for exploring folders and files on your VM.
  • Document Viewer (under Graphics) is for viewing PDF and images.
  • Firefox (under Internet) is the default Web browser. You can use to access course website and Sakai and submit your work from within your VM directly (without transferring files to your host for submission).

You can also use the Lubuntu Software Center (under System Tools) to browse and install other software on your VM.