Bookmark this page

Lab: Access the Command Line

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

  1. Use the date command to display the current time and date.

    [student@workstation ~]$ date
    Mon Feb 28 01:57:25 PM PDT 2022
  2. Display the current time in 24-hour clock time (for example, 13:57). Hint: The format string that displays that output is %R.

    1. Use the +%R argument with the date command to display the current time in 24-hour clock time.

      [student@workstation ~]$ date +%R
      13:58
  3. What kind of file is /home/student/zcat? Is it readable by humans?

    1. Use the file command to determine its file type.

      [student@workstation ~]$ file zcat
      zcat: a /usr/bin/sh script, ASCII text executable
  4. Use the wc command and Bash shortcuts to display the size of the zcat file.

    1. 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 zcat
        51  299 1988 zcat
  5. Display the first 10 lines of the zcat file.

    1. 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.
  6. Display the last 10 lines of the zcat file.

    1. Use the tail command to display the last 10 lines of the zcat file.

      [student@workstation ~]$ tail Esc+.
      [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 "$@"
  7. Repeat the previous command exactly with four or fewer keystrokes.

    1. 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 "$@"
  8. 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.

    1. 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 "$@"
  9. Use the shell history to run the date +%R command again.

    1. Use the history command to display the list of previous commands and to identify the specific date command to execute. Use !number to run the command, where number is the command number to use from the output of the history 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 ~]$ history
      1   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 ~]$ !2
      date +%R
      14:02

Evaluation

As the student user on the workstation machine, use the lab command to grade your work. Correct any reported failures and rerun the command until successful.

[student@workstation ~]$ lab grade cli-review

Finish

On the workstation machine, change to the student user home directory and use the lab command to complete this exercise. This step is important to ensure that resources from previous exercises do not impact upcoming exercises.

[student@workstation ~]$ lab finish cli-review

Revision: rh124-9.3-770cc61