Shell Basics

“Shell” is a command-line interpreter usually found in Unix and Unix-like systems. It is what you get when you access a Ubuntu system via ssh (including when you connect to your VM using vagrant ssh). You can also get a shell on your Mac host via the “Terminal” program, and a shell on your Windows host using programs such as MobaXterm.

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@trusty64:~$; 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 short tutorial that  Ademola (our very own UTA) created. We highlight a few more interesting commands below.

 

Viewing a File

To see what’s in a text file named myfile, simply use the command:
less myfile
Unlike catless 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

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

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/.