In this lab, you use the Bash shell to execute commands.
Outcomes
Successfully run simple programs from the Bash shell command line.
Execute commands to identify file types and to display parts of text files.
Practice using some Bash command history shortcuts to more efficiently repeat commands or parts of commands.
As the student user on the workstation machine, use the lab command to prepare your system for this exercise.
This command prepares your environment and ensures that all required resources are available.
[student@workstation ~]$ lab start cli-review
Instructions
Use the date command to display the current time and date.
Display the current time in 24-hour clock time (for example, 13:57).
Hint: The format string that displays that output is %R.
What kind of file is /home/student/zcat?
Is it readable by humans?
Use the wc command and Bash shortcuts to display the size of the zcat file.
You can use the wc command to display the number of lines, words, and bytes in the zcat script.
Instead of retyping the file name, use the Bash history shortcut Esc+. (the keys Esc and . pressed at the same time) to reuse the argument from the previous command.
[student@workstation ~]$wc Esc+.[student@workstation ~]$wc zcat51 299 1988 zcat
Display the first 10 lines of the zcat file.
The head command displays the beginning of the file.
Try the Esc+. shortcut again.
[student@workstation ~]$head Esc+.[student@workstation ~]$head zcat#!/bin/sh # Uncompress files to standard output. # Copyright (C) 2007, 2010-2018 Free Software Foundation, Inc. # This program is free software; you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation; either version 3 of the License, or # (at your option) any later version.
Display the last 10 lines of the zcat file.
Use the tail command to display the last 10 lines of the zcat file.
[student@workstation ~]$tail Esc+.[student@workstation ~]$tail zcatWith no FILE, or when FILE is -, read standard input. Report bugs to "<bug-gzip@gnu.org>." case $1 in --help) printf '%s\n' "$usage" || exit 1;; --version) printf '%s\n' "$version" || exit 1;; esac exec gzip -cd "$@"
Repeat the previous command exactly with four or fewer keystrokes.
Repeat the previous command exactly with four or fewer keystrokes.
Press the UpArrow key once to scroll back one command through the command history, and then press Enter (uses two keystrokes).
An alternative would be to enter the shortcut command !! and then press Enter (uses four keystrokes) to run the most recent command in the command history.
Try both methods.
[student@workstation]$ !!
tail zcat
With no FILE, or when FILE is -, read standard input.
Report bugs to "<bug-gzip@gnu.org>."
case $1 in
--help) printf '%s\n' "$usage" || exit 1;;
--version) printf '%s\n' "$version" || exit 1;;
esac
exec gzip -cd "$@"Use the tail command -n 20 option to display the last 20 lines in the file.
Use command-line editing to accomplish this task with a minimal number of keystrokes.
Use UpArrow key to display the previous command.
Next, use the Ctrl+A key combination to move the cursor to the beginning of the line.
Next, use the Ctrl+RightArrow key combination to jump to the next word.
Next, add the -n 20 option and press Enter to execute the command.
[student@workstation ~]$ tail -n 20 zcat
-l, --list list compressed file contents
-q, --quiet suppress all warnings
-r, --recursive operate recursively on directories
-S, --suffix=SUF use suffix SUF on compressed files
--synchronous synchronous output (safer if system crashes, but slower)
-t, --test test compressed file integrity
-v, --verbose verbose mode
--help display this help and exit
--version display version information and exit
With no FILE, or when FILE is -, read standard input.
Report bugs to "<bug-gzip@gnu.org>."
case $1 in
--help) printf '%s\n' "$usage" || exit 1; exit;;
--version) printf '%s\n' "$version" || exit 1; exit;;
esac
exec gzip -cd "$@"Use the shell history to run the date +%R command again.
Use the history command to display the list of previous commands and to identify the specific date command to execute.
Use ! to run the command, where number is the command number to use from the output of the numberhistory command.
Your shell history might differ from the following example.
Determine the command number to use based on the output of your own history command.
[student@workstation ~]$history1 date 2 date +%R 3 file zcat 4 wc zcat 5 head zcat 6 tail zcat 7 tail -n 20 zcat 8 history [student@workstation ~]$!2date +%R 14:02
This concludes the section.