Introduction

Created by Christopher Lawrence GS '24

Welcome back to the Navigating Terminal Cyber-space! In this tutorial we will learn how to quickly look at the contents of a file, how to rename them, and how to move them around within our computers framework. All without using the mouse!

Files Needed for Today:
canfam-ychr.fa
 

Getting Started

If we want to view the contents of a file, we might use the mouse and double click on an icon or filename somewhere on the Desktop. Like so.

double

In the terminal, however, we don't have that ability as it is text based. Meaning, we wont be able to view images, per say, but we still have the ability to view and interact with text files. To do so we will use the cat command. But before we do that we need to get to our Desktop. We can do that from the command line using the cd command.

cd ~Desktop

We will cover this command in the next section. So for now move on to cat.
 

cat

Cat is short for concatenate or to link things together in a string or chain. This command will spit out a string of the file contents to our terminal screen. If we cat the file we created in the last exercise, we find out that its empty. We can also see that the file is empty when I open it above.

cat file.txt

We could have also used ls -sh to find out how big or how much data is contained within the file.

ls -sh

lssh

If we look to the left of our file.txt, we see a 0kb. There is no data in this file. But what if there was? We can pass data to the file using cat! Below, I am filling our file with the DNA sequence of a dog that I have on file.

cat canfamychr.fa >> file.txt
ls -sh

catdata

We can check to see that it worked with ls -sh. See above. And there it is! Our data copied from one file to the next. Try viewing file.txt using cat.

cat file.txt

cat

Viewing every single line in a file can be overwhelming. Especially if there are gigs of data on the screen output! The output above, doesn't even contain the beginning of the file. That's how large it is! To get around this we can use two commands to view certain parts of the data.
 

head

The head command does what its name implies, views the head, headers, and start of the file. There is a useful flag -n, that allows the user to designate how many lines of output to be printed to the terminal screen. Try this out for yourself.

head file.txt head -n 3
file.txt .

head

tail

Similar to the head command, where head displays the head, tail displays the tail or the end of the file. The -n flag works here too!

tail file.txt
tail -n 3 file.txt

tailtxt

A caveat to both these commands.

  • If the file is a non-readable format (i.e. cannot be converted to text for viewing on the terminal, contains images, or is a binary) you may get out a bunch of gibberish on the terminal. Just an FYI.
  • And, if all else fails, ...its okay to view the file using the mouse and GUI.

BUT..before you take a break from the terminal and go and open up Word or LibreOffice, the terminal comes with its own text editors!!
 

Command Line Text Editors and What Makes Them Special.

They may not have all the bells and whistles of Word or LibreOffice, but they are functional and useful for split-second editing of files, code, and software launched with your computer. Now, there are many text editors on the command line (Nerds am I right??). But for the purpose of this tutorial we are going to focus on the big three: Nano/ VIM/Emacs. Each have their own fan-bases who have strong feelings about the others etc.

emacs
emacs

Some of these editors need to be installed. You can do that here.

  • For mac users, nano, emacs, and vim (termed vi on OSX) are already installed.
  • For linux users, I suggest running sudo apt get (editor here).

Lets start with the simplest.
 

Nano

Nano is the most basic of the text editor options. But don't be fooled! Its useful for getting into a file quickly and making fast changes before returning to the command line. Lets try it out. Before we used cat to view file.txt. Lets use nano this time around.

nano file.txt

nanotxt

Once in Nano, all commands to operate within this space can be conveniently found at the bottom of the screen. CTRL+X to save and exit the file. In the last tutorial, we used touch to create a new file. But, we can do the same thing with nano!! However, if we use nano, we must add some data or text in order to save the file. We cant use nano to create a placeholder file, like we did with touch.

nano fileb.txt

Nano is nice, and should you be drawn to it, use it with pride! I like nano. But don't take my word for it.

  • Pros: Simple, Easy, quick
  • Cons: Simple, not customizable
     

Emacs

Emacs is an "operating system" dressed up as a text editor. It comes with a built-in web browser, calculator, and you can even run Tetris within this mug! Emacs has a shortcut for basically everything. Meaning very efficient! And fully customizable. Lets try it out!

emacs

emacs file.txt
for google: emacs then using alt + x, type eww, return, then type the URL.
for tetris: emacs, alt + x, then type tetris, return.

tet
goog

To save and exit use CTRL+X+C

  • Pros: Customizable and extensive, Powerful
  • Cons: Can cause typing pain and not available by default. Must be installed.
     

VIM

"the" text editor favored by sysadmins, programmers, and anyone working on the command line everyday

Where nano favors simplicity, and emacs throws the kitchen sink, VIM does its job and does it exceedingly well. VIM is "the" text editor favored by sysadmins, programmers, and anyone working on the command line everyday. The die hard command line users go all in for VIM.

Why?

Well once you get used to vim, it can make you super fast and efficient. With the ability to edit and view multiple files at once, perform global substitutions with a couple of keyboard clicks, you can magically change files and make large swaths of text appear at your fingertips. Because of this, VIM is the default editor for the majority of linux distributions. Lets try it out.

vim file.txt

vim

To save and exit use :q (Thats colon + q, not the emoji**)**

  • Pros: powerful, simple, effective, unparalleled
  • Cons: has a learning curve, can be dangerous to use without having practiced, can be overkill.

This completes part 2.

That covers it for this section! In the next section we will cover moving around in our cyber space, creating folders using the terminal and some other medium level administrative actions.