Skip to main content

Guided Exercise: Accessing the Command Line in linux

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

Outcomes

  • Successfully run simple programs using the Bash shell command line.
  • Practice using some Bash command history "shortcuts" to more efficiently repeat commands or parts of commands.

Before You Begin

This exercise was prepared assuming that you have an Amazon EC2 account, have used it to launch an Amazon EC2 cloud instance based on a free-tier eligible version of Red Hat Enterprise Linux 7, and that you can connect to that instance using ssh key-based authentication as the regular user ec2-user.
If you haven't set this up yet, download Lab Set-up Instructions for Exercises on Amazon EC2 and follow the steps.
Steps
  1. Use ssh to log into your Amazon EC2 system as ec2-user.
  2. Use the date command to display the current time and date.
    [ec2-user@ip-192-0-2-1 ~]$ date
    Mon Apr 17 10:13:04 PDT 2017
    
    In this example, [ec2-user@ip-192-0-2-1 ~]$ is the shell's command prompt, how the shell prompts you to enter a command. It indicates the current user (ec2-user), the short hostname of the machine (ip-192-0-2-1 in this example, although yours will be different), and information about the current directory (which will be discussed later). date is the command you typed. After you press Enter, the output of date is printed by the computer below your prompt (Mon Apr 17 10:13:04 PDT 2017 in the example). You should get a new prompt after the command completes.
  3. Use the +%r argument with the date command to display the current time in 12-hour clock time (for example, 11:42:11 AM).
    [ec2-user@ip-192-0-2-1 ~]$ date +%r
    10:14:07 AM
    
  4. What kind of file is /usr/bin/zcat? Is it readable by humans? Use the file command to determine its file type.
    [ec2-user@ip-192-0-2-1 ~]$ file /usr/bin/zcat
    /usr/bin/zcat: POSIX shell script, ASCII text executable
    
  5. The wc command can be used to display the number of lines, words, and bytes in the script /usr/bin/zcat. 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.
    [ec2-user@ip-192-0-2-1 ~]$ wc <Esc>.
    [ec2-user@ip-192-0-2-1 ~]$ wc /usr/bin/zcat
      56  290 1941 /usr/bin/zcat
  6. Use the head command to display the first 10 lines of /usr/bin/zcat. Try using the Esc+. shortcut again.
    The head command displays the beginning of the file. Did you use the bash shortcut again?
    [ec2-user@ip-192-0-2-1 ~]$ head <Esc>.
    [ec2-user@ip-192-0-2-1 ~]$ head /usr/bin/zcat
    #!/bin/sh
    # Uncompress files to standard output.
    
    # Copyright (C) 2007 Free Software Foundation
    
    # 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.
    
    
  7. Display the last 10 lines at the bottom of the /usr/bin/zcat file. Use the tail command.
    [ec2-user@ip-192-0-2-1 ~]$ tail <Esc>.
    [ec2-user@ip-192-0-2-1 ~]$ tail /usr/bin/zcat
    With no FILE, or when FILE is -, read standard input.
    
    Report bugs to <bug-gzip@gnu.org>."
    
    case $1 in
    --help)    exec echo "$usage";;
    --version) exec echo "$version";;
    esac
    
    exec gzip -cd "$@"
    
  8. Repeat the previous command exactly. Either press the UpArrow key once to scroll back through the command history one command and press Enter, or run the shortcut command !! to run the most recent command in the command history. (Try both!)
    [ec2-user@ip-192-0-2-1 ~]$ !!
    tail /usr/bin/zcat
    With no FILE, or when FILE is -, read standard input.
    
    Report bugs to <bug-gzip@gnu.org>."
    
    case $1 in
    --help)    exec echo "$usage";;
    --version) exec echo "$version";;
    esac
    
    exec gzip -cd "$@"
    
  9. Repeat the previous command again, but this time add the -n 20 option to display the last 20 lines in the file. Use command line editing to accomplish this with a minimal amount of keystrokes.
    UpArrow displays the previous command. Ctrl+a makes the cursor jump to the beginning of the line. Ctrl+Right Arrow jumps to the next word, then add the -n 20 option and press Enter to run the command.
    [ec2-user@ip-192-0-2-1 ~]$ tail -n 20 /usr/bin/zcat
      -f, --force       force; read compressed data even from a terminal
      -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
      -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)    exec echo "$usage";;
    --version) exec echo "$version";;
    esac
    
    exec gzip -cd "$@"
    
  10. Use the shell history to run the date +%r command again.
    Display the list of previous commands with the history command to identify the specific date command to be executed. Run the command with the !number history command.
    Note that your shell history may be different than the following example. Figure out the command number to use based on the output of your own history command.
    [ec2-user@ip-192-0-2-1 ~]$ history
        1  date
        2  date +%r
        3  file /usr/bin/zcat
        4  wc /usr/bin/zcat
        5  head /usr/bin/zcat
        6  tail /usr/bin/zcat
        7  tail -n 20 /usr/bin/zcat
        8  history
    [ec2-user@ip-192-0-2-1 ~]$ !2
    date +%r
    10:49:56 AM
    
  11. Finish your session with the bash shell.
    Use either exit or the Ctrl+d key combination to close the shell and log out.
    [ec2-user@ip-192-0-2-1 ~]$ exit
    
  12. This concludes this exercise. Stop your Amazon EC2 instance.

Comments

Popular posts from this blog

The Seven-Step Model of Migration

Irrespective of the migration approach adopted, the Seven-step Model of Cloud Migration creates a more rational point of view towards the migration process and offers the ability to imbibe several best practices throughout the journey Step 1: Assess Cloud migration assessments are conducted to understand the complexities in the migration process at the code, design and architectural levels. The investment and the recurring costs are also evaluated along with gauging the tools, test cases, functionalities and other features related to the configuration. Step 2: Isolate The applications to be migrated to the cloud from the internal data center are freed of dependencies pertaining to the environment and the existing system. This step cuts a clearer picture about the complexity of the migration process. Step 3: Map Most organisations hold a detailed mapping of their environment with all the systems and applications. This information can be used to distinguish between the ...

Cloud Computing architecture

Cloud computing architecture refers to all components and sub-components that together form the structure of the cloud computing system. This architecture can be divided into three parts for better understanding – front end, back end and middleware. Each part of the cloud architecture has its own set of functionalities and protocols that work together to deliver on-demand services to user-facing hardware as well as software. In general, the architecture is evolved out of large distributed network applications over the last two decades. Hence it supports any system where resources can be pooled and partitioned as required. The general cloud architecture is capable of running multiple software applications running on multiple virtual hardware in multiple locations to efficiently render on-demand services to the users. The users could be using these software applications from their desktop or laptop or mobile or tablets. Usually, whatever the user is looking at – through t...

connection oriented

connection-oriented:- connection-oriented  describes a means of transmitting data in which the devices at the end points use a preliminary  protocol  to establish an end-to-end connection before any data is sent. Connection-oriented protocol service is sometimes called a "reliable" network service, because it guarantees that data will arrive in the proper sequence. Transmission Control Protocol ( TCP ) is a connection-oriented protocol. For connection-oriented communications, each end point must be able to transmit so that it can communicate. The alternative to connection-oriented transmission is the  connection-less  approach, in which data is sent from one end point to another without prior arrangement. Connection-less protocols are usually described as  stateless  because the end points have no protocol-defined way to remember where they are in a "conversation" of message exchanges. Because they can keep track of a conversation, connection-or...

tag