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
| Command | Primary purpose | Typical input | Typical output | Common use case |
|---|---|---|---|---|
type | Describe shell command resolution | Command name | Classification or path | Find aliases, functions, builtins, and executables |
fmt | Reflow paragraphs | Plain text or stdin | Wrapped plain text | Make prose fit a selected width |
tr | Translate, delete, or squeeze characters | stdin | Transformed character stream | Case conversion and whitespace cleanup |
time | Measure command duration and CPU use | A command | Command output plus timing diagnostics | Basic performance investigation |
nl | Number input lines | File or stdin | Numbered text | Code review and line references |
od | Display raw input in selected representations | File or stdin | Offsets and formatted bytes | Inspect 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 namereports how the current shell resolvesname.type -a namedisplays all known interpretations or locations when supported.type -t namein shells that support it prints only a classification such asalias,builtin,function, orfile.type -P namein Bash searches for an external executable and prints its path, bypassing aliases and functions.
| Result type | Meaning | Effect on command execution | How to investigate further |
|---|---|---|---|
| alias | A shell substitution for the name | The alias expands before normal execution | Use alias name or type -a name |
| shell function | A named shell command group | The function can override an executable | Use declare -f name in Bash or inspect shell function listings |
| shell builtin | Implemented by the current shell | The shell runs it without locating a separate file | Read help name and the shell manual |
| external executable | A runnable file found through PATH | The shell starts that program | Use type -a, command -v, or inspect PATH |
| not found | No recognized command matches | The shell reports an error | Check 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 nameprovides a concise command location or description and is useful in scripts.command -V nameusually gives a more explanatory result.which namecommonly 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 namesearches 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.
| Utility | Operates on | Can reflow text | Can replace characters | Can number lines | Typical limitation |
|---|---|---|---|---|---|
fmt | Plain-text paragraphs | Yes | No | No | Unsafe for whitespace-sensitive formats |
tr | Characters in a stream | No | Yes, character by character | No | Not a general regex or string replacement tool |
nl | Input lines | No | No | Yes | Default excludes blank lines |
sed | Lines and patterns | Limited | Yes | Indirectly | Requires command-language knowledge |
awk | Records and fields | Application-dependent | Yes | Yes | More 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.
| Measurement | What it represents | What can affect it | Appropriate interpretation |
|---|---|---|---|
| real | Wall-clock time: total elapsed time | Scheduling, I/O, contention, sleep, caching, and system load | How long a user waits |
| user | CPU time executing program instructions in user space | Algorithm, input size, CPU frequency, and parallelism | Work performed by the program itself |
| sys | CPU time spent in operating-system services for the process | System calls, file I/O, memory management, and kernel activity | Operating-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 numberselects the starting number.-i incrementchanges the amount added to each subsequent number.-w widthsets the number field width.-s separatorsets the text between the number field and the input line.-b anumbers all lines;-b tnumbers 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 selection | Representation | Best for | Example observation |
|---|---|---|---|
| default or octal byte format | Base-8 values | Traditional Unix-oriented inspection | A byte is shown as an octal number |
-tx1 | One-byte hexadecimal values | Binary signatures and byte comparison | 7f 45 4c 46 suggests an ELF file |
-c | Character and escape-style output | Text and control characters | \n, \r, or \t becomes visible |
-a with character output | Named characters where supported | Readable labels for nonprinting bytes | A control byte may be labeled nul or lf |
-td | Decimal values | Numeric byte or word analysis | Bytes 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
fmtfor prose paragraphs and line width. - Use
trfor character-by-character translation, deletion, and squeezing. - Use
nlfor physical or logical input-line numbering. - Use
odfor bytes and representations such as hexadecimal or octal. - Use
timearound 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
typedescribes shell resolution;whichusually only searches external executables.trreads stdin and transforms characters; it does not perform general string substitution or edit a file directly.fmtreflows plain-text paragraphs and can be unsafe when whitespace carries meaning.timereports wall-clock, user CPU, and system CPU measurements; these values are not interchangeable.nlnumbers nonblank lines by default;nl -banumbers all lines.oddisplays 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.