pyladies talk: learn to love the command line!

39
LEARN TO LOVE THE COMMAND LINE Thursday, 19 February, 2015 Google Montreal Shopify Montreal

Upload: blanca-mancilla

Post on 07-Aug-2015

148 views

Category:

Software


1 download

TRANSCRIPT

Page 1: PyLadies Talk: Learn to love the command line!

LEARN TO LOVE THECOMMAND LINE

Thursday, 19 February, 2015

Google MontrealShopify Montreal

Page 2: PyLadies Talk: Learn to love the command line!

IN LOVE WITH...THE COMMAND LINE

/ /

http://cli-talk.bitballoon.com

Blanca Mancilla bluciam @Blanckus

Page 4: PyLadies Talk: Learn to love the command line!

LinuxCommand.org

You have Linux installed and running. TheGUI is working fine, but you are gettingtired of changing your desktop themes.You keep seeing this "terminal" thing.

Page 5: PyLadies Talk: Learn to love the command line!

WHAT IS THE COMMANDLINE?

Page 6: PyLadies Talk: Learn to love the command line!

Anybody with little or no experience?What do you know about it?

What are you expecting to learn tonight?

WHAT IS THE COMMANDLINE?

Page 7: PyLadies Talk: Learn to love the command line!

Specifically, the computer program is the operatingsystem (UNIX or nowadays Linux) and the interface is

what we called the .

THE COMMAND LINEAND THE SHELL

A command-line interface (CLI) is a means of interacting with a computer program where the user issues commands to the program in the form of successive lines of text (command lines).

https://en.wikipedia.org/wiki/Command-line_interface

shell

Page 8: PyLadies Talk: Learn to love the command line!

TYPES OF SHELLS IN LINUXshbashcsh and tcshkshread more...

Page 9: PyLadies Talk: Learn to love the command line!

HOW DO I KNOW WHICHSHELL?

~ $ w h i c h b a s h/ b i n / b a s h~ $ w h i c h s h/ b i n / s h~ $ w h i c h c s h~ $ e c h o $ S H E L L/ b i n / b a s h~ $ s h$ e c h o $ S H E L L/ b i n / b a s h$ p s - p $ $ P I D T T Y T I M E C M D1 7 7 9 3 p t s / 2 8 0 0 : 0 0 : 0 0 s h$ ^ D~ $ p s - p $ $ P I D T T Y T I M E C M D1 5 6 1 5 p t s / 2 8 0 0 : 0 0 : 0 1 b a s h

Page 10: PyLadies Talk: Learn to love the command line!

THE INSPIRATION

Page 11: PyLadies Talk: Learn to love the command line!

by Jeroen JanssensData Science at the Command Line

Page 12: PyLadies Talk: Learn to love the command line!

—Chris H. WigginsAssociate Professor in the Department of Applied Physics

and Applied Mathematics at Columbia Universityand Chief Data Scientist at The New York Times

The Unix philosophy of simple tools, eachdoing one job well, then cleverly pipedtogether, is embodied by the commandline. Jeroen expertly discusses how to

bring that philosophy into your work indata science, illustrating how the

command line is not only the world of fileinput/ output, but also the world of data

manipulation, exploration, and evenmodeling.

Page 13: PyLadies Talk: Learn to love the command line!

APPENDIX: COMMANDSalias awk aws bash bc bigmler body cat cd chmod colscowsay cp csvcut csvgrep csvjoin csvlook csvsort csvsql

csvstack csvstat curl curlicue cut display drake dseq echoenv export feedgnuplot fieldsplit find for git grep headheader in2csv jq json2csv less ls man mkdir mv parallel

paste pbc pip pwd python R Rio Rio-scatter rm

Page 14: PyLadies Talk: Learn to love the command line!

SOME DETAILSNOW!

Page 15: PyLadies Talk: Learn to love the command line!

Yes! Of course.

FOR LOOPS?

f o r i i n * p d f ; d o e v i n c e $ i ; d o n e

Page 16: PyLadies Talk: Learn to love the command line!

FOR LOOPS: l y c e u m $ l s * p d fH o w T o B e A P r o g r a m m e r . p d f S E N G 3 0 2 0 - 0 . 1 . p d f t e a c h i n g - p r g m . p d fS E N G 3 0 1 0 - B A R T - R e q - v 1 - 1 - 3 d . p d f S E N G 3 0 2 0 _ 0 7 s 2 . p d f: l y c e u m $ e v i n c e * p d f: l y c e u m $ f o r i i n * p d f ; d o e v i n c e $ i ; d o n e

Page 17: PyLadies Talk: Learn to love the command line!

or even

FOR LOOPSIn a script

f o r i i n * p d fd o e v i n c e $ id o n e

f o r i i n ` l s * p d f `d o e v i n c e $ id o n e

More info

Page 18: PyLadies Talk: Learn to love the command line!

CHECK IF A FILE EXISTS ANDIF NOT ...

c l i - t a l k : $ [ - f i n d e x . h t m l ] & & e c h o " Y e s , w e h a v e a p r e s e n t a t i o n ! " \| | e c h o " P A N I C "

Page 19: PyLadies Talk: Learn to love the command line!

CHECK IF A FILE EXISTS ANDIF NOT ...

Page 20: PyLadies Talk: Learn to love the command line!

Let's try a demo

CHECK IF A FILE EXISTS ANDIF NOT ...

c l i - t a l k : $ l o c a t e * 9 7 6 8 . J P G

Page 21: PyLadies Talk: Learn to love the command line!

GLOB PATTERNS:REAL MAGIC!

Strictly speaking, there are neither regular expressionsnor patterns used in pattern recognition.

They are .glob patterns

Page 22: PyLadies Talk: Learn to love the command line!

GLOB PATTERNS:REAL MAGIC!

l s a p p / * . { e r b , s c s s , r b , y m l }

v i p u b l i c _ h t m l / [ i I ] * . h t m *

Page 23: PyLadies Talk: Learn to love the command line!

My favorite: Move all image files for the years 2010 to 2012 into .

GLOB PATTERNS:REAL MAGIC!

my_dir

m v { I M G , i m g } * 2 0 1 [ 0 1 2 ] * . { j p g , J P G , p n g , g i f } m y _ d i r

Page 24: PyLadies Talk: Learn to love the command line!

FIND A RESOURCEANYWHERE IN YOUR HARD

DRIVE

Page 25: PyLadies Talk: Learn to love the command line!

versus

FIND A RESOURCE ...locate find

L o r i B i z : $ l o c a t e * . e r bL o r i B i z : $ f i n d . - n a m e * . e r b

Page 26: PyLadies Talk: Learn to love the command line!

find . -name *.erb -exec ls {} \;

find . -name *.erb -exec grep mytableauthor {} \;

find . -name *.erb -exec grep -H mytableauthor {} \;

find . -name *.scss -exec grep -H mytableauthor {} \;

find . -name *.scss -exec grep -n -H mytableauthor {} \;

FIND A RESOURCE...

Page 27: PyLadies Talk: Learn to love the command line!

FIND AND REPLACE TRAILINGSPACES IN SOURCE CODE

Page 28: PyLadies Talk: Learn to love the command line!

FIND ... TRAILING SPACES INSOURCE CODE

c l i - t a l k $ g r e p " \ s \ + $ " i n d e x . h t m l

Page 29: PyLadies Talk: Learn to love the command line!

... REPLACE TRAILING SPACESIN SOURCE CODE

L o r i B i z $ f i n d . - n a m e * . e r b - e x e c g r e p - H n " \ s \ + $ " { } \ ; L o r i B i z $ f i n d . - n a m e * . e r b - e x e c g r e p - H n " \ s \ + $ " { } \ ; - e x e c v i { } \ ;

# O n c e i n v i f i n d a n d r e p l a c e u s i n g t h e c o m m a n d: % s / \ s \ + $ / /

Page 30: PyLadies Talk: Learn to love the command line!

MAKING BACKUPS OFGIT REPOSITORIESsuperspreadsheet.wordpress.com

Page 31: PyLadies Talk: Learn to love the command line!

MAKING BACKUPS OF ...1. pull from the bare repository2. commit local changes3. push changes to the bare repository

Page 32: PyLadies Talk: Learn to love the command line!

Pull and Commit

MAKING BACKUPS OF ...

1 # ! / b i n / s h 2 # 3 # A u t o m a t i c p u l l a n d c o m m i t o n t h e d i r e c t o r i e s l i s t e d i n d i r s _ t o _ p u l l 4 5 C R O N L O G = " / c o m p l e t e / p a t h / t o / p u l l c o m m i t p u s h . l o g " 6 C O M M I T M S G = " / c o m p l e t e / p a t h / t o / m s g A u t o C o m m i t " 7 e c h o > > $ C R O N L O G 8 e c h o " N i g h t l y g i t a u t o - c o m m i t f o r t h e a b s e n t m i n d s : $ ( d a t e ) " > > $ C R O N L O G 9 f o r i i n ` c a t / c o m p l e t e / p a t h / t o / d i r s _ t o _ p u l l `1 0 d o ( c d $ i ;1 1 e c h o " P U L L $ i $ ( d a t e ) " > > $ C R O N L O G ;1 2 g i t p u l l > > $ C R O N L O G 2 > & 1 ;1 3 e c h o " C O M M I T $ i $ ( d a t e ) " > > $ C R O N L O G ;1 4 g i t c o m m i t - a - F $ C O M M I T M S G > > $ C R O N L O G 2 > & 11 5 e c h o > > $ C R O N L O G )1 6 d o n e 1 7 e c h o > > $ C R O N L O G ;

Page 33: PyLadies Talk: Learn to love the command line!

Push

MAKING BACKUPS OF ...

1 # ! / b i n / s h 2 # 3 # A u t o m a t i c p u s h o n t h e d i r e c t o r i e s l i s t e d i n d i r s _ t o _ p u s h 4 5 C R O N L O G = " / c o m p l e t e / p a t h / t o / p u l l c o m m i t p u s h . l o g " 6 e c h o > > $ C R O N L O G 7 e c h o " N i g h t l y g i t a u t o - p u s h f o r t h e a b s e n t m i n d s : $ ( d a t e ) " > > $ C R O N L O G 8 f o r i i n ` c a t / c o m p l e t e / p a t h / t o / d i r s _ t o _ p u s h ` 9 d o ( c d $ i ;1 0 e c h o " P U S H : $ i $ ( d a t e ) " > > $ C R O N L O G1 1 g i t p u s h > > $ C R O N L O G 2 > & 11 2 e c h o > > $ C R O N L O G1 3 ) ;1 4 d o n e1 5 e c h o > > $ C R O N L O G ;

Page 34: PyLadies Talk: Learn to love the command line!

: from the Greek word for time, χρόνος chronos.

MAKING BACKUPS USING ...cron

Page 35: PyLadies Talk: Learn to love the command line!

https://en.wikipedia.org/wiki/Cron

MAKING BACKUPS USING ...Cron is a time-based job scheduler thatSysadmins use to schedule jobs to run

periodically at fixed times, dates, orintervals (i.e. download email at regular

intervals).

Page 36: PyLadies Talk: Learn to love the command line!

MAKING BACKUPS USING ...c r o n t a b - e

minute hour dayOfMonth month dayOfWeek command

3 2 * * * / c o m p l e t e / p a t h / t o / p u l l _ c o m m i t 2 > / c o m p l e t e / p a t h / t o / p u l l _ c o m m i t . e r r5 2 * * * / c o m p l e t e / p a t h / t o / p u s h 2 > / c o m p l e t e / p a t h / t o / p u s h . e r r

Page 37: PyLadies Talk: Learn to love the command line!

Blog post in development!

CREATING A COPY OF A GITREPOSITORY WHICH IS NOT

QUITE A COPY

Page 38: PyLadies Talk: Learn to love the command line!

THE ENDAre you in love yet?

Page 39: PyLadies Talk: Learn to love the command line!

JavaScript source code for the slides taken from.

I encourage you to try it!

THE ENDreveal.js