VMware ESXi and vSphere Cluster Management

Linux Command-Line Utilities: Shell Interpretation, Text Processing, Timing, and File Inspection

Learn type, fmt, tr, time, nl, and od for command discovery, text transformation, timing, line numbering, and raw file inspection in Linux.

Linux command-line utilities are small tools that read input, perform one focused operation, and write output. This lesson covers type, fmt, tr, time, nl, and od, along with the shell streams and pipelines that connect them.

Command-line utility fundamentals

A command usually follows this pattern:

command [options] [operands]

An option changes behavior, while an operand identifies an input such as a file name. For example, -w 72 selects a width for fmt, and notes.txt is its input file.

The three standard streams

  • Standard input (stdin) is the default input stream. It commonly comes from the terminal, a file redirection, or a pipeline.
  • Standard output (stdout) is normal command output.
  • Standard error (stderr) is diagnostic output such as warnings and error messages. It is separate from stdout.

Redirection controls these streams:

utility < input.txt              # read stdin from a file
utility > output.txt             # write stdout to a new file or replace it
utility >> output.txt            # append stdout
utility 2> errors.txt             # write stderr
utility > output.txt 2> errors.txt
utility &> all-output.txt         # common Bash syntax for stdout and stderr

A pipeline connects stdout from one command to stdin of another:

command1 | command2 | command3

For example, tr can transform text before nl numbers it. Utilities in this lesson generally do not modify an input file in place. Save transformed output to a different file, inspect it, and replace the original only after verifying it.

Help and safe experimentation

Manual pages document installed commands:

man type
man fmt
man tr
man time
man nl
man od

Shell builtins often provide concise help:

help type
help time

Use a command's own options to request help when supported, for example fmt --help. Manual pages are authoritative for the implementation installed on the current system because options can differ between shells and Unix-like systems.

Utility overview

CommandPrimary purposeTypical inputTypical outputCommon use case
typeDescribe shell command resolutionCommand nameClassification or pathFind aliases, functions, builtins, and executables
fmtReflow paragraphsPlain text or stdinWrapped plain textMake prose fit a selected width
trTranslate, delete, or squeeze charactersstdinTransformed character streamCase conversion and whitespace cleanup
timeMeasure command duration and CPU useA commandCommand output plus timing diagnosticsBasic performance investigation
nlNumber input linesFile or stdinNumbered textCode review and line references
odDisplay raw input in selected representationsFile or stdinOffsets and formatted bytesInspect binary data and control characters

Discovering command resolution with type

The shell does not automatically treat every typed name as a file in PATH. A command name may identify an alias, a shell function, a shell builtin, a shell keyword, or an external executable. An alias is a shell-defined replacement, commonly used to add options or abbreviate a command. A builtin is implemented inside the shell. A function is a named group of shell commands.

type cd
 type ls
 type -a printf

Typical results might identify cd as a shell builtin, ls as an alias or executable, and printf as both a builtin and an external program. Exact wording depends on the shell.

Useful type options

  • type name reports how the current shell resolves name.
  • type -a name displays all known interpretations or locations when supported.
  • type -t name in shells that support it prints only a classification such as alias, builtin, function, or file.
  • type -P name in Bash searches for an external executable and prints its path, bypassing aliases and functions.
Result typeMeaningEffect on command executionHow to investigate further
aliasA shell substitution for the nameThe alias expands before normal executionUse alias name or type -a name
shell functionA named shell command groupThe function can override an executableUse declare -f name in Bash or inspect shell function listings
shell builtinImplemented by the current shellThe shell runs it without locating a separate fileRead help name and the shell manual
external executableA runnable file found through PATHThe shell starts that programUse type -a, command -v, or inspect PATH
not foundNo recognized command matchesThe shell reports an errorCheck spelling, installation, permissions, and PATH

Lookup order and related tools

Exact lookup rules vary by shell, but aliases and functions can take precedence over builtins and external files. External commands are searched in the directories listed in PATH, from left to right. A command in an earlier directory wins when several executable files have the same name.

  • command -v name provides a concise command location or description and is useful in scripts.
  • command -V name usually gives a more explanatory result.
  • which name commonly searches for external executables, but may not understand the current shell's aliases, functions, or builtins. It is less reliable for shell-resolution questions.
  • whereis name searches standard locations for binaries, source files, and manual pages; it is not a complete representation of what the current shell will execute.

Run type before scripting or troubleshooting surprising behavior. If the result is unexpected, run type -a name, inspect aliases and functions, review echo "$PATH", and compare with command -V name.

Reformatting paragraphs with fmt

fmt reformats plain-text paragraphs by joining and wrapping lines to a target width. A paragraph is normally a block separated from the next block by a blank line.

fmt notes.txt
fmt -w 72 notes.txt > notes-formatted.txt
printf 'This is a long sentence supplied through standard input.\n' | fmt -w 40

The output-width option is -w width. Writing to a new file preserves the source:

fmt -w 72 notes.txt > notes-formatted.txt

Blank lines usually separate paragraphs. Indentation also affects how fmt recognizes and preserves text structure. Options such as -u can request uniform spacing, and -p prefix can format only lines beginning with a selected prefix in implementations that support it. Read the local manual for precise indentation controls, including options such as -c and -s.

fmt is intended for prose, not arbitrary documents. It can damage source code, tables, markup, lists, preformatted blocks, or structured documents where whitespace has meaning. Preview the result and format only prose sections when necessary.

Translating and filtering characters with tr

tr reads standard input and operates character by character. It does not open a named input file as an operand in the usual form and does not modify files directly. Use redirection or a pipeline.

Translation sets

The first set supplies source characters and the second supplies corresponding destination characters:

tr '[:lower:]' '[:upper:]' < input.txt > output.txt
tr 'abc' 'ABC' < input.txt

Character classes are named sets such as [:lower:], [:upper:], [:digit:], [:space:], and [:blank:]. Their exact behavior is affected by the current locale. Single quotes prevent the shell from interpreting most punctuation in the sets.

Deleting and squeezing

tr -d '\r' < windows-lines.txt > unix-lines.txt
tr -d '[:digit:]' < input.txt
tr -s ' ' < input.txt
printf 'one     two   three\n' | tr -s ' '

-d deletes characters in the selected set. -s squeezes consecutive occurrences of a character into one occurrence. Common escape sequences include \n for newline, \t for tab, and \r for carriage return. Ranges such as A-Z and bracket expressions are supported, but locale-sensitive classes are often safer for alphabetic data.

tr is not a regular-expression replacement tool. It translates individual characters, not arbitrary strings. For replacing words, matching patterns, or conditional processing, use tools such as sed, awk, or Perl.

UtilityOperates onCan reflow textCan replace charactersCan number linesTypical limitation
fmtPlain-text paragraphsYesNoNoUnsafe for whitespace-sensitive formats
trCharacters in a streamNoYes, character by characterNoNot a general regex or string replacement tool
nlInput linesNoNoYesDefault excludes blank lines
sedLines and patternsLimitedYesIndirectlyRequires command-language knowledge
awkRecords and fieldsApplication-dependentYesYesMore powerful but more complex

Measuring execution with time

time measures a command and reports several durations. A shell may implement it as a keyword or builtin, while an external program may also exist. Check with:

type time
command -V time
time sort large-list.txt > sorted-list.txt

Timing information commonly goes to stderr, while the command's normal output goes to stdout. Therefore the redirection above saves sorted data but normally leaves timing information visible in the terminal.

MeasurementWhat it representsWhat can affect itAppropriate interpretation
realWall-clock time: total elapsed timeScheduling, I/O, contention, sleep, caching, and system loadHow long a user waits
userCPU time executing program instructions in user spaceAlgorithm, input size, CPU frequency, and parallelismWork performed by the program itself
sysCPU time spent in operating-system services for the processSystem calls, file I/O, memory management, and kernel activityOperating-system work caused by the command

A pipeline, loop, compound command, or shell function can be timed as a unit:

time sort data.txt | uniq
 time { for file in *.log; do cat "$file"; done; }
 time my_shell_function

Use the syntax supported by the current shell. External implementations often provide formatting options such as -f, but the format string and available options differ. Consult man time and remember that it may document the external program rather than the shell keyword.

Wall-clock time can be much greater than user plus system CPU time when a command waits for disk, network, another process, or a scheduler. For meaningful comparisons, use the same input, run multiple trials, account for filesystem caching and system load, and avoid treating one run as a benchmark.

Numbering lines with nl

nl adds line numbers for review, debugging, references, and later text processing. By default, it numbers nonblank lines:

nl document.txt
nl -ba document.txt

-ba means number all physical lines, including blank lines. Input can also arrive through stdin:

printf 'first\n\nthird\n' | nl
printf 'first\n\nthird\n' | nl -ba

Number formatting and sections

  • -v number selects the starting number.
  • -i increment changes the amount added to each subsequent number.
  • -w width sets the number field width.
  • -s separator sets the text between the number field and the input line.
  • -b a numbers all lines; -b t numbers nonblank lines; implementations also support pattern-based numbering such as -b ppattern.
nl -v 100 -i 10 -w 5 -s ': ' document.txt

nl can recognize logical page sections using form-feed separators. Header, body, and footer sections may have separate numbering rules where supported. This distinction matters because a logical page is an input section managed by nl, not necessarily a physical sheet of paper.

cat -n generally numbers every line, while cat -b generally numbers only nonblank lines. Use nl when you need configurable starting values, increments, separators, formats, or section behavior.

Inspecting raw data with od

od means octal dump. An octal number uses base 8, a traditional Unix representation. Raw byte inspection is useful for identifying file signatures, line endings, nonprinting characters, padding, unexpected bytes, and corruption.

od file.bin
od -c textfile.txt
od -Ax -tx1z file.bin

The default output is octal-oriented. Character output uses escapes or readable characters for control bytes. The command can also display hexadecimal, decimal, floating-point, and other formats supported by the local implementation.

Format selectionRepresentationBest forExample observation
default or octal byte formatBase-8 valuesTraditional Unix-oriented inspectionA byte is shown as an octal number
-tx1One-byte hexadecimal valuesBinary signatures and byte comparison7f 45 4c 46 suggests an ELF file
-cCharacter and escape-style outputText and control characters\n, \r, or \t becomes visible
-a with character outputNamed characters where supportedReadable labels for nonprinting bytesA control byte may be labeled nul or lf
-tdDecimal valuesNumeric byte or word analysisBytes are displayed as base-10 values

Offsets, widths, skipping, and limits

A byte offset is a byte's position from the beginning of the input. -A x selects hexadecimal addresses, while -A d and -A o select decimal and octal address radices. Format directives such as -t x1, -t o1, and -t d1 select one-byte hexadecimal, octal, or decimal output. Width suffixes can request larger units where supported.

od -Ax -tx1z file.bin       # hexadecimal offsets, hex bytes, printable view
od -An -tx1 file.bin       # suppress offsets
od -j 16 -N 32 -Ax -tx1 file.bin  # skip 16 bytes and read 32 bytes

Options vary slightly between implementations. The local manual specifies whether skip and count values accept suffixes and which format combinations are available.

Repeated identical output lines may be compressed into a line containing an asterisk. This prevents a large run of identical bytes from filling the terminal. If you need every repeated line, use the implementation's option for outputting duplicates, commonly -v.

For general inspection, od -Ax -tx1z combines offsets, hexadecimal bytes, and a printable-character rendering. hexdump and xxd are convenient alternatives with different formatting and reverse-conversion features; choose according to the format needed by your task.

Combining utilities in pipelines

Choose the utility according to the data level involved:

  • Use fmt for prose paragraphs and line width.
  • Use tr for character-by-character translation, deletion, and squeezing.
  • Use nl for physical or logical input-line numbering.
  • Use od for bytes and representations such as hexadecimal or octal.
  • Use time around a command or compound operation when measuring execution.
tr '[:lower:]' '[:upper:]' < input.txt | fmt -w 60 | nl -ba > reviewed.txt

This pipeline converts letters to uppercase, reformats plain-text paragraphs to 60 columns, numbers every resulting line, and saves the result. The original remains unchanged.

printf 'A  B\r\nC\tD\n' | tr -d '\r' | tr -s '[:space:]' | nl -ba
printf 'header\nbody\n' | od -Ax -tx1z

The first example removes carriage returns, squeezes repeated whitespace characters, and numbers the stream. The second inspects the bytes rather than reformatting the text.

Troubleshooting common problems

type reports an unexpected command

  • An alias or shell function may override the executable.
  • The expected directory may not be first in PATH.
  • Different shells may provide different builtins and lookup behavior.

Run type -a name, inspect aliases and functions, review PATH, and compare with command -V name or command -v name.

fmt damages layout

The input may contain code, tables, markup, lists, or preformatted blocks. Test on a copy, preview redirected output, select a suitable width, and restrict formatting to prose. Use a format-aware tool for structured documents.

tr does not replace a word or phrase

tr translates characters rather than matching strings or regular expressions. Test a small sample with printf, quote character sets carefully, and use sed, awk, or another pattern-oriented tool for multi-character replacement.

time output is missing or mixed with data

Timing diagnostics commonly use stderr. A redirection of stdout alone captures the program's data, not the timing report. Also determine whether the shell keyword, builtin, or external implementation is being used, because formatting options differ.

nl does not number blank lines

That is the default behavior. Use nl -ba for every physical line, or use the appropriate body-style and pattern options for selective numbering.

od is unreadable or shows an asterisk

Select a representation suited to the problem, such as -c for text controls or -tx1z for binary data. An asterisk usually indicates repeated lines were compressed; use -v when full duplicate output is required. Use skip and byte-count options to focus on a header or damaged region.

Exam-relevant notes

  • type describes shell resolution; which usually only searches external executables.
  • tr reads stdin and transforms characters; it does not perform general string substitution or edit a file directly.
  • fmt reflows plain-text paragraphs and can be unsafe when whitespace carries meaning.
  • time reports wall-clock, user CPU, and system CPU measurements; these values are not interchangeable.
  • nl numbers nonblank lines by default; nl -ba numbers all lines.
  • od displays representations of raw input bytes, and offsets are positions from the start of the input.
  • Use redirection to preserve source files and remember that stdout and stderr are separate streams.

For broader command-line practice, continue with the Linux command-line guide.