Working with Files and Text#

Information

Details

Learning Objectives

• Use basic text viewing and editing commands
• Find files and search within files
• Process text using pipes and redirection

Prerequisites

Basic command line usage

Estimated Time

45 minutes

Topics

Command line, text processing, file operations, pipes

Text Viewing and Editing#

Basic File Viewing#

Several commands are available for viewing file contents, each with different strengths:

  1. cat - Display entire file content

    cat filename.txt          # Show file content
    cat -n filename.txt      # Show with line numbers
    
  2. less - View long files page by page

    less filename.txt        # View file with scrolling
    
    • Use Space to move forward one page

    • Use b to move back one page

    • Use / to search for text

    • Press q to quit

  3. head and tail - View start or end of files

    head filename.txt        # Show first 10 lines
    head -n 5 filename.txt   # Show first 5 lines
    tail filename.txt        # Show last 10 lines
    tail -f log.txt         # Watch file in real-time (useful for logs)
    
  4. more - Simple file pager (older version of less)

    more filename.txt       # View file page by page
    

Combining View Commands#

You can combine these commands for more specific views:

# View lines 5-10 of a file
head -n 10 filename.txt | tail -n 6

# Watch the last few lines of a log file
tail -f /var/log/syslog | grep "error"

Text Editors#

Occasionally, you may need to edit a file in the shell. There are several editors available. The most common ones are nano, vim, and emacs.

Nano (Beginner-friendly)#

The nano editor is a simple text editor that is available on most Linux distributions. It is easy to use: to edit a file, do nano FILENAME. When nano opens, just type your text.

To exit nano, press Ctrl + X. If you have unsaved changes, nano will ask you confirm.

Here is an online video to refer to:

Vim (Advanced)#

vim is a powerful text editor with a steep learning curve. As an evidence, the question how do I exit vim on StackOverflow has over 3 million views.

Vim has two modes, normal mode and insert mode. In normal mode, you can navigate and execute commands. In insert mode, you can type text. You enter insert mode by pressing i, and you exit back to normal mode by pressing Esc.

Exiting Vim

To exit vim, press Esc to ensure you are in the normal mode. Then type :q

To save a file, you can type :w. To save and exit, you can type :wq.

With the very basics, you will at least not stuck in vim. If interested, there are plenty of materials to teach yourself.

Below is a video called “Vim in 100 Seconds”. It serves as a great intro. But honestly, you can’t go from zero to hero on Vim in just 100 seconds :-)

Below is a Vim crash course for Beginners (30 minutes):

Finding Things#

Find Files#

The find command is used to search for files in a directory.

User@Machine:~$ find . -iname "file.txt"
./file.txt

where -iname is the combination of -i for case-insensitive search and -name for specifying the file name.

Search in Files#

grep is used to search for a pattern in a plain-text file.

User@Machine:~$ grep -i "hello" file.txt
Hello, World!

You can specify a path to the file or use a wildcard to search in multiple files.

User@Machine:~$ grep -iR "hello" .
Hello, World!

It searches for the pattern hello in all files in the current directory (.), recursively (-R).

Exercise

Use the find command to locate all .log files within the power_logs directory.

Text Processing#

Echo / Display Text#

The echo command is used to display a line of text.

User@Machine:~$ echo "Hello, World!"
Hello, World!

The echo command is powerful when combined with other commands, such as the redirection operator >.

Pipes#

Pipes | are used to connect the output of one command to the input of another command. A basic idea of UNIX is that you can chain commands together to do complex tasks. Pipes are what makes the shell powerful.

For example, ls -l lists the files and folders, By inspection of ls -l, you can tell that the first line is the heading that indicates the number of disk blocks used. The rest of the lines are the files and folders.

The command wc -l counts the number of lines. You can combine them to count the number of files and folders:

User@Machine:~$ ls -l | wc -l
25

The output of ls -l is piped to wc -l. The output is 25, so excluding the header, there are 24 files and folders.

Redirection#

The redirection operator > redirects the output of a shell command to a file.

User@Machine:~$ echo "Hello, World!" > file.txt
User@Machine:~$ cat file.txt
Hello, World!

There are two types of redirection operators. > overwrites the file, while >> appends the output to the file.

Further Reading: Linux File Descriptors

In Linux, everything is a file. That includes the standard input, output, and standard error. They correspond to file descriptors 0, 1, and 2, respectively.

  • > redirects the standard output (file descriptor 1) to a file.

  • 2> redirects the standard error (file descriptor 2) to a file.

  • &> redirects both the standard output and standard error to a file.

Advanced Features#

Environment variables are used to store information that can be used by programs. They are set in the shell and are inherited by child processes. The env command is used to list the environment variables. You will likely see a long output with many variables.

PATH#

A well-known environment variable is PATH. It is a colon-separated list of directories where the shell looks for executable files. When you type a command, the shell looks for the command in the directories listed in the PATH variable. This is how you can run a command without specifying the full path.

To find out where a command is located, you can use the which command.

User@Machine:~$ which ls
/usr/bin/ls

This command shows that the ls command is located at /usr/bin/ls.

Manual Pages#

Most commands support --help or -h to show a brief description of the command. The full usage and options are documented in the manual pages. man COMMAND shows the manual page for the command.

man rmdir

will open the manual page for the rmdir command. When browsing, press q to exit, and use the arrow keys to scroll, and use / to search.

Quizzes#