Creating, Viewing, and Editing Text Files
Tags: linux
Published: 2022-09-26 14:26:07

Redirecting Output to a File
I/O redirection changes how the process gets its input or output. Instead of getting input from the keyboard, or sending output and errors to the terminal, the process reads from or writes to files. Redirection lets you save messages to a file that are normally sent to the terminal window. Alternatively, you can use redirection to discard output or errors, so they are not displayed on the terminal or saved.

Redirecting stdout suppresses process output from appearing on the terminal. As seen in the following table, redirecting only stdout does not suppress stderr error messages from displaying on the terminal. If the file does not exist, it will be created. If the file does exist and the redirection is not one that appends to the file, the file's contents will be overwritten.
If you want to discard messages, the special file /dev/null quietly discards channel output redirected to it and is always an empty file.
Examples for Output Redirection

Many routine administration tasks are simplified by using redirection. Use the previous table to assist while considering the following examples:

- Save a time stamp for later reference.

```
[user@host ~]$ date > /tmp/saved-
timestamp
 ```

- Copy the last 100 lines from a log file to another file.

```
[user@host ~]$ tail -n 100 
/var/log/dmesg > 
/tmp/last-100-boot-messages
```

- Concatenate four files into one.

```
[user@host ~]$ cat file1 file2 file3 file4
 >/tmp/all-four-in-one
```

- List the home directory's hidden and regular file names into a file.

```
[user@host ~]$ ls -a > 
/tmp/my-file-names
```

- Append output to an existing file.

```
[user@host ~]$ echo "new line of 
information" >> /tmp/many-lines-
of-information

[user@host ~]$ diff previous-file 
current-file >> /tmp/tracking-
changes-made
```

- The next few commands generate error messages because some system directories are inaccessible to normal users. Observe as the error messages are redirected. Redirect errors to a file while viewing normal command output on the terminal.

```
[user@host ~]$ find /etc -name 
passwd 2> /tmp/errors
```

- Save process output and error messages to separate files.

```
[user@host ~]$ find /etc -name 
passwd > /tmp/output 2> /tmp/errors
```

- Ignore and discard error messages.

```
[user@host ~]$ find /etc -name 
passwd > /tmp/output 2> /dev/null
```

- Store output and generated errors together.

```
[user@host ~]$ find /etc -name 
passwd &> /tmp/save-both
```

- Append output and generated errors to an existing file.

```
[user@host ~]$ find /etc -name 
passwd >> /tmp/save-both 2>&1
```

Constructing Pipelines
A pipeline is a sequence of one or more commands separated by the pipe character (|). A pipe connects the standard output of the first command to the standard input of the next command.
Pipeline Examples

This example takes the output of the ls command and uses less to display it on the terminal one screen at a time.

```
[user@host ~]$ ls -l /usr/bin | less
```
The output of the ls command is piped to wc -l, which counts the number of lines received from ls and prints that to the terminal.

```
[user@host ~]$ ls | wc -l
```
In this pipeline, head will output the first 10 lines of output from ls -t, with the final result redirected to a file.

```
[user@host ~]$ ls -t | head -n 10 > 
/tmp/ten-last-changed-files
```
Editing Text Files from the Shell Prompt
Objectives
After completing this section, you should be able to create and edit text files from the command line using the vim editor.

Editing Files with Vim
A key design principle of Linux is that information and configuration settings are commonly stored in text-based files. These files can be structured in various ways, as lists of settings, in INI-like formats, as structured XML or YAML, and so on. However, the advantage of text files is that they can be viewed and edited using any simple text editor.

Vim is an improved version of the vi editor distributed with Linux and UNIX systems. Vim is highly configurable and efficient for practiced users, including such features as split screen editing, color formatting, and highlighting for editing text.
Starting Vim

Vim may be installed in Red Hat Enterprise Linux in two different ways. This can affect the features and Vim commands available to you.

Your server might only have the vim-minimal package installed. This is a very lightweight installation that includes only the core feature set and the basic vi command. In this case, you can open a file for editing with vi filename, and all the core features discussed in this section will be available to you.

Alternatively, your server might have the vim-enhanced package installed. This provides a much more comprehensive set of features, an on-line help system, and a tutorial program. In order to start Vim in this enhanced mode, you use the vim command.

```
[user@host ~]$ vim filename
```
Vim Operating Modes

An unusual characteristic of Vim is that it has several modes of operation, including command mode, extended command mode, edit mode, and visual mode. Depending on the mode, you may be issuing commands, editing text, or working with blocks of text. As a new Vim user, you should always be aware of your current mode as keystrokes have different effects in different modes.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pplgghx2szxhy9dxjb11.png)
When you first open Vim, it starts in command mode, which is used for navigation, cut and paste, and other text manipulation. Enter each of the other modes with single character keystrokes to access specific editing functionality:

- An i keystroke enters insert mode, where all text typed becomes file content. Pressing Esc returns to command mode.

- A v keystroke enters visual mode, where multiple characters may be selected for text manipulation. Use Shift+V for multiline and Ctrl+V for block selection. The same keystroke used to enter visual mode (v, Shift+V or Ctrl+V) is used to exit.

- The : keystroke begins extended command mode for tasks such as writing the file (to save it), and quitting the Vim editor.
The Minimum, Basic Vim Workflow

Vim has efficient, coordinated keystrokes for advanced editing tasks. Although considered useful with practice, Vim's capabilities can overwhelm new users.

The i key puts Vim into insert mode. All text entered after this is treated as file contents until you exit insert mode. The Esc key exits insert mode and returns Vim to command mode. The u key will undo the most recent edit. Press the x key to delete a single character. The :w command writes (saves) the file and remains in command mode for more editing. The :wq command writes (saves) the file and quits Vim. The :q! command quits Vim, discarding all file changes since the last write. The Vim user must learn these commands to accomplish any editing task.
Rearranging Existing Text

In Vim, copy and paste is known as yank and put, using command characters y and p. Begin by positioning the cursor on the first character to be selected, and then enter visual mode. Use the arrow keys to expand the visual selection. When ready, press y to yank the selection into memory. Position the cursor at the new location, and then press p to put the selection at the cursor.

Visual Mode in Vim

Visual mode is a great way to highlight and manipulate text. There are three keystrokes:

- Character mode: v

- Line mode: Shift+v

- Block mode: Ctrl+v

Character mode highlights sentences in a block of text. The word VISUAL will appear at the bottom of the screen. Press v to enter visual character mode. Shift+v enters line mode. VISUAL LINE will appear at the bottom of the screen.

Visual block mode is perfect for manipulating data files. From the cursor, press the Ctrl+v to enter visual block. VISUAL BLOCK will appear at the bottom of the screen. Use the arrow keys to highlight the section to change.