Bookmark this page

Lab: Accessing the Command Line

Performance Checklist

In this lab, you will use the Bash shell to execute commands.

Outcomes

  • Successfully run simple programs using the Bash shell command line.

  • Execute commands used to identify file types and display parts of text files.

  • Practice using some Bash command history "shortcuts" to more efficiently repeat commands or parts of commands.

Log in to workstation as student using student as the password.

On workstation, run the lab cli-review start script to set up a clean lab environment. The script also copies the zcat file to student's home directory.

[student@workstation ~]$ lab cli-review start
  1. Use the date command to display the current time and date.

    [student@workstation ~]$ date
    Thu Jan 22 10:13:04 PDT 2019
  2. Display the current time in 12-hour clock time (for example, 11:42:11 AM). Hint: The format string that displays that output is %r.

    Use the +%r argument with the date command to display the current time in 12-hour clock time.

    [student@workstation ~]$ date +%r
    10:14:07 AM
  3. What kind of file is /home/student/zcat? Is it readable by humans?

    Use the file command to determine its file type.

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

    The wc command can be used 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 1983 zcat
  5. Display the first 10 lines of zcat.

    The head command displays the beginning of the file. Try using 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.

    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 three or fewer keystrokes.

    Repeat the previous command exactly. Either press the UpArrow key once to scroll back through the command history one command and then press Enter (uses two keystrokes), or enter the shortcut command !! and then press Enter (uses three keystrokes) to run the most recent command in the command history . (Try both.)

    [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. Repeat the previous command, but use the -n 20 option to display the last 20 lines in the file. Use command-line editing to accomplish this with a minimal number of keystrokes.

    UpArrow displays the previous command. Ctrl+A makes the cursor jump to the beginning of the line. Ctrl+RightArrow jumps to the next word, then add the -n 20 option and hit 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.

    Use the history command to display the list of previous commands and to identify the specific date command to be executed. Use !number to run the command, where number is the command number to use from the output of the history command.

    Note that your shell history may be different 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
    10:49:56 AM

Evaluation

On workstation, run the lab cli-review grade script to confirm success on this exercise.

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

Finish

On workstation, run the lab cli-review finish script to complete the lab.

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

This concludes the lab.

Revision: rh124-8.2-df5a585