Linux shell tricks
Topics
Topics ● Me talking ○ Useful key combinations ○ Basic commands ○ Special shell expressions ○ Pipes and redirects ○ On-the-fly “scripts” ○ vim survival guide ○ Customizing the shell ● You doing ○ Get information from a set of log files ● Q&A
Key combinations
Key combinations Ctrl+C Panic button. Usually stops what’s going on (C = Cancel) ↑, ↓ Move through previous commands Ctrl+R Search in history. (Start typing to search, arrow keys to use + modify command) Ctrl+Z Suspend current program without terminating it. Resume with fg
Basic commands
Basic commands ls List files du Show disk usage of files touch Create a file or update mtime df Show free disk space mkdir / rmdir Create/delete directory lsof List open files / sockets rm Delete file ssh Secure shell on another machine find Find files and directories xargs Run command for every line of input cat Show file contents sed Modify text files less Show file contents for users cut Select columns grep Find text in file(s) man Get Help head, tail Get first / last lines
Special chars
Special shell expressions * Match 0, 1 or more characters in file names ? Match exactly 1 character in file names {N..M} Generate a sequence of numbers from N to M `...` Backticks: Execute the command between the backticks and insert the output at that place ~ The home directory Shell expansions Often-used characters - Dash: The previous ‘thing’ (cd, git, …) or the input (cat) -- Double dash: End of arguments (more on that later)
Pipes and Redirects
Pipes and redirects Input: ● standard input (“STDIN”) ○ usually the user typing on their keyboard Outputs: ● standard output (“STDOUT”) ○ usually printed on the screen ● standard error (“STDERR”) ○ usually printed on the screen
Pipes and redirects | Pipe: pass the output of one program to another program > Save the output of a program into a file, deletes the old file contents! >> Same as >, but appends to the original file (creates the file if necessary) < Pass the contents of a file as input to the program 2>, 2>> Same as >, >> but for error output &1 Instead of a filename, this is the standard output. For example: 2> &1 redirects the error output to standard output.
On-the-fly scripts
On-the-fly scripts ● Chain commands with ; ● Sub shells with ( ) ● loops with for and while
vim survival guide
vim survival guide The first thing you do when bad things happen: Get a clean state ● ESC ESC ESC ^C ^C ESC ESC Edit text: Hit I (“insert mode” - then vim works almost like an ordinary editor). ● Leave insert mode with ESC Undo: Hit U (press repeated times to go back further) Copy & Paste: ● Start selection with v or Shift+V (whole lines) ● Hit Y to copy or D to cut. Move to new position and hit p Save and quit: type “:wq” and hit Return Quit without saving: “:q!” + Return
Shell Customizations
Hands-On
Hands-On Log file processing: ● Count the occurrences of HTTP status codes ● Get a list of customer IDs which were denied for manual refunds

Tech talk 01.06.2017

  • 1.
  • 2.
  • 3.
    Topics ● Me talking ○Useful key combinations ○ Basic commands ○ Special shell expressions ○ Pipes and redirects ○ On-the-fly “scripts” ○ vim survival guide ○ Customizing the shell ● You doing ○ Get information from a set of log files ● Q&A
  • 4.
  • 5.
    Key combinations Ctrl+C Panicbutton. Usually stops what’s going on (C = Cancel) ↑, ↓ Move through previous commands Ctrl+R Search in history. (Start typing to search, arrow keys to use + modify command) Ctrl+Z Suspend current program without terminating it. Resume with fg
  • 6.
  • 7.
    Basic commands ls Listfiles du Show disk usage of files touch Create a file or update mtime df Show free disk space mkdir / rmdir Create/delete directory lsof List open files / sockets rm Delete file ssh Secure shell on another machine find Find files and directories xargs Run command for every line of input cat Show file contents sed Modify text files less Show file contents for users cut Select columns grep Find text in file(s) man Get Help head, tail Get first / last lines
  • 8.
  • 9.
    Special shell expressions *Match 0, 1 or more characters in file names ? Match exactly 1 character in file names {N..M} Generate a sequence of numbers from N to M `...` Backticks: Execute the command between the backticks and insert the output at that place ~ The home directory Shell expansions Often-used characters - Dash: The previous ‘thing’ (cd, git, …) or the input (cat) -- Double dash: End of arguments (more on that later)
  • 10.
  • 11.
    Pipes and redirects Input: ●standard input (“STDIN”) ○ usually the user typing on their keyboard Outputs: ● standard output (“STDOUT”) ○ usually printed on the screen ● standard error (“STDERR”) ○ usually printed on the screen
  • 12.
    Pipes and redirects |Pipe: pass the output of one program to another program > Save the output of a program into a file, deletes the old file contents! >> Same as >, but appends to the original file (creates the file if necessary) < Pass the contents of a file as input to the program 2>, 2>> Same as >, >> but for error output &1 Instead of a filename, this is the standard output. For example: 2> &1 redirects the error output to standard output.
  • 13.
  • 14.
    On-the-fly scripts ● Chaincommands with ; ● Sub shells with ( ) ● loops with for and while
  • 15.
  • 16.
    vim survival guide Thefirst thing you do when bad things happen: Get a clean state ● ESC ESC ESC ^C ^C ESC ESC Edit text: Hit I (“insert mode” - then vim works almost like an ordinary editor). ● Leave insert mode with ESC Undo: Hit U (press repeated times to go back further) Copy & Paste: ● Start selection with v or Shift+V (whole lines) ● Hit Y to copy or D to cut. Move to new position and hit p Save and quit: type “:wq” and hit Return Quit without saving: “:q!” + Return
  • 17.
  • 18.
  • 19.
    Hands-On Log file processing: ●Count the occurrences of HTTP status codes ● Get a list of customer IDs which were denied for manual refunds