Linux and Vi commands

If you search the web for ‘linux cheat sheet‘ you find numerous one-page documents that list the most commonly used Linux commands.  I recommend that you print or bookmark one to help you learn them.

Navigating the File System

Some of the most common commands that we’ll use are for listing the files within a directory and moving around the file system.

  • ls
  • ls -l
  • ls -al
  • pwd
  • cd /
  • cd $HOME
  • cd ..
  • cd [dir] (absolute or relative path)

UNIX directory structure

Standard-unix-filesystem-hierarchy

Manipulating Directories and Files

  • mkdir [dir]
  • rmdir [dir]
  • mv [src] [dest]
  • touch [file]
  • cp [orig] [new]
  • cp -r [src_dir] [dest_dir]
  • rm [file]
  • rm -r [dir]    (BE CAREFUL!)
  • chmod [mode] [file]

Displaying File, Manual, Environment and System Information

  • cat [file]
  • man [command]
  • which [command]
  • whereis [command]
  • env
  • whoami
  • hostname
  • date
  • clear

Viewing and Killing Processes

  • ps
  • ps -ax
  • kill [pid]
  • kill -9 [pid]

Pipes

  • [command] | [command]
  • cat filename | less

Vi – The Editor of Champions

  • Insert Mode (i)
    • arrow keys
    • delete
  • Command Mode (esc)
    • x – delete char
    • r [char] – replace char
    • dd – delete line
    • # dd – delete multiple lines
    • u – undo last command
    • yy – copy to clipboard
    • # yy – copy multiple lines
    • p – paste contents of clip board
    • $ – go to the end of a line
    • 0 – go to the beginning of a line
    • shift + G – go to the end of the file
    • shift + H – go to the beginning of the file
    • :# – goto line a particular line
    • :/search_term – search for a particular term
    • :%s/old/new/g – find and replace
    • :w – write the file
    • :q – quit vi
    • :q! – quit without writing
    • shift+z+z – write and quit
    • :set number – show line numbers
    • :set nonumber – hide line numbers

How to Transfer Files Between your Local Machine to Your Linux Instance

On a Windows machine:

  1. Download any jpg or gif image to your local machine and name the file background.jpg or background.gif depending on the file type.
  2. Download the Filezilla client.
  3. Select Edit -> Settings -> Connection -> SFTP.
    1. Press Add key file… button.
    2. Locate and select your .pem key file.
    3. Click OK in the bottom left pane.
  4. Select File->Site Manager
    1. Press New Site
    2. Add IP address to Host field.
    3. Set Port field to 22
    4. Set Protocol to SFTP: SSH File Transfer Protocol
    5. Set Logon Type to Normal
    6. Set User to ec2-user
    7. Clear Password field
    8. Select OK
  5. Below the File menu option select the arrow for Open Site Manager.
    1. Choose the Site you created in step 4.
  6. Once a connection is established you can drag and drop files from your local machine to the Linux instance and vice-versa.

On a Mac machine, don’t be a wimp and use Filezilla. Do the following:

  1. Download any jpg or gif image to your local machine and name the file background.jpg or background.gif depending on the file type.
  2. Open up the Terminal application.
  3. Identify the absolute path to your .pem key file.
  4. Change directory to the location of the image you downloaded.
  5. Type the following command:

sftp -i PATH_TO_KEY_FILE/XXX.pem ec2-user@YOUR_IP_ADDRESS

where PATH_TO_KEY_FILE is the absolute path to your key file, XXX.pem is the name of your keyfile and YOUR_IP_ADDRESS is (wait for it) your IP address.

  1. Once logged into the remote machine with sftp, use the cd and ls commands to change the current working directory to where you want the uploaded file to be copied.
  2. Copy the file from your local machine to the current working directory of the remote machine using the following command:

put background.jpg

  1. You can quit out of sftp by using the quit command.

Using Curl

$ curl URL

Flags

-v –verbose

  • mode shows you the state of curl as it is processing the request

-l –location

-d [str]

  • sends str to the URL
  • need double quotes if str has spaces in it

-d [@filename]

  • sends contents of file to URL

-u [username:password]

-w [text]

  • writes text tp stdout after curl completes

-o [filename]

  • output contents of curl to file
  • (alternate) curl http://example.com/filename

–compress

  • request server send compressed data.  curl decompresses data.

–limits-rate [#X]

  • Limits the download speed.  X can be K,M or G.

–max-filesize [size]

  • Checks file size prior to downloading. Wont download if exceeds max-filesize.

-T [data]

  • Upload data to server.

File URLs

file://hostname/path

hostname must be localhost or 127.0.0.1

 

Creating an Amazon Web Services Student Account

In this course we’ll be working in the cloud, in particular in Amazon’s cloud using Amazon Web Services (AWS).  We’ll take advantage of a program that Amazon offers to educators and students.  With their AWS Educate program, Amazon provides free tutorials and free credit for their AWS cloud services.

Join AWS Educate

To Join AWS Educate, navigate to Amazon Web Services (AWS) Educate and choose Join AWS Educate.

On Step 1/3, choose Student.

On Step 2/3, fill out the form.

    • Type Bridgewater College in the School or Institution Name field, AND choose Bridgewater College from the drop down menu.
    • Be sure to use your BC email address.
    • When complete, select Next.
    • On the next screen, check the box next to I agree, and press Submit.

For Step 3/3, AWS will send you an email.  Click the link in the email to verify your email address.

After your application has been processed and approved (which may take a day or two), you will receive a welcome email from AWS with a promo code in it. Keep that email as we’ll need the promo code in the lab.

Creating a Linux VM in the AWS Cloud

The AWS website contains various tutorials for setting up systems in the AWS cloud.  Complete Steps 1-4 in the following tutorial: Launch a Linux Virtual Machine.  Steps 1-4 show you how to create a Linux instance in a virtual machine and log into it via Terminal or the Git terminal.

Please do not perform Step 5.  Step 5 shows you how to terminate the Linux instance.  Note that when you terminate an instance, you delete the instance.  We don’t want to delete the instance.  We’ll keep the instance running for the entire semester.

If you run into problems, please let me know.

Managing Software on Your Amazon Linux Instance Using yum

Amazon Linux includes a program called yum which is used to maintain (add, remove, update) software packages on your instance.

Below are instructions for updating your system and for installing development tools that include the gcc compiler.

If you want to explore what else yum can do, below are links to the yum documentation.

Updating Your System

Login to your instance.

Change user to root.

$ sudo su

Check if there are updates that available.

$ yum check-update

Update the packages that have updates available.

$ yum update
Reboot the VM
$ shutdown -r now

Your ssh connection will be lost.  If you want to continue working on your server, wait 2 minutes or so, and then log back into your instance.

Installing Dev Tools

Install the Java compiler and development libraries.

$ yum install java-devel

Install all of the packages in the “Development Tools” group.  This includes various compilers.

$ yum groupinstall "Development Tools"

Create and Connect to a MySQL Database on AWS

The following notes were created while following the AWS tutorial titled Create and Connect to a MySQL Database found here.  They describe experiences that I had when following the tutorial. They may include things that I encountered that differed from what was described in the tutorial and other challenges.

Step 1: Create a MySQL DB Instance

I found the RDS (Relational Database Service) console by logging into the AWS console, choosing Services, and then navigating to the Database section.

The Amazon RDS console looks different from the image in the tutorial (see screenshot below).

I clicked the Create database button and on the next screen chose MySQL as the type and pressed Next.

Select engine (Step 1 of 3)

I chose MySQL and pressed Next.

Choose use case (Step 2 of 4)

I chose Dev/Test and pressed Next.

Specify DB details (Step 3 of 4)

  • In the blue box labeled Free tier I checked the box labeled Only enable options eligible for RDS Free Usage Tier.
  • At the bottom of the page in the section labeled Settings, I entered rds-mysql-test for the DB instance identifier.
  • I chose a Master username and Master password and pressed Next.

Configure advanced settings (Step 4 of 4)

For the Public accessibility setting, I chose Yes.

For the VPC security groups setting, I chose Choose existing VPC security groups and deleted the default security group, then chose the security group for used by my linux instance.

Under Database options I set the Database name to contacts.

Under Backup, I changed the Backup retention period to 1 day.

At the bottom of the page I pressed Create database.

I navigated to my Linux Instance in the AWS console, selected Security Groups, and added an Inbound rule to the security group that I chose above to allow MySQL connections.

Step 2: Download a SQL Client

On the MySQL download page, rather than creating an account, I selected No thanks, just start my download.

I installed the software.

Step 3: Connect to the MySQL Database

I chose Database > Connect to Database from the menu bar.

In my web browser I navigated to the RDS console, chose Databases in the side bar, and selected by database instance in the Databases table.

I used the Endpoint url of the database instance for the hostname, entered my Master username and Master password, and pressed OK.

I was able to connect and saw the following screen.

Configuring Android Studio on McKinney Lab Computers

Android Studio is installed on all of the PCs in McKinney 226, but in order to use it, it has to be configured when a user logs in. Here are the steps to take:

  1. Open Android studio, It will ask if you want to load configuration from another version, Click no and next.
  2. Click next on the welcome page
  3. Click custom install and next.
  4. Choose either the light or dark version and click next.
  5. UNcheck Performance(Intel HAXM), and click next.
  6. Click finish. Updates will download and install. It takes a few minutes.
  7. Click finish.  The program will start automatically and display the main menu.