Leaving presentation mode.

Create, View, and Edit Text Files

Dennis Kibbe

Mesa Community College

Keyboard Shortcuts

Module Outline

Learning Objectives

After completing the work in this module you will be able to:

Redirect Output to a File or Program

Shell redirection lets you redirect the output of the keyboard to a file instead of displaying it on the monitor. A pipe takes the output of a command and uses it as the input to another command.

Standard Input, Standard Output, and Standard Error

[student@servera ~]$ ls -R /etc/      
      

Standard Input is how you communicate with a program usually with the keyboard or it could be with the mouse or another means. Standard Output, and Standard Error are the responses you get from the program. Normally, you want what you type on the keyboard to show on your monitor but sometimes you want to redirect some or all of the output to a file instead.

Redirect Output to a File

[student@servera ~]$ ls -R /etc/ > file1
      

In this example standard output is redirected to a file overwriting the content if the file already exists while standard error still goes to the monitor.

Append Output to a File

[student@servera ~]$ ls -R /etc/ >> file1
      

Use a double > if you want to keep the content in the file and append the new content to the end of the file.

Redirect Standard Error to a File

[student@servera ~]$ ls -R /etc/ 2> file2
      

In this example Stderr is redirected to a file.

Redirect Standard Error to the Bit Bucket

[student@servera ~]$ ls -R /etc/ > file1 2> /dev/null
      

If you are not interested in seeing any error messages you can redirect standard error to /dev/null which is known as the "Bit Bucket" a black hole for data. This is useful in Bash shell scripts.

Redirect Both Standard Output and Standard Error to the Same File

[student@servera ~]$ ls -R /etc/ > file1 2>&1
      

Both standard output and standard error can be redirected to the same file.

Redirect Standard Output and Standard Error to Different Files

[student@servera ~]$ ls -R /etc/ > file1 2> file2
      

In this example standard output is redirected to file1 while standard error is redirected to file2.

Construct Pipelines

[student@servera ~]$ cat /etc/passwd | wc -l
      

A pipeline is a sequence of one or more commands that are separated by the vertical bar character (|). A pipeline connects the standard output of the first command to the standard input of the next command. This example will display the number of accounts on the system.

Graded Quiz

Redirect Output to a File or Program

After reading this section in the textbook complete the quiz in Canvas.

Edit Text Files from the Shell Prompt

Unlike Windows system administration in Linux is often done by editing plain text files using a text editor like Vim. While graphical editors exist they are often impractical when you need to configure a remote server over a slow connection.

Edit Files with Vim

Vim has become the standard command line editor in UNIX and Linux because it is available on all distributions, works over slow connections, and allows you to make quick, precise edits. While Vim is not intuitive nor is it menu-driven, every system administrator needs to have a basic understanding of how Vim works.

Benefits of the Vim Editor

Get Started with Vim

[student@servera ~]$ vim myscript.sh
      

To get started with Vim simply type vim plus the name of the file you wish to edit. Vim will recognize type of file (in this case, a Bash shell script) and provice the correct syntax coloring which helps to catch errors as you type.

Vim Operating Modes

  1. Normal mode: Keyboard input navigates around the document (h, k, j, l)
  2. Insert mode: Keyboard input edits the text. (i, a, A)
  3. EX mode: Execute advanced commands (set number)
  4. Visual mode: Select rows and columns of text (v, V, Ctrl+v)
  5. Command-Line mode: More powerful file editing plus save and exit Vim
  6. Replacing mode: Text substitution (r, x)

Unlike other text editors, in Vim you do not start off immedently editing a file. When you open Vim you are placed in the Normal mode. In the Normal mode keystrokes move you around the screen. To actually edit a file you need to enter the Insert mode by pressing i on the keyboard. To save changes to a file requires that you first press the Escape key to return to the normal mode and press the colon (:) character to enter the Command mode. In the Visual mode you can edit text in columns and rows.

The Minimum, Basic Vim Workflow

These basic key strokes will let do most editing in Vim. To become more efficient in Vim see some of the resources at the end of this presentation.

The .vimrc Configuration File

You can customize how Vim runs in a hidden .vimrc file. In this example the proper indentation for YAML files and line numbers are set.

Recover a Vim Session

Vim saves edits as you type in case Vim dies while editing a file. This could be caused, for example, by a broken network connection. Vim creates a hidden swap file and offers to recover your work when you restart Vim. This short video shows how it works.

The vimtutor Command

The vimtutor program which is installed along side Vim is a good place to start learning the basics of Vim. When the vimtutor program is run it starts an instance of Vim with a practise tutorial file which makes it easy to get up to speed on the basics of using the Vim text editor.

Guided Exercise

Edit Text Files from the Shell Prompt

Use the vimtutor command to practice basic editing techniques in the vim editor.

Change the Shell Environment

[student@servera ~]$ printenv $PATH
/home/user/bin:/usr/local/bin:/usr/bin:/bin
[student@servera ~]$ printenv $HOME
/home/user
      

A Linux system uses many preset variables which set the environment every time a shell is opened. Examples are the PATH variable which is a list of directories where commands can be found and the HOME variable which defines the user's home directory.

Shell Variable Usage

[student@servera ~]$ VARIABLENAME=value
      

The syntax for setting a variable is the variable name followed by an equal sign followed by the value assigned to the variable. Like most things in Linux variables are case sesnitive. Shell variables are unique to the current shell and disappear when the session is terminated.

Shell Variable Usage

[student@servera ~]$ set | less
BASH=/bin/bash
BASH_VERSION='4.4.23(1)-release'
HISTFILE=/home/user/.bash_history
HISTFILESIZE=1000
OSTYPE=linux
PAGER=less
PATH=/home/user/bin:/usr/local/bin:/usr/bin:/bin
output omitted...
      

The set command lists all the varibles set for the current shell both environmental variables and shell variables.

Retrieve Values with Variable Expansion

[student@servera ~]$ pet=hamster
[student@servera ~]$ echo "My favorite pet is my $pet."
My favorite pet is my hamster.
      

You create a variable by assigning a value to a name with the equal sign. To use the variable the dollar sign tells the shell that the second instance of pet should be replaced with the value hamster.

Configure Bash with Shell Variables

[student@servera ~]$  PS1="[\u@\h \W]\> "
[student@servera ~]>
      

The PS1 prompt is a shell variable and can be changed to display a custom prompt when a Bash shell is opened. Here the > is used to replace the dollar sign at the end of the prompt for a non-priviledged user. See Resources below for further customization options.

Configure Programs with Environment Variables

[student@servera ~]$ export EDITOR=nano
      

The visudo command consults the environmental variable EDITOR to determine which editor to use. If you want to use a different editor, nano for example you can use the export command to set nano as the default editor temporarily.

Set Variables Automatically

[student@servera ~]$ echo "export EDITOR=nano" >> ~/.bash_profile
      

To make the editor change permanent append the command to ~/.bash_profile. The Bash shell reads ~/.bash_profile each time it starts.

Bash Aliases

[student@servera ~]$ type ls
ls is aliased to 'ls --color=auto'
      

Creating a Bash alias lets you run a long command with a minimal amount of typing. In this example the type command shows that ls is actually an alias for ls --color=auto. There is a short exercise in Canvas where you will create a simple alias.

Make a Bash Alias Permanent

When the Bash shell is closed the newly alias is no longer availabe. To make the alias permanent add it to the ~/.bashrc file. When the shell starts it will use any aliases found in that file. This excerpt from the ~/.bashrc file on my laptop shows several alaises and a Bash function used to back up the labtop to an AWS S3 bucket.

Unset and Unexport Variables and Aliases

To temporarily unalias a command run it with the backslash (\) character.

Guided Exercise

Change the Shell Environment

In this exercise you use shell variables and variable expansion to run commands, and set an environment variable to adjust the default editor for new shells.

Summary

  1. Processes communicate through standard input, standard output, and standard error.
  2. You can use I/O redirection to redirect output to a file or files.
  3. Using a pipe lets you build more complex commands.
  4. The Vim text editor is found on all UNIX and Linux distributions and a basic understanding of how Vim works is required.
  5. Shell variables are unique to a shell session.
  6. A Bash alias lets you customize a command.
  7. Environmental variables can be used by commands and scripts.

Resources

Graded Lab

Create, View, and Edit Text Files

In this lab you edit a text file with the Vim editor. When you complete the lab submit a screenshot of the output of the lab grade edit-review command.

Thanks for watching

MCC logo
Created 18 February 2025 by Dennis Kibbe. Last modified $Date: 2025/02/19 14:07:11 $ by $Author: dnk $.