Chapter 2: Command-line essentials
Exercise: Run commands efficiently
Investigate a real file on workstation, /usr/bin/zcat, with the commands from this chapter, and use completion, history and line editing so that you type its long name only once.
zcat prints the contents of compressed files. It is a good subject because it is small, and because its name doesn't tell you what kind of file it is.
Run commands efficiently
- Identify a file's type and read parts of it.
- Reuse arguments and commands from the history instead of retyping them.
- Edit a previous command with the line-editing keys.
Show the time on a 24-hour clock
On workstation, as student:
[student@workstation ~]$ date +%R
09:39Find out what kind of file zcat is
Type file /usr/bin/zc and press Tab to complete the name, then Enter:
[student@workstation ~]$ file /usr/bin/zcat
/usr/bin/zcat: a /usr/bin/sh script, ASCII text executableIt is a script: a text file of shell commands that runs like a program. Because it is text, you can read it.
Count its lines with Alt+.
Type wc and a space, then press Alt+. to bring back the last argument, /usr/bin/zcat:
[student@workstation ~]$ wc /usr/bin/zcat
51 299 1988 /usr/bin/zcat51 lines, 299 words and 1988 bytes.
Show its first and last lines
Use Alt+. again for each command:
[student@workstation ~]$ head /usr/bin/zcat
#!/usr/bin/sh
# Uncompress files to standard output.
# Copyright (C) 2007, 2010-2022 Free Software Foundation, Inc.
...output omitted...
[student@workstation ~]$ tail /usr/bin/zcat
With no FILE, or when FILE is -, read standard input.
...output omitted...
exec gzip -cd "$@"The first line, #!/usr/bin/sh, names the program that runs the script. The last line shows that zcat is a small wrapper: it hands its work to gzip -cd.
Repeat the last command in two keystrokes
Press ↑ then Enter. Then do the same with !!, which prints the command it expands to:
[student@workstation ~]$ !!
tail /usr/bin/zcat
With no FILE, or when FILE is -, read standard input.
...output omitted...Edit the last command to show 20 lines
Press ↑ to bring back tail /usr/bin/zcat, Ctrl+A to jump to the start, Ctrl+→ to jump past tail, type a space and -n 20, then press Enter:
[student@workstation ~]$ tail -n 20 /usr/bin/zcat
-l, --list list compressed file contents
-q, --quiet suppress all warnings
...output omitted...
exec gzip -cd "$@"Rerun the date command from the history
List the history and find the number of date +%R. Yours will differ from this example:
[student@workstation ~]$ history | tail -n 7
41 date +%R
42 file /usr/bin/zcat
43 wc /usr/bin/zcat
44 head /usr/bin/zcat
45 tail /usr/bin/zcat
46 tail -n 20 /usr/bin/zcat
47 history | tail -n 7
[student@workstation ~]$ !41
date +%R
09:41You ran tail /usr/bin/zcat three times in a row, but it appears once: RHEL sets HISTCONTROL=ignoredups in /etc/profile, which tells Bash not to store a command that repeats the one before it.
| tail -n 7 sends the long history list through tail, so you see only the last seven lines. Chapter 5 explains the | (pipe).
Find a command with Ctrl+R
Press Ctrl+R and type wc. The prompt changes to a search, and shows the most recent match:
(reverse-i-search)`wc': wc /usr/bin/zcatPress Enter to run it, or Esc to put it on the command line for editing.