Viewing a File
To see what’s in a small text file named myfile, simply use the command:
cat myfile
It will simply output the content of myfile in your terminal.
If myfile is long, you will want to use the following 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
Both nano and vim come pre-installed on your virtual machine and lets you edit text files within a terminal. 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. An advantage over nano is that vim provides nice syntax highlighting by default for many languages, including Python and SQL. 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/.