Advanced Lab 4

CS 2110

Spring 2015

tux

Learning Linux

Shameless Plug

Come to LUG!


Wednesdays 7:00 Klaus 1456

What is Linux?

Linux is an open source kernel.


A kernel is the core of an OS that manages devices and io.


Ubuntu is a Linux distribution (or distro).


A distro is a collection of software which includes a kernel and other programs.

Why use Linux?

  • Powerful
  • Fun
  • Configurable
  • Employers like Linux experience (shell)
  • Required for this class

Linux File System

Personal Files


Unix/Linux

/home/user

Windows

C:\Documents and Settings\User

Linux File System

Program Files


Unix/Linux

  • /bin
  • /sbin
  • /usr/bin
  • /usr/sbin
  • /usr/local/bin
  • /usr/local/sbin

Windows

C:\Program Files

Linux doesn't have just one spot for this.

Linux Filesystem Hierarchy System


/
|-- bin         #system binaries
|-- boot        #stuff for booting
|-- dev	        #device files
|-- etc         #config files
|-- home        #user files
|-- lib	        #shared libraries
|-- lib64       #64 bit shared libraries
|-- mnt         #mounted file systems
|-- opt         #optional software
|-- proc        #process information (virtual file system)
|-- root        #root user home directory
|-- run         #info about running system
|-- sbin        #system binaries
|-- srv         #files to be served
|-- tmp         #temporary files
|-- usr         #second file system hierarchy
`-- var         #variable files - logs
                

You can read more about the FHS standards at wikipedia.

What's the difference between:

  • Terminal
  • Shell

List of Common Shells

Posix Shells

  • Borne Shell (sh) - 1977
  • Bash - 1989
  • Zsh - 1990

Windows Shells

  • COMMAND.COM - 1981
  • cmd.exe - 1993
  • PowerShell - 2006

Navigating using the shell


linus@host:~$ cd Documents/Folder
                

The cd command changes the current directory.

List directory contents

Unix/Linux


linus@host:~$ ls
Documents Downloads Desktop
                

Getting Command Help


linus@host:~$ ls --help
            

Man, I wish I knew how to use ... grep , find , and git.


Well now you can learn!


man command
              

Also run man intro for a brief Linux intro.

Time to Learn Some Basic Commands!

Create a Directory

mkdir directory

Create an Empty File

touch file

Text Editors

gedit file.txt

If you want to learn vim run vimtutor for an intro.

Printing Files (to the screen)

cat file

Be Careful

The following commands can overwrite your files if used improperly.

Deleting Files

rm file

Deleting Directories

Be careful there is no recycle bin!
rm -rf directory

Renaming Files

mv old new

Copying Files

cp source dest

Symbolic Links (Shortcuts)

ln -s target linkname

File Permissions

Making a file executable

chmod +x program

Useful Commands for this class

apt-get

You will use apt-get to install software on Ubuntu.
Other Linux distros might use something else.

Searching packages

apt-cache search program

Installing a program

sudo apt-get install program

Update package list

sudo apt-get update

Update all packages

sudo apt-get upgrade

tar

Tar is program that compresses files into a .tar.gz. We will often give you tar.gz files and ask you to turn them in as well.

Compress

tar czvf archive.tar.gz list of files

Compress Ze Various Files

Extract

tar xzvf archive.tar.gz

Extract Ze Various Files

Job Control

Allows you to run multiple programs from the same terminal.

Signals

Most terminal emulators support sending to a signal to a running process. This is what happens when you press Ctrl-C

Background Processes

This is more fun if you follow along on your own shell.


user@~$ sleep 2 # wait 2 seconds
user@~$ sleep 200 & # runs the program in the background
user@~$ sleep 100 # who wants to wait that long (presses ctrl-z)
^Z
user@~$ jobs
[1]-  Running                 sleep 200 &
[2]+  Stopped                 sleep 100
user@~$ bg
user@~$ jobs
[1]-  Running                 sleep 200 &
[2]+  Running                 sleep 100 &
user@~$ fg 1

            

Wildcards and Globbing

How you can be faster on the command line.

Let's say your in a directory and need to delete all the .o files but you want to keep all of the other files.


rm -i *.o
            
Don't add the -i if you don't want it to ask you if you are sure.

But what is happening here?

Let's write a quick java program to find out!

Echo.java


public class Echo {
    public static void main(String[] argv) {
        System.out.println(java.util.Arrays.toString(argv));
    }
}

To run and compile:


javac Echo.java
java Echo *
            

The shell expands words with * to all filenames with a matching regular expression.

Pipes and Redirection

Pipes are a way you can use the output of one command as the input for another.

This is one of the reasons people love the command line.

Special Characters

  • > Send stdout to a file.
  • >> Append stdout to a file.
  • | Send stdout of a program to stdin of another.

Filters

Filters are programs that process text.


List of common filters:

  • tr
  • sed
  • grep

Example Pipe


fortune | cowsay
              

You can stick this at the bottom of your ~/.bashrc

Important Files and Folders

  • Hidden Files start with a .
  • ~ (home directory)
  • ~/.bashrc (bash config file)
  • /dev/null (null device)

Obsure Bash Stuff

Bash features you don't need know about.

Keybindings

Binding Action
C-a Move cursor to start of line
C-e Move cursor to end of line
C-u Cut text before cursor
C-k Cut text after cursor
C-y Paste text from shell

Aliasing

Don't get slowed down by your typing mistakes.


alias sl="ls"
            

Also alias long commands to save keystrokes.


alias a="sudo apt-get install"
            

Bang Commands


echo why hello there
why hello there
echo !:2
hello
            
See more obscure ones here

Edit and Execute Command

Pressing ctrl-x ctrl-e will open vim for quickly creating a command. When you save and quit the command will be executed.

Fun Linux Stuff

LUG Scripts

Resources