SlideShare a Scribd company logo
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
$ cat > file.txt
Some random stuff...
^D
 Change where stdin comes from and stdout goes.
 End your line with > to redirect stdout to a file.
 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
 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.
 Turn the stdout of one program to the stdin of
another using a pipe |
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?)
BEGIN {FS=”:”}
BEGIN {OFS=”;”}
 Set output column seperator as semicolons:
 Divides each line into columns
 default separator is spaces
 Specify the separator between each column:
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
...
 Sum total memory
$ ls ­l | awk '{s+=$5} END{print s}' 
$ ls ­l | awk '$6=="Dec"&&$7=="12"{print $0}'
 Print only files from Dec 12
(last awk script)
 Replace all columns with their absolute value:
$ awk '{ for (i = 1; i <= NF; i++) 
         if ($i < 0) $i = ­$i; print $0}'
 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/
 So many one liners
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”
$ find . ­type f ( ­iname "*.sh" ­or 
                   ­iname "*.pl" )
 -iname for case insensitive search
 -type f finds only files and -type d only folders
 Example: find files ending with either 'sh' or '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
 -B prints lines before match
 -A prints lines after each match
 -C prints the lines before and after
 -i case insenstive search
 -v prints lines with no match
 -c prints just number of matches
 --color highlights matches
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/match-
ip-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 {}
$ find . ­name '*.dat' | xargs ­I{} cp 
{} /folder/
 Copy lots of files at once
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' 
http://en.wikipedia.org/wiki/xargs
 -t prints out the command before executing (for
debugging)
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 25
# one fish # 
# two fish # 
# red # 
# fish blue fish # 
# one fish two fish    # 
# red fish blue fish   # 
 par can fix your code comments
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/linux-
paste-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
 Prints the command to the screen
$ !comma
$ !comma:p
$ !comma
 Runs previous command beginning with
comma
Fun stuff
$ mkdir ­p /home/make/all/of/these/dirs/ 
$ cp /path/filename1 /path/filename2 
 Instead:
 Creates all subdirectories.
$ 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

More Related Content

What's hot

15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linuxTeja Bheemanapally
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
NB Veeresh
 
Format String
Format StringFormat String
Format String
Wei-Bo Chen
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
Nirajan Pant
 
Unit 6 bash shell
Unit 6 bash shellUnit 6 bash shell
Unit 6 bash shellroot_fibo
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
Raj vardhan
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
Tristan Penman
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
Prof. Wim Van Criekinge
 
Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3
MOHIT TOMAR
 
TDD in C - Recently Used List Kata
TDD in C - Recently Used List KataTDD in C - Recently Used List Kata
TDD in C - Recently Used List Kata
Olve Maudal
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
Utkarsh Sengar
 
Bioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekingeBioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekinge
Prof. Wim Van Criekinge
 
Unit 5 vim an advanced text editor
Unit 5 vim an advanced text editorUnit 5 vim an advanced text editor
Unit 5 vim an advanced text editorroot_fibo
 
Unix lab
Unix labUnix lab
Python basic
Python basicPython basic
Python basic
Saifuddin Kaijar
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introduction
ASIT Education
 
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square Learning
ASIT Education
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
Krasimir Berov (Красимир Беров)
 
Perl tutorial final
Perl tutorial finalPerl tutorial final
Perl tutorial final
Ashoka Vanjare
 
Fundamentals of Cryptography - Caesar Cipher - Python
Fundamentals of Cryptography - Caesar Cipher - Python Fundamentals of Cryptography - Caesar Cipher - Python
Fundamentals of Cryptography - Caesar Cipher - Python
Isham Rashik
 

What's hot (20)

15 practical grep command examples in linux
15 practical grep command examples in linux15 practical grep command examples in linux
15 practical grep command examples in linux
 
Introduction to Python , Overview
Introduction to Python , OverviewIntroduction to Python , Overview
Introduction to Python , Overview
 
Format String
Format StringFormat String
Format String
 
Grep - A powerful search utility
Grep - A powerful search utilityGrep - A powerful search utility
Grep - A powerful search utility
 
Unit 6 bash shell
Unit 6 bash shellUnit 6 bash shell
Unit 6 bash shell
 
UNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN CUNIT 4-HEADER FILES IN C
UNIT 4-HEADER FILES IN C
 
Generating parsers using Ragel and Lemon
Generating parsers using Ragel and LemonGenerating parsers using Ragel and Lemon
Generating parsers using Ragel and Lemon
 
Bioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introductionBioinformatica 29-09-2011-p1-introduction
Bioinformatica 29-09-2011-p1-introduction
 
Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3Header files of c++ unit 3 -topic 3
Header files of c++ unit 3 -topic 3
 
TDD in C - Recently Used List Kata
TDD in C - Recently Used List KataTDD in C - Recently Used List Kata
TDD in C - Recently Used List Kata
 
Python Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard WayPython Workshop - Learn Python the Hard Way
Python Workshop - Learn Python the Hard Way
 
Bioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekingeBioinformatics v2014 wim_vancriekinge
Bioinformatics v2014 wim_vancriekinge
 
Unit 5 vim an advanced text editor
Unit 5 vim an advanced text editorUnit 5 vim an advanced text editor
Unit 5 vim an advanced text editor
 
Unix lab
Unix labUnix lab
Unix lab
 
Python basic
Python basicPython basic
Python basic
 
Ruby programming introduction
Ruby programming introductionRuby programming introduction
Ruby programming introduction
 
Learn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square LearningLearn Ruby Programming in Amc Square Learning
Learn Ruby Programming in Amc Square Learning
 
IO Streams, Files and Directories
IO Streams, Files and DirectoriesIO Streams, Files and Directories
IO Streams, Files and Directories
 
Perl tutorial final
Perl tutorial finalPerl tutorial final
Perl tutorial final
 
Fundamentals of Cryptography - Caesar Cipher - Python
Fundamentals of Cryptography - Caesar Cipher - Python Fundamentals of Cryptography - Caesar Cipher - Python
Fundamentals of Cryptography - Caesar Cipher - Python
 

Viewers also liked

Software Engineering
Software EngineeringSoftware Engineering
Software EngineeringSanjay Saluth
 
SYSTEMS DESIGN / CAPSTONE PROJECT
SYSTEMS DESIGN / CAPSTONE PROJECTSYSTEMS DESIGN / CAPSTONE PROJECT
SYSTEMS DESIGN / CAPSTONE PROJECT
Sanjay Saluth
 
Telecommunication Network 1
Telecommunication Network 1Telecommunication Network 1
Telecommunication Network 1
Sanjay Saluth
 
Yamaha Manufacturing Plant
Yamaha Manufacturing PlantYamaha Manufacturing Plant
Yamaha Manufacturing Plant
Sanjay Saluth
 
Mettalurgy & heat treatment
Mettalurgy & heat treatmentMettalurgy & heat treatment
Mettalurgy & heat treatmentSanjay Saluth
 
Arithmetic circuits
Arithmetic circuitsArithmetic circuits
Arithmetic circuits
Sanjay Saluth
 
2s complement arithmetic
2s complement arithmetic2s complement arithmetic
2s complement arithmetic
Sanjay Saluth
 
Online Railway Reservation System
Online Railway Reservation SystemOnline Railway Reservation System
Online Railway Reservation System
Sanjay Saluth
 
Direct Memory Access
Direct Memory AccessDirect Memory Access
Direct Memory Access
Sanjay Saluth
 
Boss (Indian) Window
Boss (Indian) WindowBoss (Indian) Window
Boss (Indian) WindowSanjay Saluth
 

Viewers also liked (10)

Software Engineering
Software EngineeringSoftware Engineering
Software Engineering
 
SYSTEMS DESIGN / CAPSTONE PROJECT
SYSTEMS DESIGN / CAPSTONE PROJECTSYSTEMS DESIGN / CAPSTONE PROJECT
SYSTEMS DESIGN / CAPSTONE PROJECT
 
Telecommunication Network 1
Telecommunication Network 1Telecommunication Network 1
Telecommunication Network 1
 
Yamaha Manufacturing Plant
Yamaha Manufacturing PlantYamaha Manufacturing Plant
Yamaha Manufacturing Plant
 
Mettalurgy & heat treatment
Mettalurgy & heat treatmentMettalurgy & heat treatment
Mettalurgy & heat treatment
 
Arithmetic circuits
Arithmetic circuitsArithmetic circuits
Arithmetic circuits
 
2s complement arithmetic
2s complement arithmetic2s complement arithmetic
2s complement arithmetic
 
Online Railway Reservation System
Online Railway Reservation SystemOnline Railway Reservation System
Online Railway Reservation System
 
Direct Memory Access
Direct Memory AccessDirect Memory Access
Direct Memory Access
 
Boss (Indian) Window
Boss (Indian) WindowBoss (Indian) Window
Boss (Indian) Window
 

Similar to Unix Tutorial

awk_intro.ppt
awk_intro.pptawk_intro.ppt
awk_intro.ppt
PrasadReddy710753
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ewout2
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheetMartin Cabrera
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
Kasper de Waard
 
Scripting and the shell in LINUX
Scripting and the shell in LINUXScripting and the shell in LINUX
Scripting and the shell in LINUX
Bhushan Pawar -Java Trainer
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1Dr.Ravi
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
BITS
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell ScriptDr.Ravi
 
Handling Files Under Unix.pptx
Handling Files Under Unix.pptxHandling Files Under Unix.pptx
Handling Files Under Unix.pptx
Harsha Patel
 
Handling Files Under Unix.pptx
Handling Files Under Unix.pptxHandling Files Under Unix.pptx
Handling Files Under Unix.pptx
Harsha Patel
 
Love Your Command Line
Love Your Command LineLove Your Command Line
Love Your Command Line
Liz Henry
 
Shell Scripts
Shell ScriptsShell Scripts
Shell ScriptsDr.Ravi
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
Leandro Lima
 
COMMAND.pptx
COMMAND.pptxCOMMAND.pptx
COMMAND.pptx
4AL20CS071MAYUR
 

Similar to Unix Tutorial (20)

awk_intro.ppt
awk_intro.pptawk_intro.ppt
awk_intro.ppt
 
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaaShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux
LinuxLinux
Linux
 
Linux com
Linux comLinux com
Linux com
 
Hex file and regex cheat sheet
Hex file and regex cheat sheetHex file and regex cheat sheet
Hex file and regex cheat sheet
 
Cheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regexCheatsheet: Hex file headers and regex
Cheatsheet: Hex file headers and regex
 
Scripting and the shell in LINUX
Scripting and the shell in LINUXScripting and the shell in LINUX
Scripting and the shell in LINUX
 
Talk Unix Shell Script 1
Talk Unix Shell Script 1Talk Unix Shell Script 1
Talk Unix Shell Script 1
 
Text mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformaticsText mining on the command line - Introduction to linux for bioinformatics
Text mining on the command line - Introduction to linux for bioinformatics
 
Talk Unix Shell Script
Talk Unix Shell ScriptTalk Unix Shell Script
Talk Unix Shell Script
 
Handling Files Under Unix.pptx
Handling Files Under Unix.pptxHandling Files Under Unix.pptx
Handling Files Under Unix.pptx
 
Handling Files Under Unix.pptx
Handling Files Under Unix.pptxHandling Files Under Unix.pptx
Handling Files Under Unix.pptx
 
Love Your Command Line
Love Your Command LineLove Your Command Line
Love Your Command Line
 
Shell Scripts
Shell ScriptsShell Scripts
Shell Scripts
 
Slides
SlidesSlides
Slides
 
Workshop on command line tools - day 1
Workshop on command line tools - day 1Workshop on command line tools - day 1
Workshop on command line tools - day 1
 
COMMAND.pptx
COMMAND.pptxCOMMAND.pptx
COMMAND.pptx
 

Recently uploaded

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
AafreenAbuthahir2
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
MdTanvirMahtab2
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
Neometrix_Engineering_Pvt_Ltd
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
ankuprajapati0525
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
gerogepatton
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
zwunae
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
WENKENLI1
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
TeeVichai
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
manasideore6
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
gdsczhcet
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
JoytuBarua2
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Teleport Manpower Consultant
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
fxintegritypublishin
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
Osamah Alsalih
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
AmarGB2
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
SamSarthak3
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
SupreethSP4
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation & Control
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
R&R Consult
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
BrazilAccount1
 

Recently uploaded (20)

WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234WATER CRISIS and its solutions-pptx 1234
WATER CRISIS and its solutions-pptx 1234
 
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
Industrial Training at Shahjalal Fertilizer Company Limited (SFCL)
 
Standard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - NeometrixStandard Reomte Control Interface - Neometrix
Standard Reomte Control Interface - Neometrix
 
The role of big data in decision making.
The role of big data in decision making.The role of big data in decision making.
The role of big data in decision making.
 
Immunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary AttacksImmunizing Image Classifiers Against Localized Adversary Attacks
Immunizing Image Classifiers Against Localized Adversary Attacks
 
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
一比一原版(IIT毕业证)伊利诺伊理工大学毕业证成绩单专业办理
 
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdfGoverning Equations for Fundamental Aerodynamics_Anderson2010.pdf
Governing Equations for Fundamental Aerodynamics_Anderson2010.pdf
 
Railway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdfRailway Signalling Principles Edition 3.pdf
Railway Signalling Principles Edition 3.pdf
 
Fundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptxFundamentals of Electric Drives and its applications.pptx
Fundamentals of Electric Drives and its applications.pptx
 
Gen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdfGen AI Study Jams _ For the GDSC Leads in India.pdf
Gen AI Study Jams _ For the GDSC Leads in India.pdf
 
Planning Of Procurement o different goods and services
Planning Of Procurement o different goods and servicesPlanning Of Procurement o different goods and services
Planning Of Procurement o different goods and services
 
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdfTop 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
Top 10 Oil and Gas Projects in Saudi Arabia 2024.pdf
 
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdfHybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
Hybrid optimization of pumped hydro system and solar- Engr. Abdul-Azeez.pdf
 
MCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdfMCQ Soil mechanics questions (Soil shear strength).pdf
MCQ Soil mechanics questions (Soil shear strength).pdf
 
Investor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptxInvestor-Presentation-Q1FY2024 investor presentation document.pptx
Investor-Presentation-Q1FY2024 investor presentation document.pptx
 
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdfAKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
AKS UNIVERSITY Satna Final Year Project By OM Hardaha.pdf
 
Runway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptxRunway Orientation Based on the Wind Rose Diagram.pptx
Runway Orientation Based on the Wind Rose Diagram.pptx
 
Water Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdfWater Industry Process Automation and Control Monthly - May 2024.pdf
Water Industry Process Automation and Control Monthly - May 2024.pdf
 
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptxCFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
CFD Simulation of By-pass Flow in a HRSG module by R&R Consult.pptx
 
AP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specificAP LAB PPT.pdf ap lab ppt no title specific
AP LAB PPT.pdf ap lab ppt no title specific
 

Unix Tutorial

  • 1. Unix Tutorial By Joshua Lande SASS January 21
  • 2. 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)
  • 3. 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
  • 4. 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
  • 5. 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/
  • 6. 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
  • 7. input/output redirection $ cat > file.txt Some random stuff... ^D  Change where stdin comes from and stdout goes.  End your line with > to redirect stdout to a file.  Use >> to append to a file  Use < to read stdin from a file. $ cat < file.txt Some random stuff...
  • 8. pipes $ cat *.txt | sort | uniq > output.txt  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.  Turn the stdout of one program to the stdin of another using a pipe |
  • 9. 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
  • 10. 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
  • 11. awk (again?) BEGIN {FS=”:”} BEGIN {OFS=”;”}  Set output column seperator as semicolons:  Divides each line into columns  default separator is spaces  Specify the separator between each column:
  • 12. 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 ...  Sum total memory $ ls ­l | awk '{s+=$5} END{print s}'  $ ls ­l | awk '$6=="Dec"&&$7=="12"{print $0}'  Print only files from Dec 12
  • 13. (last awk script)  Replace all columns with their absolute value: $ awk '{ for (i = 1; i <= NF; i++)           if ($i < 0) $i = ­$i; print $0}'  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/  So many one liners
  • 14. 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
  • 15. 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
  • 16. find (stuff quickly)  Syntax: find path expression  Searches recursively through all subfolders $ find /path/ ­name ”file.txt” $ find . ­type f ( ­iname "*.sh" ­or                     ­iname "*.pl" )  -iname for case insensitive search  -type f finds only files and -type d only folders  Example: find files ending with either 'sh' or 'pl':  Use a to continue a long line
  • 17. 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}'
  • 18. grep $ grep ­B2 $ grep ­A4 $ grep ­C3  -B prints lines before match  -A prints lines after each match  -C prints the lines before and after  -i case insenstive search  -v prints lines with no match  -c prints just number of matches  --color highlights matches
  • 19. 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/match- ip-range-using-egrep-bash.html
  • 20. 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
  • 21. 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 {} $ find . ­name '*.dat' | xargs ­I{} cp  {} /folder/  Copy lots of files at once
  • 22. 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.
  • 23. xargs (to the rescue)  Example: run cvs update in all subfolders: find . ­type d | xargs ­i ­t sh ­c                     'cd {};cvs update'  http://en.wikipedia.org/wiki/xargs  -t prints out the command before executing (for debugging)
  • 24. 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...
  • 26. 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/linux- paste-command-good-examples-uses.html
  • 27. 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
  • 28. Lazy history  Prints the command to the screen $ !comma $ !comma:p $ !comma  Runs previous command beginning with comma
  • 29. Fun stuff $ mkdir ­p /home/make/all/of/these/dirs/  $ cp /path/filename1 /path/filename2   Instead:  Creates all subdirectories. $ cp /path/filename{1,2} $ mkdir foo1 foo2 foo3 bar1 bar2 bar3 $ mkdir {foo, bar}{1, 2, 3}     
  • 30. 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?