Linux Text Processing: Pipes and Redirection

Learn to work with text on the Linux command line. View files with cat, less, head and tail, send output and error messages to files, and chain small commands into pipelines that count, sort and cut text. Search inside files with grep and regular expressions, find files by name, size and age, and edit configuration files in the terminal editors nano and vi.

BeginnerLesson 3 of 7 in Linux Basics Course

What you will learn

  1. Display short files in full, page through long files, and show only the first or last lines of a file.
    • Display and page through files with cat and less
      Print short files with cat, read long files one screen at a time with less, and move and search inside less.
    • Show the first or last lines with head and tail
      Use head and tail to show the start or end of a file, skip a header line, pick out a range of lines and follow a growing log file.
  2. Explain the three standard streams of a command and redirect input, output and error messages to and from files.
    • Understand standard input, output and error
      Describe the three standard streams, why output and errors are separate, and how a command can read its input from a file instead of the keyboard.
    • Redirect output and errors to files
      Save output and error messages to files by overwriting or appending, combine or discard streams, and avoid destroying a file by redirecting onto it.
  3. Connect small commands with pipes to count, sort, de-duplicate and cut text, answering questions about data in a single command line.
    • Connect commands with pipes and tee
      Use the pipe to pass one command's output to the next, build and check a pipeline stage by stage, and save a copy along the way with tee.
    • Count, sort and remove duplicate lines
      Count lines, words and bytes with wc, order lines alphabetically, numerically or by a field with sort, and remove or count duplicates with uniq.
    • Extract columns with cut
      Select fields from delimited text and characters from fixed positions with cut, and use it inside pipelines to answer questions about data.
  4. Find lines in files with grep, and describe the text to look for precisely with basic and extended regular expressions.
    • Search files with grep and its main options
      Use grep to print, count, number or invert matching lines, search whole folders, and choose between pattern and fixed-text searches.
    • Write basic regular expressions
      Build patterns from literal characters, the dot, anchors, bracket expressions and the star, and avoid confusing them with file-name wildcards.
    • Use extended regular expressions with grep -E
      Use the operators ?, +, { }, | and ( ) with grep -E to match optional, repeated and alternative text, and print only the matching parts.
  5. Locate files anywhere in the directory tree with find and locate, and run commands on the files found.
    • Search the directory tree with find
      Use find with name, type, size and time tests, combine tests, and avoid the quoting and permission problems beginners meet.
    • Act on found files and use faster lookups
      Run commands on every file find returns, delete matches safely, and know when locate or which is the quicker tool.
  6. Open, change, save and close text files in the two terminal editors found on most Linux systems: the simple nano and the modal vi.
    • Edit and save a file in nano
      Open a file in nano, type and change text, search and replace, cut and paste lines, and save and exit correctly.
    • Work in vi's modes
      Switch between vi's normal, insert and command-line modes, move and edit with single-key commands, and save or quit without losing work.

Linux Text Processing study guide

6 chapters

1

View files from the terminal

A text file may hold a few notes or thousands of settings. Choose a view that fits what you need: the whole file, a screen you can move through, or a small part near either end. You will learn how to move and search without editing, select an exact line range, and watch text arrive in a growing file. These viewing commands also accept output from other commands, so the same skills help you inspect a long directory listing or a pipeline's results.

1.1 Display and page through files with cat and less

Short files are easy to print. Long files are easier to read when the display waits for you. Neither cat nor the normal reading view in less edits the file.

Print or join text with cat

cat notes.txt sends the entire file to standard output, normally your terminal. The name comes from concatenate: with several file names, it prints their contents in the order you give them. It does not insert a heading between files.

Suppose a.txt contains the lines "a" and "b", and b.txt contains "c d". Run:

cat -n a.txt b.txt

You see three numbered lines. The numbers continue across the two files; -n numbers the output, not the stored files. To save the joined text, use cat a.txt b.txt > ab.txt. The new file contains the three original lines without numbering. Including -n would save the numbers too.

Printing a large file may push its beginning beyond the visible screen. A pager keeps you in control of the view. less /etc/services opens a long text file and shows a screenful. It can show the first screen before reading the entire file, which helps with large files.

Move through less

The shell prompt is hidden while less is running. The program is waiting for a navigation key:

KeyAction
Space or fMove forward one screen
bMove back one screen
Down arrow, Enter, or jMove down one line
Up arrow or kMove up one line
g / GJump to the first / last line
h / qShow help / quit

Uppercase matters: g and G go to opposite ends. To finish reading, press q; waiting for a new shell prompt without quitting can make the pager seem stuck.

Search inside the view

Type /, enter a word, and press Enter to search forward. For example, /ssh finds the next matching text. Type ?ssh and Enter to search backward. Then n repeats the search in its current direction, while N goes in the opposite direction. After a backward search, n continues backward.

The search is a regular expression. /^# finds a line beginning with "#" because ^ marks the start of a line. You can learn the pattern rules separately while using simple words now.

less -N notes.txt adds line numbers to the display. For wide records, less -S notes.txt keeps a long line on one display row by cutting off the visible right end; Left and Right arrows move sideways. The text remains in the file. Without -S, long lines wrap onto extra screen rows. more is an older pager with fewer navigation choices.

Page output from another command

ls -l /usr/bin | less lets you browse the long listing. The pipe delivers the listing to the pager. For an existing file, less notes.txt opens it directly; cat notes.txt | less introduces an extra command for the same view.

Use these commands on text. Printing a binary program such as /bin/ls can show control characters and garble the terminal. If that happens, type reset and press Enter to restore the display.

Key Takeaways

  • cat prints files in the order supplied; -n numbers the combined output.
  • less pages text without editing it; q returns to the shell.
  • In less, / searches forward, ? searches backward, and n repeats the current direction.
  • less -N shows line numbers; less -S lets you scroll across wide lines.

1.2 Show the first or last lines with head and tail

When you need only a file's opening or latest entries, printing everything hides the useful part. head selects the beginning, and tail selects the end. Both print 10 lines by default.

Choose a number of lines

If numbers.txt contains 1 through 100, one number per line, head numbers.txt prints 1 through 10, while tail numbers.txt prints 91 through 100. Use -n to change the count:

head -n 3 numbers.txt
tail -n 3 numbers.txt

The first command prints 1, 2, 3. The second prints 98, 99, 100. If a file has fewer than the requested lines, the command prints what is available.

head -c 5 selects five bytes instead of five lines. For text beginning with "banana", it prints "banan". Bytes are storage units; with UTF-8 text, a character may use more than one byte, so a byte count is not always a character count.

Read a starting position correctly

A plus sign changes the meaning of tail -n. Compare:

CommandMeaning
tail -n 2 filePrint the last two lines
tail -n +2 filePrint from line 2 through the end

tail -n +2 scores.csv drops the first line, useful when that line holds column headings. It does not pick only the second line. On the numbered file, tail -n +98 numbers.txt prints 98 through 100.

GNU head also accepts a negative count: head -n -97 numbers.txt omits the last 97 lines, leaving 1 through 3. The sign belongs to the count and is part of the instruction.

Select a middle range

To display lines 11 through 20, first keep everything through line 20. Then keep the last 10 lines of that selection:

head -n 20 numbers.txt | tail -n 10

After head, the data is 1 through 20. The last ten lines are 11 through 20. An inclusive range contains the end number minus the start number plus one lines; that final one accounts for including both ends. Using tail -n 11 would also include line 10.

Both commands can read a pipe when no input file is named. ls /etc | head -n 5 limits a listing to its first five names. With ls -lt | head -n 4, remember that the first output line is the "total" line: only three entries follow it.

Watch a file grow

tail -f /var/log/app.log first displays the final lines, then waits and prints text appended to the file. Use it while a program writes new entries. Press Ctrl+C to stop following and return to the shell. The waiting view is intentional; it is different from the completed one-time output of plain tail.

tail -F also retries and follows the file name when a log is removed and created again during rotation. With several input files, head and tail label each file's output with a header such as "==> fruit.txt <==". Add -q when you need the selected text without those headers.

Key Takeaways

  • head and tail default to 10 lines; -n sets a line count.
  • tail -n +K starts at line K; it does not mean the last K lines.
  • For an inclusive range, keep the first ending-line count, then the last range-length count.
  • tail -f follows appended text; Ctrl+C stops it.
  • tail -F follows a file name through replacement, and -q suppresses multi-file headers.
2

Control where input, output and errors go

A command can produce useful results and error messages during the same run. Linux gives these separate routes, alongside a route for input. The shell can connect each route to a file before the command starts. Understanding those connections lets you save a report, keep errors in a separate file, and supply data without typing it. The order of the redirections matters, and an output file can be emptied before the program reads any input.

2.1 Understand standard input, output and error

A command communicates through standard streams. Thinking of input and output as separate routes explains how the same program can read a keyboard, read a file, or receive another command's output.

Identify the three streams

A file descriptor is the number used to identify an open stream. A normal shell command inherits these three:

NumberNameUsual terminal connection
0Standard input, stdinKeyboard input
1Standard output, stdoutNormal results on the screen
2Standard error, stderrErrors and status messages on the screen

Although stdout and stderr normally share the screen, they are separate connections. A program can report a result on stdout and explain a problem on stderr during the same run.

For example, ls /etc/hostname /nonexistent > out.txt lists one existing path and encounters one missing path. The listing goes to out.txt. The "No such file or directory" message remains on the terminal because > redirects stdout, not stderr. Seeing an error on the screen does not mean output redirection failed.

Give a command input from a file

Commands such as cat, sort, wc, grep, head, and tail can read stdin when no input file is named. Run sort alone, type a few lines, and press Ctrl+D at the beginning of an empty line. That signals the end of input, letting sort print the sorted lines. Ctrl+C stops the command instead of completing the input.

The input operator < connects a file to stdin:

wc -l < fruit.txt

If fruit.txt has six newline-terminated lines, the output is "6". Compare wc -l fruit.txt, which prints "6 fruit.txt". In the redirected form the shell opens the file, and wc receives a stream without the original file name. In the argument form, wc opens the named file itself.

The shell prepares redirections before it starts the program. sort < fruit.txt and < fruit.txt sort therefore connect the same file to the same input stream. The usual placement after the command is easier to read.

Supply a few lines directly

A here document places input below the command. The word after << marks where that input ends:

cat <<EOF > greeting.txt
Hello
See you soon
EOF

cat receives the two greeting lines on stdin and writes them to the output file. The last line contains only EOF; it is a delimiter, not part of the saved text. Another delimiter word can be used if the closing line matches it exactly.

In Bash, a here string supplies one string plus a newline. wc -w <<< "a b c" reports three words. These two forms supply input directly without first creating an input file.

Read the arrow before running the command

sort < fruit.txt reads the file. sort > fruit.txt opens it as an output destination, empties existing contents, and waits for input from the keyboard. The shell performs that destructive opening before sort runs. Keep input files separate from output destinations unless the command has a documented way to update its input safely.

Key Takeaways

  • stdin, stdout, and stderr use file descriptors 0, 1, and 2.
  • < supplies a file through stdin; > changes stdout's destination.
  • Ctrl+D on an empty input line signals end of input; Ctrl+C stops the command.
  • A here document uses a matching delimiter on a line by itself.

2.2 Redirect output and errors to files

Redirection changes a stream's destination. It is a shell operation, so the same operators work with many different programs. Start by deciding which stream to send and whether an existing file should be replaced or extended.

Save normal results or append them

echo one > log.txt creates the file or empties it before writing the line "one". A later echo two >> log.txt appends "two", leaving two lines. >> also creates a missing file.

OperatorEffect
> out.txtWrite stdout, replacing existing contents
>> out.txtAppend stdout
2> err.txtWrite stderr, replacing existing contents
2>> err.txtAppend stderr

ls /etc/hostname /nonexistent > out.txt 2> err.txt separates the successful listing from the missing-path error. The descriptor must touch the operator. ls 2> err.txt redirects errors; ls 2 > err.txt asks ls to list a file named "2" and redirects its normal output.

Combine streams in the correct order

2>&1 points stderr at stdout's current destination. Read redirections from left to right:

ls /etc/hostname /nonexistent > all.txt 2>&1

First, stdout moves to all.txt. Then stderr copies that connection. Both streams now reach the file. Bash's &> all.txt is a shorter form, and &>> all.txt appends both streams.

Reversing the order changes the result:

ls /etc/hostname /nonexistent 2>&1 > out.txt

First, stderr points where stdout currently points: the terminal. Then stdout moves to the file. Stderr keeps the earlier connection, so the error remains visible. 2>&1 makes a connection at that moment; it does not make stderr follow every later change to stdout.

Discard a stream deliberately

/dev/null discards data written to it. find / -name passwd 2>/dev/null leaves matching paths on stdout while hiding diagnostics such as "Permission denied". Those messages may identify unsearched folders, so discarding them also removes information about gaps in the search.

Protect an input file

Do not write sort fruit.txt > fruit.txt. The shell empties the destination before sort reads it. Use a different output path, check the result, and then rename it. For sort specifically, sort -o fruit.txt fruit.txt uses its supported output-file option to update that file safely.

Bash can also reject accidental overwriting:

set -o noclobber

With this setting, ordinary > fails for an existing regular file. >> still appends. Use >| for an intentional overwrite, or set +o noclobber to turn the setting off. The protection is useful when saving repeated reports, but it does not change a command's own file-writing options.

Key Takeaways

  • > overwrites, while >> appends; use descriptor 2 for stderr.
  • > all.txt 2>&1 captures both streams because redirections run left to right.
  • 2>&1 > out.txt leaves stderr at stdout's earlier destination.
  • /dev/null discards the selected stream, including useful diagnostics.
  • Avoid redirecting onto an input file; sort -o provides a supported alternative.
  • Bash's noclobber rejects ordinary > on existing regular files; >| overrides it.
3

Combine commands into pipelines

Small text commands become more useful when their inputs and outputs fit together. A pipeline can remove a header, select a column, group repeated values, and count them without creating a file after every step. Each stage changes what the next one receives. Work through the stages separately so that you can tell whether a wrong total comes from the selection, the ordering, or the counting, and save intermediate results when you need to inspect them.

3.1 Connect commands with pipes and tee

A pipe, written |, joins the normal output of one command to the input of the next. You can connect several small jobs without saving a temporary file between them.

Follow the flow of text

In ls /etc | wc -l, ls writes names and wc -l counts the newline characters it receives. A command that reads stdin, selects or changes text, and writes stdout is called a filter. sort, uniq, wc, cut, grep, head, and tail can all serve as filters.

The general shape is command1 | command2 | command3. Each stage receives the previous stage's output, which may have a different form from the original file. A later stage cannot recover lines or columns that an earlier stage removed.

Build a count in stages

Suppose fruit.txt contains six lines: "banana", "apple", "cherry", "apple", "banana", "apple". To count the different names, first run:

sort fruit.txt

The output has three "apple" lines, two "banana" lines, and one "cherry" line. Equal names are now next to one another. Add a stage:

sort fruit.txt | uniq

uniq removes adjacent repeats, leaving "apple", "banana", and "cherry". Finally, sort fruit.txt | uniq | wc -l reports 3. Counting the original file directly would report six lines, which answers a different question.

Run each growing pipeline and inspect its result before adding the next command. For very long output, temporarily add | head to see the first ten lines. This makes it easier to spot a header that was not removed or a field that was selected incorrectly.

Keep errors separate

An ordinary pipe carries stdout, not stderr. With one existing path and one missing path, ls /etc/hostname /nonexistent | wc -l counts the one normal listing line. The error still appears on the terminal.

To include diagnostics in the data, use ls /etc/hostname /nonexistent 2>&1 | wc -l, which counts two lines in this example. Bash's |& combines stdout and stderr into the pipe as well. Whether including errors is useful depends on what you want the final count to mean.

The stages run concurrently. tail -f /var/log/app.log | grep ERROR displays newly appended matching lines on the terminal. This is a live view, not a process that waits for the file to finish. Programs can buffer output, however; immediate display is not a general promise for every possible pipeline.

Save a copy along the route

tee writes its input both to a file and to stdout:

sort fruit.txt | uniq | tee unique.txt | wc -l

The terminal shows 3, while unique.txt stores the three names. tee -a appends instead of replacing the saved file. Placing tee earlier would save an earlier form of the data.

A pipe connects to a command, while > names a file. ls > less creates a file named "less"; it does not start the pager. The receiving command must also read stdin: piping text into echo or ls does not turn that text into their arguments.

Key Takeaways

  • | connects stdout to the next command's stdin.
  • Check a pipeline one stage at a time; each stage receives the previous stage's output.
  • stderr stays outside an ordinary pipe unless you combine the streams.
  • tee saves input and passes it onward; tee -a appends.
  • Piped text is stdin, not a list of command arguments.

3.2 Count, sort and remove duplicate lines

Counting and ordering are separate jobs. Before using a count, decide whether you need records, words, bytes, or distinct values. Before removing duplicates, decide whether equal lines are already next to one another.

Count the right thing with wc

Plain wc file reports lines, words, and bytes, in that order, then the file name. The six-line fruit list containing "banana", "apple", "cherry", "apple", "banana", "apple" gives "6 6 39 fruit.txt". Each line contributes its letters plus a newline byte.

OptionWhat it counts
-lNewline characters
-wWords separated by whitespace
-cBytes
-mCharacters

A final piece of text without a newline is not counted by wc -l. Text containing "one", a newline, and then "two" therefore has a count of 1. In UTF-8, "café" followed by a newline uses six bytes but five characters because "é" uses two bytes. With several input files, wc prints their counts and an additional total.

Choose text or numeric sorting

sort writes ordered lines without changing the input file. Its default order follows the current language setting. sort -f compares without distinguishing uppercase from lowercase.

Text sorting compares characters: "100", "25", "9" appear in that order because their first digits are "1", "2", "9". sort -n compares numeric values, giving 9, 25, 100. Add -r for reverse order, as in sort -rn. sort -h understands size suffixes such as "2K", "300M", and "1G".

Sort on a field

For comma-separated records, -t, sets the separator. -k3,3nr selects only field 3, compares numerically, and reverses the order:

tail -n +2 scores.csv | sort -t, -k3,3nr | head -n 3

Given "name,city,score" followed by "Ana,Lisbon,82", "Ben,Oslo,75", "Chen,Lisbon,91", "Dara,Oslo,68", and "Eli,Lisbon,88", the result is Chen, Eli, then Ana with their complete records. The first stage drops the header. Otherwise its nonnumeric score key counts as zero during numeric sorting.

Without -t, blanks separate fields. -k2 extends the key from field 2 through the line's end; -k2,2 limits it to field 2.

Group duplicates before counting them

uniq compares each line with the adjacent run. On the unsorted fruit list, it leaves all six lines because no equal names are adjacent. sort fruit.txt | uniq prints the three different names; sort -u fruit.txt can also produce a distinct sorted list.

After sorting, uniq -c reports each run's count: "3 apple", "2 banana", "1 cherry", with spaces padding the counts. uniq -d prints one copy of each repeated value, and uniq -u prints only values that occur once. uniq -i ignores case in its comparisons.

For the most frequent values first, use sort fruit.txt | uniq -c | sort -rn. The initial sort groups identical lines; the final sort ranks their counts. Omitting the first sort counts scattered runs separately.

Key Takeaways

  • wc -l counts newlines, -w words, -c bytes, and -m characters.
  • sort -n compares numbers; -r reverses order; -h handles size suffixes.
  • sort -t, -k3,3nr sorts comma-separated records by field 3 numerically in descending order.
  • Remove a header before sorting data records.
  • uniq removes adjacent repeats; sort first when equal values are scattered.
  • uniq -c counts runs, -d selects repeated values, and -u selects values occurring once.
  • sort | uniq -c | sort -rn groups values, counts them, and ranks the counts.

3.3 Extract columns with cut

A record often holds more information than a question needs. cut selects fields or fixed positions from each line and writes the selection to stdout. It does not rearrange or edit the original file.

Select fields by their separator

A delimiter separates fields. In /etc/passwd, colons separate the user name, password placeholder, user ID, group ID, full name, home folder, and login shell. For this example record:

alice:x:1001:1001:Alice Smith:/home/alice:/bin/bash

cut -d: -f1,6 prints "alice:/home/alice". Field numbers begin at 1, and the delimiter remains between selected fields. cut -d: -f5 prints "Alice Smith"; the internal space is part of that field because the delimiter is a colon.

SelectionFields printed
-f1,3Fields 1 and 3
-f2-4Fields 2 through 4
-f3-Field 3 through the last field

The list selects fields, not a new order. cut -d, -f3,1 still prints field 1 followed by field 3. By default the separator is a tab, so cut -f2 file selects the second tab-separated field.

A line with no delimiter is printed whole. For example, cut -f2 prints "no tabs here" unchanged because it has no tab. Add -s to suppress lines with no delimiter. A line containing delimiters but lacking a requested field is a different case; -s is not a general test for complete records.

Select fixed positions

cut -c1-5 selects positions 1 through 5. On the account record above, it prints "alice". Position selection suits fixed-width text, where the desired columns start at the same places on every line. Field selection suits records whose field lengths vary but whose separators stay consistent.

Check how separators behave

With cut -d' ' -f2, every single space is a separator. The text "a b" has an empty second field between its two spaces, so the result is an empty line. If repeated spaces are just padding between columns, tr -s ' ' | cut -d' ' -f2 first squeezes each run of spaces to one.

cut also treats commas inside quoted values as separators. A quoted CSV value such as "Smith, Ann" is not kept together automatically. Use simple delimiter-separated examples only when the field boundaries really match the delimiter rule.

Count values from a selected column

To count cities in scores.csv, start with cut -d, -f2 scores.csv. With the records for Ana, Ben, Chen, Dara, and Eli, this prints "city", "Lisbon", "Oslo", "Lisbon", "Oslo", "Lisbon". Then:

cut -d, -f2 scores.csv | tail -n +2 | sort | uniq -c | sort -rn

Dropping the first line removes "city". Sorting groups the three Lisbon entries and the two Oslo entries; uniq -c counts them, and the last sort places "3 Lisbon" before "2 Oslo". Forgetting the header stage creates an extra value, "1 city", because a heading is still text to these commands.

Key Takeaways

  • cut -d sets the delimiter and -f selects fields numbered from 1.
  • Field lists select fields in their original order; -c selects fixed positions.
  • The default delimiter is a tab; -s omits lines with no delimiter.
  • Repeated spaces create empty fields, and cut does not parse CSV quoting.
  • For frequency counts, remove the header, select the value, group equal lines, and count.
4

Search text with grep and regular expressions

Searching text starts with deciding what counts as a match. You may need a literal phrase, a whole word, a complete line, or a pattern such as two letters followed by four digits. grep can display the matching lines or report their numbers, counts, and file names. Regular expressions describe the patterns, while quoting keeps the shell from changing them first. You will build from simple matches to grouped alternatives and repeated parts.

4.1 Search files with grep and its main options

grep searches text and normally prints every line containing a match. A useful search starts by choosing both the pattern and the kind of result you need: lines, counts, line numbers, or file names.

Start with a quoted pattern

The usual form is grep 'pattern' file. With no file argument, grep reads stdin. Searches are case-sensitive unless you add -i.

Suppose server.log has five entries: an INFO startup line, an ERROR disk-full line, a WARN retry line, a lowercase "error timeout" line, and an INFO backup line. grep 'ERROR' server.log prints only the uppercase ERROR line. grep -i 'error' server.log prints both error lines.

Quote patterns even when they look simple. grep 'disk full' server.log searches for the two words together. Without quotes, grep disk full server.log uses "disk" as the pattern and treats "full" as another file name. Quotes also keep the shell from expanding stars and other special characters before grep receives them.

Choose the output you need
OptionResult
-iMatch without distinguishing case
-vPrint lines that do not match
-cPrint the number of matching lines
-nPrefix matches with their line numbers
-wRequire a whole-word match
-xRequire a whole-line match

Options combine: grep -ic 'error' server.log reports 2, while grep -n 'WARN' server.log identifies line 3. -c counts matching lines, not the number of occurrences inside them. A line containing "error error" contributes one to the count.

Consider the lines "error", "terror", "errors", and "Error found". Plain grep 'error' matches the first three. grep -w 'error' matches only the first because "error" is not a whole word in "terror" or "errors". Adding -i also matches "Error found". -x asks for an entire matching line, so "Error found" still fails a whole-line search for "error".

Search across files

When several files are searched, grep normally prefixes matches with file names. grep -r 'TODO' notes searches files throughout the folder tree. Add -l to print only names of files with matches: grep -rl 'TODO' notes. Uppercase -L lists files without a match. These are file-selection results, not counts of matching lines.

Decide whether the pattern is literal

By default, grep interprets a regular expression. A dot matches any single character, so '10.0.0.7' can also match "10a0b0c7". grep -F '10.0.0.7' file searches for the exact text. Escaping each dot, as in '10\.0\.0\.7', also makes the dots literal.

grep -q suppresses normal output and reports through its exit status: 0 for a match, 1 for no match, and normally 2 for an error in GNU grep. A missing input file is an error, not evidence that the pattern was absent.

In a pipeline, grep -i 'error' server.log | wc -l counts matching lines just as grep -ic does. The first form leaves room for other filters between selecting lines and counting them.

Key Takeaways

  • grep prints matching lines and is case-sensitive unless -i is used.
  • Quote the pattern so the shell passes it intact.
  • -c counts matching lines; -n numbers them; -v selects nonmatches.
  • -w requires a whole word and -x a whole line.
  • -r searches a tree, -l lists matching files, and -L lists nonmatching files.
  • -F treats a pattern as fixed text; -q reports a search through exit status.

4.2 Write basic regular expressions

A regular expression, or regex, describes text that can match. grep normally looks for a matching part anywhere in a line, then prints the whole line. Most letters match themselves; special characters describe positions, choices, or repetition.

Match a character or a position

The dot . matches one character within a line. With the lines "cat", "cart", "coat", "cut", "act", "scat", and "color", the pattern 'c.t' matches "cat", "cut", and "scat". The last contains "cat" as a matching part. "coat" has two characters between "c" and "t", so one dot cannot cover them.

Use anchors to constrain positions. ^ marks the start of a line and $ the end:

PatternWhat it requires
^cA line starting with "c"
t$A line ending with "t"
^c.t$Exactly "c", one character, then "t"
^$An empty line

grep '^c.t$' words.txt prints only "cat" and "cut". Anchoring both ends rules out a longer line such as "scat".

Choose one character from a set

A bracket expression matches one character. [ao] means either "a" or "o", so 'c[ao]t' matches the "cat" portion of "cat" and "scat". [0-9] describes a digit. A caret immediately after the opening bracket negates the set: 'c[^a]t' matches "cut" but not "cat". This caret excludes a character; it does not mark a line's beginning there.

Named classes describe categories in the current language setting. [[:digit:]] selects a digit, [[:alpha:]] a letter, [[:alnum:]] a letter or digit, and [[:upper:]] or [[:lower:]] a letter of the stated case. [[:space:]] includes spaces, tabs, and other whitespace characters. Keep both pairs of square brackets: the class name sits inside the outer set expression.

Repeat the preceding item

* means zero or more occurrences of the item immediately before it. 'ca*t' can match "ct", "cat", or "caat". It also matches "act" because that line contains "ct", using zero occurrences of "a". To match any run of characters, combine dot and star as .*.

'^c.*t$' matches "cat", "cart", "coat", and "cut" in the word list. A filename wildcard and a regex use different star rules: the shell pattern *.txt describes names ending in ".txt", but the regex for that ending is '\.txt$'.

Make a special character literal

A backslash can remove a special meaning. \. matches a real dot, \* a star, and \[ an opening square bracket. [.] also selects a literal dot. grep '\.conf$' file selects lines ending in ".conf".

A configuration search can remove simple comments and empty lines with grep -v '^#' app.conf | grep -v '^$'. The first test recognizes comments only when "#" is the first character. Default basic syntax treats unescaped ?, +, braces, |, and parentheses as ordinary characters. GNU grep supports backslashed operator forms; grep -E provides extended syntax without those backslashes.

Key Takeaways

  • grep searches for a matching part unless anchors require the whole line.
  • . matches one character; ^ and $ anchor the start and end.
  • A bracket expression selects one character; [^a] excludes "a".
  • Named classes such as [[:digit:]] require both pairs of square brackets.
  • * repeats the preceding item zero or more times; .* matches a run of characters.
  • Use \. or [.] for a literal dot.

4.3 Use extended regular expressions with grep -E

Extended regular expressions add useful ways to express optional parts, repeated groups, and alternatives. Use grep -E so the pattern is read in this syntax. The dot, anchors, sets, and star keep their earlier meanings.

Count repetitions

An operator follows the item it modifies. That item can be one character, a bracket expression, or a group:

OperatorNumber of occurrences
?Zero or one
+One or more
{n}Exactly n
{n,}At least n
{n,m}From n through m

'logs?' matches "log" and "logs": only the "s" is optional. 'ca+t' requires at least one "a", so it matches "cat" and the matching part in "scat", but not "ct" inside "act". Omitting -E changes an unescaped question mark or plus sign into literal text in basic syntax.

For an entire code of two uppercase letters and four digits, use:

grep -E '^[A-Z]{2}[0-9]{4}$' codes.txt

"AB1234" and "QZ0042" match. "A12345" has only one leading letter, "ab1234" uses lowercase, and "XY98765" has an extra digit. The two anchors prevent a valid-looking section inside a longer code from qualifying. Adding -i would also allow the lowercase code.

Group alternatives

| means an alternative inside an extended pattern. 'cat|cut' searches for either word anywhere in a line. Parentheses set the boundaries of a group: 'c(a|o)t' chooses "a" or "o" between "c" and "t". A following quantifier applies to the whole group, so '(ab)+' matches one or more copies of "ab".

Keep the pattern quoted. In grep -E cat|cut words.txt, the shell interprets the unquoted vertical bar as a pipe between commands. In grep -E 'cat|cut' words.txt, it delivers that character to grep as part of one pattern.

Print matches instead of complete lines

-o prints only matching parts, one per output line. The command below extracts a dotted-number shape from a longer line:

grep -Eo '([0-9]{1,3}\.){3}[0-9]{1,3}' server.log

The parenthesized part is one to three digits followed by a literal dot; {3} repeats it three times. The final part adds one to three digits. It extracts "10.0.0.7" from a line that contains it, but also accepts "999.999.999.999". Matching a shape does not check whether each number is within an address's valid range.

Remove blank and comment lines together

grep -Ev '^[[:space:]]*(#|$)' app.conf selects active settings. Read the pattern in order: start at the beginning, allow whitespace, then match either "#" or the end of the line. It therefore recognizes indented comments and whitespace-only lines as well as empty lines. -v keeps the lines that do not fit that description, such as "port=8080". The older command name egrep is obsolescent in GNU grep; use grep -E.

Key Takeaways

  • grep -E enables unescaped ?, +, braces, |, and parentheses as operators.
  • Quantifiers apply to the preceding character, set, or group.
  • Use anchors around a pattern when the entire record must fit it.
  • | supplies alternatives and parentheses group them; quote the pattern.
  • -o outputs each matching part, not the whole line.
  • A shape-matching regex may still accept values that are invalid in the real system.
5

Find files by name, type, size and age

A file search asks where an item is; a text search asks what is inside it. Start a file search in the right folder, then narrow it by name, type, size, or age. You can pass the matching paths to another command without breaking names that contain spaces. A current search of the directory tree and a quick database lookup have different strengths. Before applying an action to many matches, inspect the selection and understand when the action runs.

5.1 Search the directory tree with find

find walks a directory tree and tests the items it reaches. Choose the starting folder first: find project prints project itself and the entries below it. Adding tests narrows the selection.

Match names, paths, and types

find project -name '*.txt' selects names ending in ".txt" anywhere below the starting folder. Quote the wildcard so the shell passes it unchanged. -name is case-sensitive, so "Guide.TXT" needs -iname '*.txt' for a case-insensitive match.

The pattern must match the whole item's name, without its parent folders. -name 'guide' does not match "guide.txt"; -name 'guide*' does. Use -path '*/src/*' when the folder portion matters. find prints traversal order, so add | sort when you want ordered paths.

-type f selects regular files, -type d directories, and -type l symbolic links. find project -type f -name '*.txt' requires both the type and name tests to pass. A directory named "notes.txt" fails the type test even though its name fits.

Understand size comparisons

-size compares a size after rounding it up to the selected unit. c means bytes, k means 1,024-byte kibibytes, M means 1,048,576-byte mebibytes, and G means gibibytes. A leading plus means greater than, a minus means less than, and no sign means equal to the specified number of units.

For -size +100M, the rounded size must exceed 100 MiB units. A file one byte larger than 100 MiB rounds to 101 units and qualifies. A nonempty file smaller than 1 MiB rounds to one unit, so -size -1M finds only empty files. Use a byte test for an exact byte threshold, or a smaller unit for a useful small-file range.

Test age and limit depth

-mtime uses completed 24-hour periods since modification. -mtime -7 selects items modified less than seven days ago. -mtime +30 selects ages with more than 30 completed periods: at least 31 days. -mmin -30 uses minutes for recent changes. -user alice selects an owner, and -empty selects empty files or directories.

To examine the starting folder and its immediate entries without entering subfolders, use find project -maxdepth 1 -type f. Put -maxdepth before the tests.

Combine tests and preserve patterns

Tests written side by side mean AND. -o means OR, as in find project -name '*.txt' -o -name '*.md'. ! -name '*.c' negates a name test. For simple selection, keep the relationship between tests clear before adding actions.

An unquoted wildcard is expanded by the shell in your current folder. If only notes.txt matches there, find . -name *.txt becomes a search for that one name, missing other text files. With several local matches, the command may fail because extra names appear inside the test expression.

A search of protected folders may report "Permission denied". 2>/dev/null hides those errors but cannot make unreadable folders searchable. find checks the tree while running; a live file system can still change before a later command uses the returned paths.

Key Takeaways

  • find walks from a starting path and prints items passing its tests.
  • Quote name wildcards; -name tests the whole name and -path tests the full path.
  • Use -type f, d, or l for regular files, directories, or symbolic links.
  • Size tests round up before comparison; -size -1M selects only empty files.
  • -mtime +30 starts at 31 completed days, while -mmin uses minutes.
  • Adjacent tests mean AND, -o means OR, and ! negates a test.

5.2 Act on found files and use faster lookups

After selecting files, you can give their paths to another command. Keeping the selection and the action in the same find expression avoids treating spaces inside file names as separators between names.

Run a command on each match

The action -exec command {} \; runs the command once per matched item. find replaces {} with that item's path. For example:

find project -name 'main.c' -exec wc -l {} \;

If the match is project/src/main.c, this runs wc -l project/src/main.c. The escaped semicolon ends the action. The backslash keeps the shell from using the semicolon to end the entire shell command first.

Use {} + to pass batches of paths to fewer command invocations:

find notes -type f -name '*.txt' -exec grep -l 'TODO' {} +

grep receives the selected file paths as arguments and lists the ones containing "TODO". Both forms preserve a path such as "notes/team plan.txt" as one argument. The + form reduces startup work when many files match.

Combine file selection with text selection

find tests file properties; grep examines contents. Add -mtime -7 before the action to search only recently modified text files:

find notes -type f -name '*.txt' -mtime -7 -exec grep -l 'TODO' {} +

First the name, type, and age tests select files. Then grep searches their text. grep -r alone can search a tree's contents, but it does not replace those file-property tests.

Apply deletion only after checking the selection

-delete removes matches without confirmation or undo. First run the selection with no deleting action and read the paths. Only then add -delete at the end, as in find /tmp/build -type f -name '*.tmp' -delete. For a per-item confirmation prompt, -ok rm {} \; asks before running the removal command.

Actions run as the expression is evaluated. Putting -delete before -name '*.tmp' lets deletion happen before that name test can restrict it. A later test cannot protect an item from an action already taken.

Choose a faster lookup when it fits

locate guide.txt searches an index of stored paths containing "guide.txt". It can respond quickly without walking the disk. Because the database is refreshed periodically, a new file may be missing or a deleted path may remain. updatedb rebuilds the index; a system-wide update commonly needs administrator rights. Some distributions require an optional package such as plocate.

which grep looks through the folders in PATH for an executable program file, often reporting /usr/bin/grep. It does not describe every way Bash resolves a command. type echo, for example, can identify a built-in even when an external program named "echo" also exists. Use type commandname for Bash's interpretation, find for a current tree search, and locate for a quick indexed name lookup.

Key Takeaways

  • -exec command {} \; runs once per path; {} + batches paths.
  • Use find for file properties and grep for contents, combining them when needed.
  • Preview deletion matches, then put -delete after the restricting tests.
  • locate searches an index that can be out of date.
  • which finds executable files in PATH; type explains Bash command resolution.
6

Edit text files in nano and vi

Terminal editors let you change notes and configuration files during a remote session or on a system without a graphical desktop. nano lets you type immediately and shows shortcuts at the bottom of the screen. vi assigns different jobs to keys in different modes, so changing modes is part of editing. Practice opening, finding text, making a small change, and saving or leaving deliberately; these steps matter as much as knowing a delete command.

6.1 Edit and save a file in nano

nano notes.txt opens text in a terminal editor. You can move with arrow keys and type directly into the text. If the file does not exist, nano starts an empty buffer, the working text held in memory; saving creates the file on disk.

Read the shortcut labels

The bottom lines show key combinations. ^ means Ctrl, so ^X means Ctrl+X. M- means Meta, usually Alt, so M-U means Alt+U. Ctrl+G opens help. These labels describe keys to press together, not characters to type into the file.

Make and save a small change

To change "port=8080" to "port=9090" in app.conf:

  1. Run nano app.conf.
  2. Press Ctrl+W, enter "8080", and press Enter.
  3. At the match, delete the four digits and type "9090".
  4. Press Ctrl+O, then Enter to confirm the file name and save.
  5. Press Ctrl+X to leave.

Back at the shell, grep 'port' app.conf should show "port=9090". This checks the saved file, not just what was visible in the editor.

Ctrl+O is Write Out, not an open-file command. Ctrl+R reads another file's contents into the current buffer at the cursor. Confusing those actions can insert unwanted text into the file you are editing.

Search, replace, and move

Ctrl+W searches for text. Alt+G goes to a requested line number, useful when an error message identifies a particular line. Ctrl+C shows the current line and column.

Alt+R, also available as Ctrl+, starts replacement. Enter the text to find and then its replacement. For each match, answer Y to replace or N to skip; A replaces all. Choosing individual replacements helps when the same digits appear in several settings and only one should change.

Move text and undo changes
ShortcutAction
Ctrl+KCut the current whole line
Ctrl+UPaste cut text at the cursor
Alt+AStart marking a selection
Alt+U / Alt+EUndo / redo

Repeated Ctrl+K presses collect consecutive lines. With a marked selection, Ctrl+K cuts the marked text instead of the whole line. Ctrl+U then inserts that text where the cursor is placed. Undo is useful immediately after an accidental cut or replacement.

Leave deliberately

Ctrl+X exits. If the buffer has unsaved changes, nano asks whether to save it. Y saves, followed by Enter to confirm the name; N leaves without saving those changes; Ctrl+C cancels the exit and returns to editing. Answering N is a decision to discard work, not a way to continue editing.

nano -l app.conf shows line numbers, and nano +12 app.conf starts at line 12. If saving fails because you lack permission, the edited buffer has not been saved to that path. System files require appropriate rights, for example sudo nano /etc/hosts when an authorized system change needs administrator access. For practice, use a file in your own folder.

Key Takeaways

  • nano accepts typed text immediately; a new file is created when saved.
  • ^ means Ctrl and M- usually means Alt in shortcut labels.
  • Ctrl+O and Enter save; Ctrl+X exits and asks about unsaved changes.
  • Ctrl+W searches, Alt+R replaces, and Alt+G goes to a line.
  • Ctrl+K cuts, Ctrl+U pastes, and Alt+U undoes.

6.2 Work in vi's modes

vi notes.txt opens a terminal editor in normal mode. A key such as x is a command there. To type text, first enter insert mode. On many Linux systems, vi starts Vim or another vi-compatible editor.

Keep track of the mode
ModeJobHow to enter
NormalMove, delete, copy, and issue commandsEsc
InsertType text into the bufferi, a, o, or another insert command
Command-lineEnter commands such as saving: from normal mode

Vim shows "-- INSERT --" while inserting. Esc returns to normal mode. From normal mode, i inserts before the cursor and a after it. I starts before the line's first nonblank character, preserving indentation; A appends at the end. o opens a line below and O a line above.

Move without typing text

In normal mode, h, j, k, and l move left, down, up, and right. Arrow keys also work. w moves to the next word, 0 to the line's beginning, and $ to its end. G jumps to the last line. 1G jumps to the first; Vim also offers gg. A number before G selects a line, so 12G jumps to line 12. :12 and Enter does the same job. Counts also repeat movements: 5j moves down five lines.

Edit from normal mode

x deletes the character under the cursor. dd cuts a line; 3dd cuts three lines. yy copies a line. For whole-line text, p pastes below the current line and P above it. For character text, p pastes after the cursor and P before it.

cw replaces the rest of the current word and enters insert mode. r followed by a character replaces just one character. u undoes a change; Vim's Ctrl+R redoes it. A dot . repeats the last change.

Find and change a setting

To replace "8080" with "9090" in app.conf, run vi app.conf, then type /8080 and Enter. At the first "8", type cw, type "9090", and press Esc. Type :wq and Enter to save and leave.

Search backward with ?text and Enter. n repeats the current search direction and N reverses it. Searches accept regular expressions. To replace every occurrence throughout a file, :%s/8080/9090/g uses % for all lines and g for all occurrences on each line. :set number shows line numbers while editing.

Save or quit on purpose

From normal mode, :w and Enter saves; :w newname.txt saves under a new name. :q quits only if no unsaved changes prevent it. :wq, :x, or ZZ saves and quits. :q! discards unsaved changes and quits.

If typing unexpectedly moves the cursor or deletes text, press Esc to establish normal mode, then use u to undo unwanted edits before continuing. Enter insert mode before typing the replacement text. When leaving an editing attempt, choose whether to save or discard; the exclamation mark in :q! authorizes discarding unsaved work.

Key Takeaways

  • vi starts in normal mode; enter insert mode to type and press Esc to return.
  • i, a, o, and their uppercase forms choose where insertion begins.
  • dd cuts a line, yy copies it, and p puts whole-line text below or character text after the cursor.
  • u undoes, and . repeats the last change.
  • /text searches forward; n repeats and N reverses the search direction.
  • :wq saves and quits; :q! discards unsaved changes and quits.

Linux Text Processing examples

6 Walkthroughs

1

Read a file without losing your place

10 steps
Choose a viewing command, navigate a pager, and select a precise part of a file.
  1. 1
    IntroductionChoose how much text to see

    A terminal can show an entire file, a screen at a time, or just a selected part. The choice changes how easily you can find what matters.

    Use a short note to learn cat, then move to less for a file too long to keep on screen. Finally, use head and tail when you know the part you need. These commands read text without editing the stored file.

  2. 2
    ExampleJoin two short notes

    a.txt contains "a" and "b" on separate lines. b.txt contains "c d". Run cat -n a.txt b.txt.

    The output has three numbered lines, in that order. Numbering continues from the first file into the second; the numbers are added to the output only. To save the original joined text without numbering, run cat a.txt b.txt > ab.txt.

  3. 3
    ExplanationOpen a reading view

    Run less /etc/services to read a long file one screen at a time. The program keeps the view open and waits for navigation keys.

    Space goes forward a screen, b goes back, and arrow keys move through lines. g jumps to the first line; G to the last. Press q to close the view and return to the shell. A missing shell prompt is normal while the pager is open.

  4. 4
    ExampleSearch and continue

    In less, type /ssh and press Enter to search forward for "ssh". Press n for the next match in that direction or N to go the opposite way.

    A backward search starts with ?ssh. After that search, n continues backward. The direction comes from the search you started. less -N file adds line numbers if you need to note the location of a match.

  5. 5
    ChallengeFind the earlier match

    You have searched forward for "listen" in a long file with less. You want the previous match, then want to leave the pager. What keys do you press?

    ---reveal---

    Press N to move opposite to the forward search direction, then q to quit. Lowercase n would continue forward. Quitting is what restores the shell prompt; reaching a match or the end of the file does not close the pager.

  6. 6
    ExplanationChoose a count or a starting line

    head -n 4 file prints the first four lines. tail -n 4 file prints the last four. Without -n, each defaults to ten lines.

    A plus sign changes tail’s job: tail -n +4 file begins at line 4 and prints everything after it. For a file with one header line, tail -n +2 file removes that header. It keeps all data records, not just the final two.

  7. 7
    ExampleSelect lines 11 through 20

    Suppose a file contains the numbers 1 through 100, one per line. Start with head -n 20 numbers.txt, which keeps 1 through 20.

    Add | tail -n 10. The last ten lines of the selected input are 11 through 20. Both endpoints count, so this range contains ten lines. Asking tail for eleven lines would include line 10 as well, shifting the start backward.

  8. 8
    ChallengeChoose another middle range

    A file has at least 17 lines. Write a pipeline using head and tail that prints lines 12 through 17, including both ends.

    ---reveal---

    Use head -n 17 file | tail -n 6. First keep lines 1–17, then retain the last six of those: 12, 13, 14, 15, 16, and 17. Using a count of five would omit line 12 because the endpoints are included in the requested range.

  9. 9
    ChallengeInspect the latest text and keep watching

    You need to see the latest entries of a long application file and continue watching as new entries arrive. Which command fits, and how do you leave it?

    ---reveal---

    Use tail -f app.log, then Ctrl+C when finished. Plain tail would show the current ending and exit. tail -F is useful when the program replaces the file during rotation, because it follows the file name through replacement.

  10. 10
    RecapUse the view that fits the task

    For a whole short file, use cat; for navigation and searching, open less. A pager remains open until you quit it.

    Use head and tail for the beginning, ending, or a middle range. Read a plus sign in tail’s line count as a starting position. For a live view, follow appended text with tail -f and stop with Ctrl+C. The selection you make also works when the text arrives through a pipe.

2

Trace input, results, and errors

11 steps
Connect files to standard streams and follow the order of shell redirections.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionDraw three separate routes
  2. 2
    ExplanationName the standard streams
  3. 3
    ExampleFeed the same data through another route
  4. 4
    ChallengeComplete keyboard input
  5. 5
    ExampleSupply several lines below a command
  6. 6
    ExplanationChoose replacement or addition
  7. 7
    ExampleCombine results and diagnostics
  8. 8
    ChallengeReverse the connections
  9. 9
    TipKeep the input intact
  10. 10
    ChallengeCount input and separate a diagnostic
  11. 11
    RecapTrace the shell’s setup
3

Build a useful text report

14 steps
Inspect a pipeline as it selects a column, groups values, counts them, and saves results.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionGive each stage one job
  2. 2
    ExplanationConnect the two commands
  3. 3
    ChallengeChoose a pipe or a file
  4. 4
    ExplanationKnow what the counter measures
  5. 5
    ExampleSeparate sorting from counting
  6. 6
    ChallengeExplain a duplicate that survives
  7. 7
    ExplanationSelect the column that matters
  8. 8
    ExampleRemove the label and inspect the values
  9. 9
    ChallengeCheck a separator assumption
  10. 10
    ExampleGroup the selected values
  11. 11
    ExampleRank the counts
  12. 12
    ConnectionKeep evidence before the final count
  13. 13
    ChallengeDiagnose an extra category
  14. 14
    RecapCheck what travels between stages
4

Build patterns that match the intended text

15 steps
Choose grep options, then build basic and extended patterns one operator at a time.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionDecide what counts as a match
  2. 2
    ExplanationChoose a search report
  3. 3
    ExampleDistinguish a word from a line
  4. 4
    ChallengeSearch for an exact version label
  5. 5
    TipSearch a folder or ask a quiet question
  6. 6
    ExplanationPlace a match within the line
  7. 7
    ExampleSelect one character from a set
  8. 8
    Key InsightAllow zero copies
  9. 9
    ChallengeMatch a literal filename ending
  10. 10
    ExplanationTurn on extended operators
  11. 11
    ExampleCheck an entire code
  12. 12
    ExampleGroup a choice or a repeated part
  13. 13
    ChallengeExtract parts and inspect their limits
  14. 14
    ChallengeKeep only active settings
  15. 15
    RecapCheck the pattern and the report separately
5

Select files before acting on them

12 steps
Choose a search root, apply file tests, and pass complete paths to the next command.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionSeparate names from contents
  2. 2
    ExplanationChoose the starting folder
  3. 3
    ExampleCombine type with name
  4. 4
    ChallengeKeep the wildcard for find
  5. 5
    ExplanationCompare rounded size units
  6. 6
    ChallengeRead the day boundary
  7. 7
    ExplanationPass paths as arguments
  8. 8
    ExampleSearch only selected contents
  9. 9
    TipPut a destructive action after its tests
  10. 10
    ExplanationChoose an index or a command lookup
  11. 11
    ChallengeChoose a current content check
  12. 12
    RecapMatch the search to the evidence you need
6

Make and save a small terminal edit

13 steps
Edit a setting in nano and vi, then choose whether to save or discard the working changes.

Save this lesson to follow every Walkthrough step by step.

  1. 1
    IntroductionDistinguish working text from saved text
  2. 2
    ExplanationRead nano’s controls
  3. 3
    ExampleFind and replace one value
  4. 4
    ChallengeMove a line and recover from a mistake
  5. 5
    TipHandle the exit prompt
  6. 6
    ExplanationEnter vi’s typing mode
  7. 7
    ExampleMove to the text you need
  8. 8
    ExampleChange the setting in vi
  9. 9
    ChallengePaste the kind of text you copied
  10. 10
    TipUse small edits and undo
  11. 11
    ChallengeChoose whether the vi edit should survive
  12. 12
    ChallengeCompare two unfinished edits
  13. 13
    RecapFinish with a deliberate save or discard

Linux Text Processing practice questions

50 Practice Questions in 5 sets

Set 1: Read text and follow its flow10 Practice Questions

Question 1 of 10

The tree contains a regular file docs/a.txt, a directory docs/b.txt, and a regular file docs/c.TXT. What does find docs -type f -name '*.txt' select?
  1. Only docs/a.txt.
  2. Both docs/a.txt and docs/b.txt.
  3. Both docs/a.txt and docs/c.TXT.
  4. All three paths under docs.
Show answerHide answer
Answer: A
Both tests must pass: the item must be a regular file and its name must match the case-sensitive suffix. Only a.txt meets both. b.txt fails the type test, while c.TXT would need the case-insensitive -iname test to qualify under the same suffix pattern.

Question 2 of 10

You have finished editing in nano. What key sequence saves the current buffer under the displayed name?
  1. Ctrl+R, then Enter.
  2. Ctrl+K, then Ctrl+U.
  3. Ctrl+O, then Enter.
  4. Ctrl+X, then N.
Show answerHide answer
Answer: C
Ctrl+O starts Write Out and Enter confirms the displayed file name, writing the current buffer to disk. Ctrl+R reads another file’s text into the buffer instead. Ctrl+X followed by N exits without saving unsaved changes, so it would discard the work you want to keep.

Question 3 of 10

You are reading a long file in less. What single key returns you to the shell?
  1. g
  2. h
  3. q
  4. n
Show answerHide answer
Answer: C
Press q to quit the pager and return to the shell prompt. g moves to the beginning of the file but keeps the pager open, so it cannot restore the shell prompt. The absence of that prompt while reading means less is still handling your keys.

Question 4 of 10

What does tail -n +3 records.txt print?
  1. Every line from line 3 onward.
  2. Only the final three lines.
  3. Only line 3 of the file.
  4. Every line before line 3.
Show answerHide answer
Answer: A
The plus sign makes 3 a starting position, so the command prints line 3 and all following lines. Without the plus sign, tail -n 3 would select the final three lines. Removing two opening lines and selecting three ending lines are different operations.

Question 5 of 10

A command’s normal results are saved with > report.txt, but its diagnostics remain visible. Which stream carries those diagnostics?
  1. Standard input, descriptor 0.
  2. Standard output, descriptor 1.
  3. Standard output, descriptor 2.
  4. Standard error, descriptor 2.
Show answerHide answer
Answer: D
Diagnostics normally travel on stderr, numbered 2, separately from normal results on stdout, numbered 1. The > operator redirects stdout alone, which explains why errors can remain on the screen. Sharing the terminal by default does not make the two output streams the same stream.

Question 6 of 10

You want to keep an existing report and add a command’s stdout at its end. What redirection does that?
  1. > report.txt
  2. >> report.txt
  3. 2> report.txt
  4. < report.txt
Show answerHide answer
Answer: B
The >> operator appends stdout, preserving the file’s existing text and creating the file if needed. A single > opens the file after emptying its old contents. 2> redirects diagnostics instead of normal results, so it also selects the wrong stream for this task.

Question 7 of 10

A command prints five normal result lines and two diagnostic lines. What line count does command | wc -l report?
  1. 7
  2. 2
  3. 5
  4. 0
Show answerHide answer
Answer: C
The pipe carries the five stdout lines into wc -l, giving a count of 5. The two stderr lines retain their separate route and do not enter the counter. A count of 7 would require combining stderr with stdout before the pipe, which this command line does not do.

Question 8 of 10

A file contains "18", "4", and "105", each on its own line. What order does sort -n produce?
  1. 105, 18, 4
  2. 18, 105, 4
  3. 4, 105, 18
  4. 4, 18, 105
Show answerHide answer
Answer: D
Numeric sorting compares the values, so 4 comes before 18 and 105. Plain text sorting would start with 105 because its first character is "1", then place 18 before 4. The -n option changes the comparison; it does not merely reverse the default text order.

Question 9 of 10

supplies.txt contains "clip", "ring", "clip", "pad", "clip", "ring", and "clip", each newline-terminated. What two rows lead sort supplies.txt | uniq -c | sort -rn | head -n 2, ignoring count padding?
  1. "4 clip" then "2 ring".
  2. "4 clip" then "1 pad".
  3. "4 ring" then "2 clip".
  4. "1 clip" then "1 ring".
Show answerHide answer
Answer: A
The first sort groups four clip lines, two ring lines, and one pad line. uniq counts those groups, then reverse numeric sorting ranks the counts and head keeps the first two. The single pad line is not second because the final ordering follows frequency, not the names’ text order.

Question 10 of 10

A file has the lines "FAIL", "fail", "failure", and "all clear". How many lines does grep -ic 'fail' file count?
  1. 1
  2. 3
  3. 2
  4. 4
Show answerHide answer
Answer: B
Ignoring case lets both "FAIL" and "fail" match, and grep also finds the same character sequence inside "failure". That makes three matching lines. A count of 2 would require a whole-word restriction such as -w, which this command does not include.
Set 2: Choose commands and interpret matches10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

After searching backward with ?queue in less, what does n do?
  1. Finds the next match forward.
  2. Finds the next match backward.
  3. Moves to the next screen forward.
  4. Moves to the first line of the file.
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

labels.txt has eight newline-terminated lines. How does the output of wc -l < labels.txt differ from wc -l labels.txt?
  1. It omits the count but keeps the file name.
  2. It prints a different count without a name.
  3. It adds a total line after the same count.
  4. It omits the file name but keeps the count 8.
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

In a fresh terminal, a command writes results to stdout and diagnostics to stderr. Where does each go in command 2>&1 > result.txt?
  1. Results to the file; diagnostics to the terminal.
  2. Results to the terminal; diagnostics to the file.
  3. Both streams to the file; neither to the terminal.
  4. Both streams to the terminal; neither to the file.
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

teams.csv contains "name,team", "Uma,Blue", "Zed,Gold", "Ivo,Blue", and "Moe,Gold", with final newlines. What does cut -d, -f2 teams.csv | sort | uniq | wc -l report?
  1. 2
  2. 4
  3. 3
  4. 5
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

What command searches build.txt for the literal text "v3.8", including the real dot?
  1. grep -F 'v3.8' build.txt
  2. grep -E 'v3.8' build.txt
  3. grep -i 'v3.8' build.txt
  4. grep -w 'v3.8' build.txt
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

The lines are "dog", "dig", "drag", and "undog". What lines match grep '^d.g$' file?
  1. All but "drag".
  2. "dog" and "dig".
  3. All but "undog".
  4. All but "dig".
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

What lines match grep '^ba*t$' file among "bt", "bat", "baat", and "boat"?
  1. "bat" and "baat" only.
  2. "bat", "baat", and "boat".
  3. "bt", "bat", and "boat".
  4. "bt", "bat", and "baat".
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

A notes tree contains file names with spaces. You need the names of recently modified .txt files containing "REVIEW". What command applies all three restrictions?
  1. find notes -type f -name '*.txt' -mtime -7 | grep -l 'REVIEW'
  2. find notes -type f -name '*.md' -mtime -7 -exec grep -l 'REVIEW' {} +
  3. find notes -type f -name '*.txt' -mtime -7 -exec grep -l 'REVIEW' {} +
  4. find notes -type f -name '*.txt' -mtime +7 -exec grep -l 'REVIEW' {} +
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

At nano’s "Save modified buffer?" exit prompt, what key returns to editing without leaving the editor?
  1. N
  2. Ctrl+C
  3. Y
  4. Ctrl+O
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

In vi normal mode, you copy a line with yy and press p. Where does the copied line appear?
  1. Above the current line.
  2. After the cursor within the line.
  3. Before the cursor within the line.
  4. Below the current line.
Show answerHide answer

Save this lesson to see this answer.

Set 3: Trace pipelines and correct mistakes10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

runs.txt contains "b", "a", "b", "a", and "a", each on its own line. What leading count-value pair does uniq -c runs.txt | sort -rn produce?
  1. 2 a
  2. 3 a
  3. 2 b
  4. 1 b
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

What does cut -d: -f3,1 print from the line "Nia:ops:42"?
  1. "42:Nia"
  2. "Nia:42"
  3. "Nia:ops"
  4. "42:ops"
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

What complete lines match grep -E '^(go)+$' file?
  1. "go" and "gogo".
  2. "go" and "goo".
  3. "g" and "go".
  4. "go" and "gog".
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

A file has at least 28 lines. What command displays exactly lines 19 through 28?
  1. head -n 28 file | tail -n 9
  2. head -n 19 file | tail -n 10
  3. head -n 28 file | tail -n +19 | head -n 9
  4. head -n 28 file | tail -n 10
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

What happens to existing contents before echo done > state.txt writes its output?
  1. They remain before the new line at the end.
  2. They are removed when the shell opens the file.
  3. They move to stderr before the new line is written.
  4. They stay unless echo reports a successful write.
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

parts.txt contains "pin", "bolt", "pin", "nut", and "bolt", each newline-terminated. What does sort parts.txt | uniq | tee distinct.txt | wc -l do?
  1. Prints 5; saves all five original lines.
  2. Prints 3; saves only the count itself.
  3. Prints 3; saves three distinct names.
  4. Prints 5; saves three distinct names.
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

A line contains "node=7 node=24 node=301". What output does grep -Eo '[0-9]{1,2}' produce from that line?
  1. "7" and "24", on separate lines.
  2. "7", "24", "301", on separate lines.
  3. "7", "24", "30", "1", on separate lines.
  4. "7", "2", "4", "3", "0", "1", on separate lines.
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

Which of the sizes 0 bytes, 1 byte, 800,000 bytes, and 1,048,576 bytes passes GNU find’s -size -1M test?
  1. Up to 1 byte.
  2. All below 1 MiB.
  3. All four sizes.
  4. Only 0 bytes.
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

Why might locate plan.txt show a path that was deleted this morning?
  1. It reads deleted file contents from standard input.
  2. Its name test ignores every directory’s current state.
  3. Its path database has not yet reflected the deletion.
  4. It searches PATH before checking the requested name.
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

You are in vi insert mode with changes you want to keep. What sequence saves and leaves?
  1. Esc, :wq, Enter.
  2. Esc, :q!, Enter.
  3. Esc, :q, Enter.
  4. Esc, wq, Enter.
Show answerHide answer

Save this lesson to see this answer.

Set 4: Diagnose file and text workflows10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

You want to save first.txt followed by second.txt in joined.txt, with output line numbers continuing across both input files. What command does this?
  1. cat -n first.txt second.txt > joined.txt
  2. cat first.txt second.txt > joined.txt
  3. cat -n second.txt first.txt > joined.txt
  4. cat -n first.txt second.txt | wc -l > joined.txt
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

ls -lt produces one "total" heading followed by file entries. How many entries does ls -lt | head -n 6 display when at least six entries exist?
  1. 6
  2. 4
  3. 5
  4. 7
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

When entering lines into sort interactively, what signals end of input at the start of an empty line?
  1. Ctrl+C
  2. Ctrl+D
  3. Ctrl+O
  4. Ctrl+W
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

A command emits four stdout lines and one stderr line. What does command 2>&1 | tee combined.txt | wc -l produce?
  1. Prints 4; saves only normal results.
  2. Prints 5; saves only the final count.
  3. Prints 4; saves results and diagnostics.
  4. Prints 5; saves results and diagnostics.
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

The input line is "A B C", with two spaces after A. What does cut -d' ' -f2 print?
  1. "B"
  2. "C"
  3. An empty line.
  4. "A"
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

A file contains "alert alert", "prealert", "Alert raised", and "all clear". What does grep -icw 'alert' file report?
  1. 2
  2. 1
  3. 3
  4. 4
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

A list contains "x.cfg", "xXcfg", "x.cfg.old", and "cfg". What pattern selects only lines ending with the literal suffix ".cfg"?
  1. .cfg$
  2. \.cfg
  3. ^\.cfg$
  4. \.cfg$
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

A tree has regular files "old.cfg" (30.9 days old), "notes.txt" (31.1 days), and "runtime.cfg" (31.1 days), plus a directory "archive.cfg" (31.1 days). What does find . -type f -name '*.cfg' -mtime +30 select?
  1. Both ".cfg" files.
  2. Only "runtime.cfg".
  3. Every 31.1-day file.
  4. Every 31.1-day ".cfg".
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

In nano you cut a line accidentally and want it restored by reversing that action. What shortcut does that?
  1. Alt+U
  2. Ctrl+U
  3. Ctrl+K
  4. Alt+A
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

A vi line begins with four spaces before "enabled=yes". From normal mode, where does I begin insertion?
  1. Before all four spaces, at column one.
  2. Before "enabled", after the four spaces.
  3. After "enabled=yes", at the line’s end.
  4. On a new blank line above the setting.
Show answerHide answer

Save this lesson to see this answer.

Set 5: Combine rules in unfamiliar cases10 Practice Questions

Save this lesson to see every answer and practice every set.

Question 1 of 10

You need regular files immediately inside work, including no files from deeper folders. What search does that?
  1. find work -maxdepth 1 -type d
  2. find work -maxdepth 1 -type f
  3. find work -maxdepth 2 -type f
  4. find work -maxdepth 0 -type f
Show answerHide answer

Save this lesson to see this answer.

Question 2 of 10

In vi normal mode, x deletes one character. You then press p. Where is that character inserted?
  1. Before the cursor.
  2. Below the current line.
  3. After the cursor.
  4. Above the current line.
Show answerHide answer

Save this lesson to see this answer.

Question 3 of 10

You want a Bash command’s interpretation, including a possible alias or built-in. What command should you use for the name echo?
  1. which echo
  2. locate echo
  3. find . -name echo
  4. type echo
Show answerHide answer

Save this lesson to see this answer.

Question 4 of 10

A data file has a one-line header followed by 30 records. What command displays original file lines 14 through 18 after removing the header?
  1. tail -n +2 file | head -n 18 | tail -n 5
  2. tail -n +2 file | head -n 17 | tail -n 4
  3. tail -n +2 file | head -n 17 | tail -n 5
  4. tail -n +2 file | head -n 14 | tail -n 5
Show answerHide answer

Save this lesson to see this answer.

Question 5 of 10

A command writes two normal lines and one diagnostic line. Both log.txt and err.txt already contain older text. What happens with command >> log.txt 2> err.txt?
  1. Normal lines replace log; the diagnostic appends to err.
  2. Normal lines append to log; the diagnostic replaces err.
  3. Both streams append to log; err keeps its older text.
  4. Normal lines append to err; the diagnostic replaces log.
Show answerHide answer

Save this lesson to see this answer.

Question 6 of 10

sizes.csv contains "name,size", "alpha,8", "beta,110", and "gamma,24". What pipeline prints the two complete data records with the greatest numeric sizes first?
  1. tail -n +2 sizes.csv | sort -t, -k2,2r | head -n 2
  2. tail -n +2 sizes.csv | sort -t, -k2,2n | head -n 2
  3. tail -n +2 sizes.csv | sort -t, -k1,1nr | head -n 2
  4. tail -n +2 sizes.csv | sort -t, -k2,2nr | head -n 2
Show answerHide answer

Save this lesson to see this answer.

Question 7 of 10

jobs.csv contains "id,zone", "1,North", "2,South", "3,North", "4,East", and "5,North". What first count-value pair results from tail -n +2 jobs.csv | cut -d, -f2 | sort | uniq -c | sort -rn?
  1. 3 North
  2. 1 North
  3. 2 North
  4. 1 zone
Show answerHide answer

Save this lesson to see this answer.

Question 8 of 10

What short option makes grep display input line numbers with matching lines?
  1. -c
  2. -n
  3. -l
  4. -o
Show answerHide answer

Save this lesson to see this answer.

Question 9 of 10

What line matches grep '^r[^ae]d$' file among "rad", "red", "rid", and "road"?
  1. "rad"
  2. "red"
  3. "rid"
  4. "road"
Show answerHide answer

Save this lesson to see this answer.

Question 10 of 10

A configuration file contains " # memo", three spaces, "mode=fast", and "x#y=on", each as a separate line. What survives grep -Ev '^[[:space:]]*(#|$)' file?
  1. "mode=fast" and "x#y=on".
  2. "mode=fast" only.
  3. " # memo" and "mode=fast".
  4. Three spaces and "x#y=on".
Show answerHide answer

Save this lesson to see this answer.

Stuck on your own question? Homework Help

Key Terms

72 Key Terms, each with its definition

A

Alternation
Alternation expresses a choice between regex patterns using | in extended syntax, often with parentheses to limit the choice’s scope.
In ^(east|west)$, both alternatives must fill the whole line because they share the anchors.
Anchor
An anchor restricts a regex match to a position, such as ^ for the start or $ for the end of a line in ordinary grep searches.
Adding both anchors to d.g excludes a longer line such as "undog" even though it contains "dog".
Append
To append is to add output at the end of a file while preserving its existing contents, using >> for a command’s standard output.
Repeated commands using >> report.txt collect their results in one file instead of replacing it.

B

Bracket expression
A bracket expression selects one character from a set or range, with a leading caret inside the set reversing which characters qualify.
The pattern b[ae]t matches "bat" or "bet", while b[^a]t excludes "a" from the middle position.
Buffer
A buffer is an editor’s working copy of text in memory, which may contain changes that have not yet been written to the file on disk.
If nano reports a permission error during saving, the edited buffer can differ from the stored file.

C

cat
cat is a command that prints the contents of one or more files to standard output, following the order of its file arguments.
Running cat -n a.txt b.txt numbers their combined output without editing either input file.
Character class
A character class is a named group such as digits, letters, or whitespace, used inside a bracket expression and tied to language settings.
The complete expression [[:digit:]] selects a digit; both pairs of square brackets are needed.
Command-line mode
Command-line mode accepts a command at vi’s bottom line after : is typed in normal mode, running it when Enter is pressed.
The command :wq saves the buffer and quits after Enter; a plain wq in normal mode does not do this.
Concatenation
Concatenation joins text sequences in order, such as printing the contents of several files one after another with cat.
With cat first.txt second.txt > combined.txt, text from the first file precedes text from the second.
cut
cut is a filter that selects fields or fixed character positions from each input line, preserving the selected parts in their original order.
The command cut -d: -f1,6 /etc/passwd pairs each account name with its home folder field.

D

Delimiter
A delimiter is a separator between fields, such as a colon, comma, or tab; cut -d selects the character used to split each line.
For the record "Mia:ops:41", a colon delimiter makes "ops" field 2 regardless of word length.
Descriptor duplication
Descriptor duplication connects one numbered stream to another stream’s current destination, as 2>&1 does for stderr and stdout.
In command > all.txt 2>&1, stderr receives the file connection that stdout already has.

E

Empty field
An empty field has no characters between its separators, so consecutive delimiters can produce a field that looks like a missing value.
With a space delimiter, "a b" has an empty second field between its two spaces.
Escaping
Escaping uses a backslash to change how a special character is read, such as making a regex dot match a real period instead of any character.
The pattern \.txt$ requires a literal ".txt" ending, while .txt$ permits another character before "txt".
Exit status
An exit status is a number a command returns to the shell, such as grep’s usual 0 for a match, 1 for no match, or 2 for an error.
A quiet search with grep -q uses its status to report a result without printing matching lines.
Extended regular expression
An extended regular expression gives operators such as ?, +, |, braces, and parentheses their special meanings without backslashes.
Use grep -E before a grouped pattern such as ^(red|blue)$ to enable the alternative.

F

Field
A field is one part of a separated record, numbered from 1 by cut and selected according to the delimiter used in that record.
In "Lena,Oslo,74", field 2 is "Oslo" when a comma is the chosen separator for the line.
File descriptor
A file descriptor is a number identifying an open input or output connection, including 0 for stdin, 1 for stdout, and 2 for stderr.
The 2 in 2> errors.txt selects the error stream and must touch the redirection operator.
Filter
A filter reads data from standard input, selects or changes it, and writes a result to standard output so it can work in a pipeline.
sort, cut, and grep each act as filters when they receive text from an earlier command.
find
find searches a directory tree from specified starting paths, tests each reached item’s properties, and prints or acts on selected paths.
find notes -type f -name '*.txt' selects regular text-named files throughout that folder tree.
find -exec
find -exec runs another command on selected paths, substituting {} and using an escaped semicolon or plus sign to end the action.
The action -exec wc -l {} + passes batches of matching paths to a command that counts their lines.
Fixed-string search
A fixed-string search treats pattern characters as literal text, so dots and stars lose their regex meanings when grep uses -F.
Use grep -F "v2.4" releases.txt to require the actual dot in the version label.
Fixed-width text
Fixed-width text places information at consistent character positions on each line, allowing a position selection instead of delimiter splitting.
A five-character code at every line’s start can be selected with cut -c1-5 even if later text varies.
Follow mode
Follow mode keeps tail waiting for appended data after its initial output, so new text can appear while a program writes to the file.
Run tail -f app.log during an application check, then press Ctrl+C when you finish watching.
Frequency count
A frequency count reports how often each value occurs, commonly built by grouping equal lines with sort and counting runs with uniq -c.
Add sort -rn after uniq -c to put the largest counts first when inspecting common entries.

G

grep
grep is a text-search command that normally prints each input line containing a match for a pattern, with options to change the report.
Use grep -n WARN app.log to show matching lines together with their positions in the file.
Group
A regex group puts a sequence inside parentheses so an operator can act on it as one item or alternatives can share their surrounding text.
The expression (ab)+ permits "ab" and "abab" because the plus repeats the full two-character group.

H

head
head is a filter that prints the beginning of its input, selecting ten lines by default or a different line or byte count when requested.
Inspect a file’s opening with head -n 4 notes.txt before deciding whether you need the whole file.
Here document
A here document supplies multiple lines as standard input, ending when the shell reaches a line containing only the chosen delimiter.
With cat <<EOF, the lines entered before the closing EOF are data, while that final line is a marker.
Here string
A here string is a Bash input redirection that supplies a single string followed by a newline to a command through standard input.
The command wc -w <<< "red blue green" receives three words without needing a separate input file.

I

Insert mode
Insert mode makes typed characters become text in vi’s buffer, starting through commands such as i, a, or o and ending with Esc.
After cw removes the rest of a word, type its replacement in insert mode and then press Esc.

L

less
less is a pager that lets you move forward and backward, search text, and view files or piped output without editing the text.
Use less -N notes.txt when you want line numbers while locating a particular passage in a file.
Line range
A line range is a consecutive selection of numbered lines with a starting and ending position, normally including both endpoints.
To show lines 11–20, keep the first 20 lines with head, then the last ten of that selection.
locate
locate is a fast path-name lookup that searches a prepared database, whose contents may lag behind changes to the live file system.
A newly created note may be absent from locate results until the path database is refreshed.

M

Marked selection
A marked selection is text chosen between a starting mark and the cursor, so nano can cut the chosen part instead of a whole line.
Press Alt+A to begin marking, move to the other end, then use Ctrl+K to cut the selected text.
Match extraction
Match extraction prints matching text fragments instead of entire input lines, as grep’s -o option does for each nonempty match.
If one line contains "id=42 id=57", grep -Eo '[0-9]+' prints the two numbers on separate lines.
Metacharacter
A metacharacter has a special pattern meaning, such as the regex dot for one character or the star for repetition of the preceding item.
Quotes protect * from the shell, but grep still interprets it as a regex operator when it receives it.
Modal editor
A modal editor assigns different meanings to the same keys in different modes, separating text entry from commands for moving and editing.
In vi, pressing "x" deletes a character in normal mode but types an "x" in insert mode.
Modification time
Modification time records when file contents last changed; find’s -mtime tests elapsed age in completed 24-hour periods.
A file changed 30.5 days ago fails -mtime +30 because only 30 complete day periods have passed.

N

Name test
A name test compares an item’s whole base name with a wildcard pattern; find’s -name distinguishes case while -iname ignores it.
The test -name 'plan*' accepts "plan.txt", whereas -name 'plan' requires that exact base name.
nano
nano is a terminal text editor that accepts typed text directly and displays shortcuts for saving, searching, cutting, and leaving.
Open nano +12 app.conf when a program reports that a setting needs attention on line 12.
Newline
A newline marks the end of a text line; wc -l counts these markers, so an unfinished final line does not add to its reported total.
Text containing "one", a newline, and then "two" gives a newline count of 1 if no final marker follows.
Noclobber
Noclobber is a Bash setting that blocks ordinary > redirection onto existing regular files, helping prevent accidental replacement.
After set -o noclobber, use >| only when you intend to override the existing-file protection.
Normal mode
Normal mode is vi’s command state, where keys move the cursor or edit text and Esc establishes the state before another command.
Press Esc, then dd to cut the current line without accidentally typing those two letters.
Null device
The null device, /dev/null, is a special file that discards written data, allowing a chosen output stream to be deliberately hidden.
Adding 2>/dev/null hides diagnostics from a file search, including messages about unreadable folders.
Numeric sort
A numeric sort compares the numerical values of text fields, so values such as 9 and 100 are ordered by magnitude instead of first digit.
Use sort -n to put "9" before "100" when the lines represent quantities instead of labels.

P

Pager
A pager is a program that displays text one screen at a time and waits for navigation keys, letting you read long output at your own pace.
Open a long list with less /etc/services, then press Space for the next screen or q to quit.
PATH
PATH is the shell’s list of folders to search for external executable commands, which a tool such as which uses for a program-file lookup.
which grep searches command folders instead of scanning every directory on the computer.
Pipe
A pipe connects one command’s standard output to another command’s standard input, passing data without an intermediate saved file.
In grep -i error app.log | wc -l, selected lines flow into the command that counts them.
Pipeline
A pipeline is a series of commands joined by pipes, in which each stage receives data produced by the stage immediately before it.
In sort values.txt | uniq | wc -l, the final count measures distinct grouped values.

Q

Quantifier
A quantifier specifies how often the preceding regex item may occur, such as ? for optional text or {4} for exactly four repeats.
The expression [0-9]{4} requires four digits; anchors can make that the entire line’s content.

R

Recursive search
A recursive search visits a directory and its subdirectories; grep -r searches text in files across that directory tree.
grep -rl TODO notes lists names of matching files without printing all their matching text.
Redirection
Redirection is a shell operation that changes where a command reads input or sends output, setting the connections before execution.
In sort < names.txt > ordered.txt, the shell supplies one file as input and opens another for output.
Regular expression
A regular expression is a pattern describing matching text through literal characters and operators for positions, choices, or repetitions.
The pattern ^web[0-9]$ describes a whole line containing "web" followed by one digit.

S

Search depth
Search depth measures levels below a starting path; find’s -maxdepth limits how far traversal may descend into subdirectories.
Use -maxdepth 1 to inspect the starting folder and its immediate entries without entering deeper folders.
Sort key
A sort key is the field or part of a line used for comparison, selected with sort -k and interpreted using the chosen separator.
The key -k3,3nr compares field 3 only, numerically and in reverse order, in a score ranking.
Standard error
Standard error is the diagnostic stream numbered 2, kept separate from normal results so errors and status messages can be routed independently.
Saving a directory listing with > can leave a missing-path message visible on standard error.
Standard input
Standard input is a command’s incoming data stream, numbered 0, which the shell can connect to the keyboard, a file, or a pipe.
In sort < names.txt, the shell connects the file to the command’s standard input before it runs.
Standard output
Standard output is a command’s normal result stream, numbered 1, which can go to the terminal, an output file, or another command.
In wc -l < names.txt > count.txt, the line count travels on standard output into the report file.
Substitution
A substitution replaces matched text through vi’s command line, with a range choosing lines and flags controlling replacement within them.
The command :%s/old/new/g replaces every matching "old" on every line of the file.

T

tail
tail is a filter that prints the end of its input, or starts at a numbered line when the line count is preceded by a plus sign.
Use tail -n +2 scores.csv to omit a one-line header while keeping all remaining data records.
tee
tee is a filter that copies its input both to a named file and to standard output, saving a result while allowing a pipeline to continue.
Insert tee selected.txt before a counting stage when you also need to inspect the selected lines.
Traversal
A traversal visits items in a directory tree, moving through subfolders as needed; find’s traversal order need not be alphabetical.
Append | sort to a plain path search when the order of its printed results needs to be predictable.
Truncation
Truncation empties an existing output file when the shell opens it for ordinary > redirection, before the command begins reading input.
The command sort names.txt > names.txt loses the original data because truncation happens first.
type
type is a Bash built-in that describes how a command name is interpreted, including an alias, function, built-in, or executable path.
type echo can report a built-in even when which echo points to an external executable file.
Type test
A type test restricts a file search to a kind of item, using codes such as f for a regular file, d for a directory, or l for a link.
Adding -type f prevents a directory named "old.txt" from qualifying in a search for text files.

U

uniq
uniq is a filter that detects adjacent equal lines, removing repeated copies or reporting selected runs and their counts through options.
Sorting scattered values before uniq -c makes each value’s full frequency appear as one count.
updatedb
updatedb rebuilds the database used for indexed path lookups, making its stored view reflect the paths included during the refresh.
An administrator can run sudo updatedb when an immediate system-wide refresh is appropriate.

W

wc
wc is a counting command that reports newlines, words, and bytes by default, with options to select one measure or count characters.
For UTF-8 text such as "café", compare wc -c with wc -m to distinguish bytes from characters.
Whole-word match
A whole-word match requires the matching text to occupy an entire word, although other words may still appear on the same line.
grep -w error file matches "error found" but rejects the substring inside "terror".
Write Out
Write Out is nano’s save action, started with Ctrl+O and completed by confirming the destination file name, normally with Enter.
After changing a setting, use Write Out before exiting if you want the new text preserved on disk.

Y

Yank
To yank is to copy text in vi for later pasting, as yy does for a whole line without removing that line from the working buffer.
Use yy followed by p to duplicate a line below its original position in the file.

About this lesson

By StudeliReport a mistake

Make your own Study Materials

Turn your notes, slides and documents into Study Guides, Practice Questions, Flashcards and 6 more tools.

Get Started