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

Special Permissions in linux

The setuid permission on an executable file means that the command will run as the user owning the file, not as the user that ran the command. One example is the passwd command: [student@desktopX ~]$ ls -l /usr/bin/passwd -rw s r-xr-x. 1 root root 35504 Jul 16 2010 /usr/bin/passwd In a long listing, you can spot the setuid permissions by a lowercase s where you would normally expect the x (owner execute permissions) to be. If the owner does not have execute permissions, this will be replaced by an uppercase S . The special permission setgid on a directory means that files created in the directory will inherit their group ownership from the directory, rather than inheriting it from the creating user. This is commonly used on group collaborative directories to automatically change a file from the default private group to the shared group, or if files in a directory should be

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

RequestsDependencyWarning: urllib3 (1.24.1) or chardet (3.0.4) doesn't match a supported version

import tweepy /usr/lib/python2.7/dist-packages/requests/__init__.py:80: RequestsDependencyWarning: urllib3 (1.24.1) or chardet (3.0.4) doesn't match a supported version!   RequestsDependencyWarning) Traceback (most recent call last):   File "<stdin>", line 1, in <module>   File "/usr/local/lib/python2.7/dist-packages/tweepy/__init__.py", line 14, in <module>     from tweepy.api import API   File "/usr/local/lib/python2.7/dist-packages/tweepy/api.py", line 12, in <module>     from tweepy.binder import bind_api   File "/usr/local/lib/python2.7/dist-packages/tweepy/binder.py", line 11, in <module>     import requests   File "/usr/lib/python2.7/dist-packages/requests/__init__.py", line 97, in <module>     from . import utils   File "/usr/lib/python2.7/dist-packages/requests/utils.py", line 26, in <module>     from ._internal_utils import to_native_string   File "/usr/lib/python2.

tag