Unix Tutorial. By Joshua Lande SASS. January 21



Similar documents
ICS 351: Today's plan

A Crash Course on UNIX

Basic C Shell. helpdesk@stat.rice.edu. 11th August 2003

AN INTRODUCTION TO UNIX

grep, awk and sed three VERY useful command-line utilities Matt Probert, Uni of York grep = global regular expression print

Linux command line. An introduction to the Linux command line for genomics. Susan Fairley

Cygwin command line windows. Get that Linux feeling - on Windows

Hands-On UNIX Exercise:

SEO - Access Logs After Excel Fails...

Advanced Bash Scripting. Joshua Malone

Unix Scripts and Job Scheduling

Networks. Inter-process Communication. Pipes. Inter-process Communication

Command Line - Part 1

CPSC2800: Linux Hands-on Lab #3 Explore Linux file system and file security. Project 3-1

A UNIX/Linux in a nutshell

Unix the Bare Minimum

Thirty Useful Unix Commands

Shell Scripts (1) For example: #!/bin/sh If they do not, the user's current shell will be used. Any Unix command can go in a shell script

Cisco Networking Academy Program Curriculum Scope & Sequence. Fundamentals of UNIX version 2.0 (July, 2002)

Lecture 18 Regular Expressions

PHP Debugging. Draft: March 19, Christopher Vickery

Open Source Computational Fluid Dynamics

Command Line Crash Course For Unix

UNIX, Shell Scripting and Perl Introduction

Introduction to Operating Systems

Unix Sampler. PEOPLE whoami id who

CS Unix Tools & Scripting Lecture 9 Shell Scripting

CS2720 Practical Software Development

This presentation explains how to monitor memory consumption of DataStage processes during run time.

An Introduction to the Linux Command Shell For Beginners

Introduction to Shell Programming

Birmingham Environment for Academic Research. Introduction to Linux Quick Reference Guide. Research Computing Team V1.0

Introduction to Git. Markus Kötter Notes. Leinelab Workshop July 28, 2015

Extreme computing lab exercises Session one

L01 Introduction to the Unix OS

USEFUL UNIX COMMANDS

CTIS486 Midterm I 20/11/ Akgül

CS 103 Lab Linux and Virtual Machines

HP-UX Essentials and Shell Programming Course Summary

Lecture 22 The Shell and Shell Scripting

Introduction to Mac OS X

Introduction to Shell Scripting

Tutorial 0A Programming on the command line

XSLT File Types and Their Advantages

UNIX / Linux commands Basic level. Magali COTTEVIEILLE - September 2009

INASP: Effective Network Management Workshops

Setting up Auto Import/Export for Version 7

Lab 1: Introduction to the network lab

awk A UNIX tool to manipulate and generate formatted data

University of Pennsylvania Department of Electrical and Systems Engineering Digital Audio Basics

Introduction to Laboratory Works

Using Parallel Computing to Run Multiple Jobs

Running your first Linux Program

Linux Crontab: 15 Awesome Cron Job Examples

IBM Redistribute Big SQL v4.x Storage Paths IBM. Redistribute Big SQL v4.x Storage Paths

Secure File Transfer Installation. Sender Recipient Attached FIles Pages Date. Development Internal/External None 11 6/23/08

CSIL MiniCourses. Introduction To Unix (I) John Lekberg Sean Hogan Cannon Matthews Graham Smith. Updated on:

Partek Flow Installation Guide

AWK: The Duct Tape of Computer Science Research. Tim Sherwood UC Santa Barbara

Extreme Computing. Hadoop. Stratis Viglas. School of Informatics University of Edinburgh Stratis Viglas Extreme Computing 1

Linux System Administration on Red Hat

An Introduction to Using the Command Line Interface (CLI) to Work with Files and Directories

STEP 4 : GETTING LIGHTTPD TO WORK ON YOUR SEAGATE GOFLEX SATELLITE

Introduction to Programming and Computing for Scientists

UFTP AUTHENTICATION SERVICE

Lecture 25 Systems Programming Process Control

CS 2112 Lab: Version Control

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Linux Syslog Messages in IBM Director

CrontabFile Converter

Running Hadoop on Windows CCNP Server

Answers to Even- Numbered Exercises

JobScheduler - Quickstart

List of FTP commands for the Microsoft command-line FTP client

Fred Hantelmann LINUX. Start-up Guide. A self-contained introduction. With 57 Figures. Springer

CS10110 Introduction to personal computer equipment

Grid 101. Grid 101. Josh Hegie.

Advanced UNIX Techniques Lee Ann Lee Revised by Wendy Kajiyama

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

An Introduction to Using the Command Line Interface (CLI) to Work with Files and Directories

Installation Guide for WebSphere Application Server (WAS) and its Fix Packs on AIX V5.3L

Using SVN to Manage Source RTL

Quick Tutorial for Portable Batch System (PBS)

dotdefender v5.12 for Apache Installation Guide Applicure Web Application Firewall Applicure Technologies Ltd. 1 of 11 support@applicure.

GNU Parallel. The command line power tool. LUGA Ole Tange

Hadoop Hands-On Exercises

Installing IBM Websphere Application Server 7 and 8 on OS4 Enterprise Linux

CPSC 2800 Linux Hands-on Lab #7 on Linux Utilities. Project 7-1

Lecture 4. Regular Expressions grep and sed intro

Syntax: cd <Path> Or cd $<Custom/Standard Top Name>_TOP (In CAPS)

Beginners Shell Scripting for Batch Jobs

An A-Z Index of the Apple OS X command line (TERMINAL) The tcsh command shell of Darwin (the open source core of OSX)

Hadoop Basics with InfoSphere BigInsights

Linux Bash Shell Cheat Sheet

Hadoop Hands-On Exercises

VI(Visual) Editor Reference manual

1 Basic commands. 2 Terminology. CS61B, Fall 2009 Simple UNIX Commands P. N. Hilfinger

How to configure the DBxtra Report Web Service on IIS (Internet Information Server)

Hadoop Streaming. Table of contents

JAVS Scheduled Publishing. Installation/Configuration... 4 Manual Operation... 6 Automating Scheduled Publishing... 7 Windows XP... 7 Windows 7...

Transcription:

Unix Tutorial By Joshua Lande SASS January 21

This is not a philosophy talk! Doug McIlroy, the inventor of Unix pipes and one of the founders of the Unix tradition, summarized the philosophy as follows: This is the Unix philosophy: Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface. (http://en.wikipedia.org/wiki/unix_philosophy)

The Basics All command line programs have 3 main components: Command line arguments Standard Input (stdin) Standard Output (stdout) By default, stdin is typed from the terminal and stdout is printed to the terminal for help on any command: $ man command

A few basic programs echo sends the command line arguments to stdout cat reads file(s) as command line arguments and sends the lines to stdout. If no files specified, sends stdin to stdout. tac Just like cat but backwards tee writes the input both to the stdout and to a file specified as a command line argument

Example $ sed 's/lame/awesome/g' This example is lame This example is awesome ^D sed replaces the first word with the second word 's/lame/awesome/g' is a command line argument First line is the stdin (I typed) Second line is the stdout (printed to screen) When you are done sending stuff to stdin, type CTRL-D and the program will finish up. http://www.catonmat.net/blog/sed-one-liners-explained-part-one/

Sorting $ sort t ":" n k2 Ted:1000 John:1 Sally:100 Bob:10 John:1 Bob:10 Sally:100 Ted:1000 Sort is a program to sort the lines of standard input -t specifies the field seperator -n means numeric sort -k2 means sort the second column

input/output redirection Change where stdin comes from and stdout goes. End your line with > to redirect stdout to a file. $ cat > file.txt Some random stuff... ^D Use >> to append to a file Use < to read stdin from a file. $ cat < file.txt Some random stuff...

pipes $ cat *.txt sort uniq > output.txt Turn the stdout of one program to the stdin of another using a pipe In this example, cat outputs all text files, which are sorted. All dupicates are than removed and the output is saved to a file. $ somecommand tee output.txt Prints output of a command to stdout and a file! $ somecommand less Pipe to less for nice navigation.

awk Powerful programming language Easy to whip up powerful scripts The general syntax is an expression followed by a command. loops over stdin Example: second column if the first column is a number greater than 10 $ awk '$1>10{print $2}' file.txt

awk (more?) Put code you want to run before or after inside BEGIN and END blocks. Example: count number of occurrences in file: $ awk 'BEGIN {print "Analysis:" } /foo/{++foobar } END {print "foo appears " foobar " times." }' file

awk (again?) Divides each line into columns default separator is spaces Specify the separator between each column: BEGIN {FS= : } Set output column seperator as semicolons: BEGIN {OFS= ; }

awk $ ls l drwxr xr x 3 lande gl 2048 Dec 12 19:21 bin drwx 2 lande gl 4096 Nov 20 15:59 mail... Print only files from Dec 12 $ ls l awk '$6=="Dec"&&$7=="12"{print $0}' Sum total memory $ ls l awk '{s+=$5} END{print s}'

(last awk script) Replace all columns with their absolute value: $ awk '{ for (i = 1; i <= NF; i++) if ($i < 0) $i = $i; print $0}' So many one liners http://www.catonmat.net/blog/awk-one-liners-explained-part-one/ http://www.catonmat.net/blog/awk-one-liners-explained-part-two/ http://www.catonmat.net/blog/awk-one-liners-explained-part-three/

Job Control Control-z suspends a currently running job The jobs command shows you all the jobs running in the terminal $ jobs [1] Stopped yes [2]+ Stopped yes Each job given a number. Run the second job in the background or foreground: $ bg 2 $ fg 2

Job Control Begin job in the background $ command & List all jobs running on your machine: $ ps u lande PID TTY TIME CMD 19231 pts/21 00:00:00 vim 19233 pts/21 00:00:00 find Kill any job (by PID or name) $ kill 19231 $ killall find

find (stuff quickly) Syntax: find path expression Searches recursively through all subfolders $ find /path/ name file.txt -iname for case insensitive search -type f finds only files and -type d only folders Example: find files ending with either 'sh' or 'pl': $ find. type f \( iname "*.sh" or \ iname "*.pl" \) Use a \ to continue a long line

grep (is beautiful) Search through stdin for things Sends to stdout lines matched lines $ grep tacos this line has tacos this line has tacos this line dosen't more tacos more tacos You can do the same in awk with $ awk '/tacos/{print $0}'

grep $ grep B2 $ grep A4 $ grep C3 -i case insenstive search -v prints lines with no match -B prints lines before match -A prints lines after each match -c prints just number of matches --color highlights matches -C prints the lines before and after

grep Fancy regular expressions: -E Example: Match IP range from 172.22.21.1 to 172.22.21.35: $ grep E '172\.22\.21\.([1 9] (1[0 9] 2[0 9] 3[0 5])) ' hosts.txt http://unstableme.blogspot.com/2008/07/matchip-range-using-egrep-bash.html

xargs Makes stdin as a command line argument useful for running a command a bunch of times Example: Search in all files for a variable name $ find. name *.cxx xargs grep var This is equivalent to running grep on all *.cxx files in all subdirectories. $ grep *.cxx The above would only search files in current directory

xargs (is xtreme) Use -I{} to replace all occurrences of {} in the command with the standard input. Example (I use all the time): Run all the scripts in all subdirectories $ find. name "*.sh" xargs I{} sh {} Copy lots of files at once $ find. name '*.dat' xargs I{} cp {} /folder/

Too many jobs running? Kill all jobs running in terminal jobs p xargs i kill 9 {} jobs -p prints all job IDs. kill -9 kills the job with that ID.

xargs (to the rescue) Example: run cvs update in all subfolders: find. type d xargs i t sh c \ 'cd {};cvs update' -t prints out the command before executing (for debugging) http://en.wikipedia.org/wiki/xargs

par Reformats text Not installed by default but easy to build. $ par 30j We the people of the United States, in order to form a more perfect union, establish justice... We the people of the United States, in order to form a more perfect union, establish justice...

par (cont) par can fix your code comments $ par 25 # one fish # # two fish # # red # # fish blue fish # # one fish two fish # # red fish blue fish # http://www.nicemice.net/par/

paste $ cat f1.txt a b c $ cat f2.txt 1 2 $ paste f1.txt f2.txt a 1 b 2 c http://unstableme.blogspot.com/2009/01/linuxpaste-command-good-examples-uses.html

Various stuff Go to previous folder: $ cd Get the previous command: $ file.txt bash: file.txt: command not found $ echo!!!$ is the last part of the last command

Lazy history $!comma Runs previous command beginning with comma $!comma:p Prints the command to the screen

Fun stuff Creates all subdirectories. $ mkdir p /home/make/all/of/these/dirs/ $ cp /path/filename1 /path/filename2 Instead: $ cp /path/filename{1,2} $ mkdir foo1 foo2 foo3 bar1 bar2 bar3 $ mkdir {foo, bar}{1, 2, 3}

Guess who? Who is on your machine send them a message $ who lande pts/16 Jan 21 01:34 (rescomp 07 119188.stanford.edu)... $ write lande What's up?

Questions