SlideShare a Scribd company logo
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command Line Kung-Fu
Bash / vi / Perl
By: Ed Williams - CTL
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Biff-Bash-Bosh
 /bin/bash available on most flavours of *nix.
- You all know about the arrow keys and tab completion!?
- We’ll dig a little deeper!
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Bash - History
 Use the ‘history’ command to display your history:
 Tip: alias the ‘history’ command to ‘h’ in your .bashrc
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Bash – History by Numbers
 Select a particular command using !<n>
 You can also use this notation in the middle of a command – e.g.
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Bash – History by Numbers
 Even two [or more] commands can be combined together.
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Searching history – my fav!!
 Search backwards using ctrl-r
 Hit ctrl-r, start typing the string you are looking for, the shell will
show the most recent match – ctrl-r again will take you further
back.
 Once you have the command:
 Enter will execute chosen command
 Escape / ctrl-j will place the command on the command line for editing
 ctrl-g / ctrl-c to cancel
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Bash - Keyboard Accelerators
 ‘!!’ Repeats the previous command
 Similarly, ‘!-2’ [and -3...] repeats the command before the
previous command
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Bash – Specifying Arguments
 ‘!$’ gets the last argument from the previous command:
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Bash – Specifying Arguments
• ‘!*’ gets all of the previous arguments
• You could use substitution with ‘^’ (only replaces first instance - no
global replacement)
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Bash – Fast Searching
• !<string> executes the last command that begins with
<search>
• This can be risky – so ensure you use the :p option
to print the last command
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Good ol’ vi
Good ol’ vi
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Good ol’ vi
 Ubiquitous. Essentially all Unix and Unix-like systems come with vi.
 AIX, Solaris, and all Linux variants
 Its very flexible and, once you get the hang of it, very quick for
development.
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
vi – Command Line Options
 Vi command Line Options
- vi –R file_name
- Opens file in read-only mode (or view file_name)
- vi + file_name
- Goes to the last line of file_name
- vi +n file_name
- Goes to the n’th line of file_name
- e.g. Will take you to all the Max / Min lengths variables in the
john.conf file
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
vi – Command Line Options
 vi +/pattern file_name
Start editing at the first line matching pattern
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
And for the hard core......
 Vi on the command line:
 Vi commands such as x, dd, 0, D etc. will now work
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
....and for the not so hardcore
 To knock the option off 
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Vi and ex – address symbols
- 1G – goes to first line
- G – goes to last line
- 1,$ - all lines in file
- % - entire file
- . - current line
- $ - last line
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Vi/ex tips and tricks
 Ex commands -
- :%d - delete all lines (same as :1,$d)
- :g/1/d - delete all occurrences of 1
- :%s/^M//g - removes carriage return – useful!
- Actually replaces ^M with null space
- :g/.*/m0 - reverse the order of the lines
- :g/^$/d - removes all blank lines
 Note – to get the ^M, <ctrl>v and enter.
- *m0 is the ex command to move the line to line 0.
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Vi/ex tips and tricks
 Use % to match brackets – useful when dealing with regular expressions.
 The $HOME/.exrc file can be used to hold certain config options e.g. The
following are set in my .exrc file (viewed with the set command)
 set ic sm showmode
 “set nu
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Vi Mappings
 Key Mapping – this is used to change the meaning of typed keys –
these mappings are held in $HOME/.exrc
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command Line Perl
Command Line Perl
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command Line Perl
 Why bother?
 Perl is pretty much standard on all *nix installs.
 Perl does a lot of the common stuff for you.
 Better than grep:
• - better sub-string matching
 String Escapes for control characters
 Line-spanning matches
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command Line Perl
 Better than awk (?): - No way to specify ranges
- No way to disable input parsing
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command Line Perl
 With the use of –M, you can use perl modules*:
 Tip: use –c as a sanity check before executing commands:
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Golf & Command line perl
 Create a prog / script that will list all uid 0 users:
#! /usr/bin/perl –w
foreach (`cat /etc/passwd`) {
@line = split(/:/);
print if $line[2] =~ /^0$/
}
Note: perl increments from 0, hence $line[2] for the third element,
unlike awk**
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command line perl – cont’d....
 And from the command line:
 There is a lot going on here - lets break it down into its separate
components:
 -e - The option lets you specify a single line of code on the
command line
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command line perl – cont’d....
-n - This option places a loop around the command, in the form of:
While (<>) {
..command..
}
Note: -p adds a ‘print $_;’
-a - this will feed input lines to the split() function. The results of
the split are placed into the @F variable
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Command line perl – cont’d....
 Now that we are splitting, we need to know what to split on:
 -F - lets you change the regular expression that is used to split the
input lines e.g.
–F/:/
 -l - This option turns on line-ending processing – in effect,
chomp()’s and adds ‘n’ – really useful for one-liners!
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Removing ^M with command line perl
 File with ^M’s
 Run the command
 more of new_car
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Resources
Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved
Any Questions?
*see http://www.cpan.org/
**awk -F: ‘{if ($3==0) print $F}’ /etc/passwd

More Related Content

Similar to Command Line Kung-Fu

CMake Tutorial
CMake TutorialCMake Tutorial
CMake Tutorial
Fu Haiping
 
Unix day2 v1.3
Unix day2 v1.3Unix day2 v1.3
Unix day2 v1.3
xavier john
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentation
brian_dailey
 
Unix Command Line Productivity Tips
Unix Command Line Productivity TipsUnix Command Line Productivity Tips
Unix Command Line Productivity Tips
Keith Bennett
 
Preparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesPreparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple Architectures
Ake Koomsin
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - Summary
Nugroho Gito
 
Ultimate Unix Meetup Presentation
Ultimate Unix Meetup PresentationUltimate Unix Meetup Presentation
Ultimate Unix Meetup Presentation
JacobMenke1
 
© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx
© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx
© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx
LynellBull52
 
Cfx12 13 ccl_out_file
Cfx12 13 ccl_out_fileCfx12 13 ccl_out_file
Cfx12 13 ccl_out_fileMarcushuynh66
 
DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foobrian_dailey
 
Unix day3 v1.3
Unix day3 v1.3Unix day3 v1.3
Unix day3 v1.3
xavier john
 
Arista: DevOps for Network Engineers
Arista: DevOps for Network EngineersArista: DevOps for Network Engineers
Arista: DevOps for Network Engineers
Philip DiLeo
 
Basicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt applicationBasicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt application
Dinesh Manajipet
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
MARRY7
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
adkinspaige22
 
Firebird2.5 Benchmarks(English)20091031
Firebird2.5 Benchmarks(English)20091031Firebird2.5 Benchmarks(English)20091031
Firebird2.5 Benchmarks(English)20091031
Tsutomu Hayashi
 

Similar to Command Line Kung-Fu (20)

CMake Tutorial
CMake TutorialCMake Tutorial
CMake Tutorial
 
Unix day2 v1.3
Unix day2 v1.3Unix day2 v1.3
Unix day2 v1.3
 
NYPHP March 2009 Presentation
NYPHP March 2009 PresentationNYPHP March 2009 Presentation
NYPHP March 2009 Presentation
 
Unix Command Line Productivity Tips
Unix Command Line Productivity TipsUnix Command Line Productivity Tips
Unix Command Line Productivity Tips
 
Preparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple ArchitecturesPreparing BitVisor for Supporting Multiple Architectures
Preparing BitVisor for Supporting Multiple Architectures
 
RHCSA EX200 - Summary
RHCSA EX200 - SummaryRHCSA EX200 - Summary
RHCSA EX200 - Summary
 
Ultimate Unix Meetup Presentation
Ultimate Unix Meetup PresentationUltimate Unix Meetup Presentation
Ultimate Unix Meetup Presentation
 
© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx
© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx
© 2007 Cisco Systems, Inc. All rights reserved. Cisco Public 1.docx
 
Vim and Python
Vim and PythonVim and Python
Vim and Python
 
Cfx12 13 ccl_out_file
Cfx12 13 ccl_out_fileCfx12 13 ccl_out_file
Cfx12 13 ccl_out_file
 
DevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung FooDevChatt 2010 - *nix Cmd Line Kung Foo
DevChatt 2010 - *nix Cmd Line Kung Foo
 
Introduction to Linux
Introduction to LinuxIntroduction to Linux
Introduction to Linux
 
Unix day3 v1.3
Unix day3 v1.3Unix day3 v1.3
Unix day3 v1.3
 
Arista: DevOps for Network Engineers
Arista: DevOps for Network EngineersArista: DevOps for Network Engineers
Arista: DevOps for Network Engineers
 
C++ Naming Conventions
C++ Naming ConventionsC++ Naming Conventions
C++ Naming Conventions
 
Basicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt applicationBasicsof c make and git for a hello qt application
Basicsof c make and git for a hello qt application
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 of 70UNIX Unbounded 5th EditionAmir Afzal .docx of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docxof 70UNIX Unbounded 5th EditionAmir Afzal .docx
of 70UNIX Unbounded 5th EditionAmir Afzal .docx
 
Firebird2.5 Benchmarks(English)20091031
Firebird2.5 Benchmarks(English)20091031Firebird2.5 Benchmarks(English)20091031
Firebird2.5 Benchmarks(English)20091031
 
Linux
LinuxLinux
Linux
 

Command Line Kung-Fu

  • 1. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command Line Kung-Fu Bash / vi / Perl By: Ed Williams - CTL
  • 2. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Biff-Bash-Bosh  /bin/bash available on most flavours of *nix. - You all know about the arrow keys and tab completion!? - We’ll dig a little deeper!
  • 3. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Bash - History  Use the ‘history’ command to display your history:  Tip: alias the ‘history’ command to ‘h’ in your .bashrc
  • 4. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Bash – History by Numbers  Select a particular command using !<n>  You can also use this notation in the middle of a command – e.g.
  • 5. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Bash – History by Numbers  Even two [or more] commands can be combined together.
  • 6. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Searching history – my fav!!  Search backwards using ctrl-r  Hit ctrl-r, start typing the string you are looking for, the shell will show the most recent match – ctrl-r again will take you further back.  Once you have the command:  Enter will execute chosen command  Escape / ctrl-j will place the command on the command line for editing  ctrl-g / ctrl-c to cancel
  • 7. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Bash - Keyboard Accelerators  ‘!!’ Repeats the previous command  Similarly, ‘!-2’ [and -3...] repeats the command before the previous command
  • 8. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Bash – Specifying Arguments  ‘!$’ gets the last argument from the previous command:
  • 9. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Bash – Specifying Arguments • ‘!*’ gets all of the previous arguments • You could use substitution with ‘^’ (only replaces first instance - no global replacement)
  • 10. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Bash – Fast Searching • !<string> executes the last command that begins with <search> • This can be risky – so ensure you use the :p option to print the last command
  • 11. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Good ol’ vi Good ol’ vi
  • 12. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Good ol’ vi  Ubiquitous. Essentially all Unix and Unix-like systems come with vi.  AIX, Solaris, and all Linux variants  Its very flexible and, once you get the hang of it, very quick for development.
  • 13. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved vi – Command Line Options  Vi command Line Options - vi –R file_name - Opens file in read-only mode (or view file_name) - vi + file_name - Goes to the last line of file_name - vi +n file_name - Goes to the n’th line of file_name - e.g. Will take you to all the Max / Min lengths variables in the john.conf file
  • 14. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved vi – Command Line Options  vi +/pattern file_name Start editing at the first line matching pattern
  • 15. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved And for the hard core......  Vi on the command line:  Vi commands such as x, dd, 0, D etc. will now work
  • 16. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved ....and for the not so hardcore  To knock the option off 
  • 17. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Vi and ex – address symbols - 1G – goes to first line - G – goes to last line - 1,$ - all lines in file - % - entire file - . - current line - $ - last line
  • 18. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Vi/ex tips and tricks  Ex commands - - :%d - delete all lines (same as :1,$d) - :g/1/d - delete all occurrences of 1 - :%s/^M//g - removes carriage return – useful! - Actually replaces ^M with null space - :g/.*/m0 - reverse the order of the lines - :g/^$/d - removes all blank lines  Note – to get the ^M, <ctrl>v and enter. - *m0 is the ex command to move the line to line 0.
  • 19. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Vi/ex tips and tricks  Use % to match brackets – useful when dealing with regular expressions.  The $HOME/.exrc file can be used to hold certain config options e.g. The following are set in my .exrc file (viewed with the set command)  set ic sm showmode  “set nu
  • 20. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Vi Mappings  Key Mapping – this is used to change the meaning of typed keys – these mappings are held in $HOME/.exrc
  • 21. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command Line Perl Command Line Perl
  • 22. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command Line Perl  Why bother?  Perl is pretty much standard on all *nix installs.  Perl does a lot of the common stuff for you.  Better than grep: • - better sub-string matching  String Escapes for control characters  Line-spanning matches
  • 23. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command Line Perl  Better than awk (?): - No way to specify ranges - No way to disable input parsing
  • 24. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command Line Perl  With the use of –M, you can use perl modules*:  Tip: use –c as a sanity check before executing commands:
  • 25. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Golf & Command line perl  Create a prog / script that will list all uid 0 users: #! /usr/bin/perl –w foreach (`cat /etc/passwd`) { @line = split(/:/); print if $line[2] =~ /^0$/ } Note: perl increments from 0, hence $line[2] for the third element, unlike awk**
  • 26. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command line perl – cont’d....  And from the command line:  There is a lot going on here - lets break it down into its separate components:  -e - The option lets you specify a single line of code on the command line
  • 27. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command line perl – cont’d.... -n - This option places a loop around the command, in the form of: While (<>) { ..command.. } Note: -p adds a ‘print $_;’ -a - this will feed input lines to the split() function. The results of the split are placed into the @F variable
  • 28. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Command line perl – cont’d....  Now that we are splitting, we need to know what to split on:  -F - lets you change the regular expression that is used to split the input lines e.g. –F/:/  -l - This option turns on line-ending processing – in effect, chomp()’s and adds ‘n’ – really useful for one-liners!
  • 29. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Removing ^M with command line perl  File with ^M’s  Run the command  more of new_car
  • 30. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Resources
  • 31. Commercial in Confidence - © Copyright 2008 NCC Group plc - all rights reserved Any Questions? *see http://www.cpan.org/ **awk -F: ‘{if ($3==0) print $F}’ /etc/passwd