120
Live with Daily CLI Live with Daily CLI Madsen sen.zhang@gmail Insitute of Computing Technology November 6, 2006 Live with Daily CLI

Live with cli(bash)

  • Upload
    -

  • View
    698

  • Download
    0

Embed Size (px)

DESCRIPTION

this was written about 6 years ago and I did not know zsh yet at that time.

Citation preview

Page 1: Live with cli(bash)

Live with Daily CLI

Live with Daily CLI

Madsensen.zhang@gmail

Insitute of Computing Technology

November 6, 2006

Live with Daily CLI

Page 2: Live with cli(bash)

Live with Daily CLI

Powered by

This slide is powered by

Linux

GNU

GNU Emacs

TEX and LATEX

LATEX beamer

Live with Daily CLI

Page 3: Live with cli(bash)

Live with Daily CLI

Introduction

Outline

1 IntroductionWhat’s CLIWhat’s “shell”Shell TypesTypes of Commands in Bash

2 Customize Bash

3 Bash Tricks

4 Great command line utils

Live with Daily CLI

Page 4: Live with cli(bash)

Live with Daily CLI

Introduction

About What

What’s This About

Some tricks and tips to customize and make better use of theBash.

Live with Daily CLI

Page 5: Live with cli(bash)

Live with Daily CLI

Introduction

About What

What’s This About

Some tricks and tips to customize and make better use of theBash.

*NOT* about bash programming.

Live with Daily CLI

Page 6: Live with cli(bash)

Live with Daily CLI

Introduction

What’s CLI

What’s CLI

Command Line, On a display screen, the space following aprompt (such as $) where a text instruction to a computer ordevice is typed.

Live with Daily CLI

Page 7: Live with cli(bash)

Live with Daily CLI

Introduction

What’s CLI

What’s CLI

Command Line, On a display screen, the space following aprompt (such as $) where a text instruction to a computer ordevice is typed.CLI, a user interface in which you type commands consistingof text instead of choosing them from a menu or selecting anicon.

Live with Daily CLI

Page 8: Live with cli(bash)

Live with Daily CLI

Introduction

What’s CLI

What’s CLI

Command Line, On a display screen, the space following aprompt (such as $) where a text instruction to a computer ordevice is typed.CLI, a user interface in which you type commands consistingof text instead of choosing them from a menu or selecting anicon.A significant minority of computer users prefer to use CLIs,some due to visual disability, but most because they feel thatCLIs provide an environment with enhanced productivity.

Live with Daily CLI

Page 9: Live with cli(bash)

Live with Daily CLI

Introduction

What’s CLI

What’s CLI

Command Line, On a display screen, the space following aprompt (such as $) where a text instruction to a computer ordevice is typed.CLI, a user interface in which you type commands consistingof text instead of choosing them from a menu or selecting anicon.A significant minority of computer users prefer to use CLIs,some due to visual disability, but most because they feel thatCLIs provide an environment with enhanced productivity.What on earth can CLI do then?

Live with Daily CLI

Page 10: Live with cli(bash)

Live with Daily CLI

Introduction

What’s CLI

What’s CLI

Command Line, On a display screen, the space following aprompt (such as $) where a text instruction to a computer ordevice is typed.CLI, a user interface in which you type commands consistingof text instead of choosing them from a menu or selecting anicon.A significant minority of computer users prefer to use CLIs,some due to visual disability, but most because they feel thatCLIs provide an environment with enhanced productivity.What on earth can CLI do then?CLI can run many many programs, perform specific task: Webbrowsing(w3m), music playing(mpg321), displaying tasks(top)receiving and sending emails(mutt+esmpt+fetchmail),BBS(ssh), editing files(emacs,vi), file management(mc), filetransfer(scp, lftp), downloading(wget), and so many . . .

Live with Daily CLI

Page 11: Live with cli(bash)

Live with Daily CLI

Introduction

What’s “shell”

What’s “shell”

“shell”: any program that users use to type commands to thesystem.

Live with Daily CLI

Page 12: Live with cli(bash)

Live with Daily CLI

Introduction

What’s “shell”

What’s “shell”

“shell”: any program that users use to type commands to thesystem.

hard outer covering of eggs, of nut-kernels, of some seeds andfruits, and of animals such as oysters, snails, crabs andtortoises.

Live with Daily CLI

Page 13: Live with cli(bash)

Live with Daily CLI

Introduction

What’s “shell”

What’s “shell”

“shell”: any program that users use to type commands to thesystem.

hard outer covering of eggs, of nut-kernels, of some seeds andfruits, and of animals such as oysters, snails, crabs andtortoises.

It hides the details of the underlying operating system behindthe shell’s interface

Live with Daily CLI

Page 14: Live with cli(bash)

Live with Daily CLI

Introduction

What’s “shell”

What’s “shell”

“shell”: any program that users use to type commands to thesystem.

hard outer covering of eggs, of nut-kernels, of some seeds andfruits, and of animals such as oysters, snails, crabs andtortoises.

It hides the details of the underlying operating system behindthe shell’s interface

“shell” provides a kind of CLI.

Live with Daily CLI

Page 15: Live with cli(bash)

Live with Daily CLI

Introduction

What’s “shell”

What’s “shell”

“shell”: any program that users use to type commands to thesystem.

hard outer covering of eggs, of nut-kernels, of some seeds andfruits, and of animals such as oysters, snails, crabs andtortoises.

It hides the details of the underlying operating system behindthe shell’s interface

“shell” provides a kind of CLI.

The word “shell” also refers to a particular program, namelythe Bourne shell or (shortly) sh or Bash.

Live with Daily CLI

Page 16: Live with cli(bash)

Live with Daily CLI

Introduction

Shell Types

Shell Types existing

*nix shells can be broadly divided into four categories:Bourne-like, C Shell-like, nontraditional, and historical.(Viewthe shell types at http://en.wikipedia.org/wiki/Unix shell andhttp://www.freebsdsoftware.org.).

Live with Daily CLI

Page 17: Live with cli(bash)

Live with Daily CLI

Introduction

Shell Types

Shell Types existing

*nix shells can be broadly divided into four categories:Bourne-like, C Shell-like, nontraditional, and historical.(Viewthe shell types at http://en.wikipedia.org/wiki/Unix shell andhttp://www.freebsdsoftware.org.).

Bash is Bourne-like. “Bash” is an acronym for ‘Bourne-AgainShell’.

Live with Daily CLI

Page 18: Live with cli(bash)

Live with Daily CLI

Introduction

Shell Types

Shell Types existing

*nix shells can be broadly divided into four categories:Bourne-like, C Shell-like, nontraditional, and historical.(Viewthe shell types at http://en.wikipedia.org/wiki/Unix shell andhttp://www.freebsdsoftware.org.).

Bash is Bourne-like. “Bash” is an acronym for ‘Bourne-AgainShell’.

Focus on Bash and some utilities only today.

Live with Daily CLI

Page 19: Live with cli(bash)

Live with Daily CLI

Introduction

Types of Commands in Bash

Types Command of in Bash

Bash built-in commands(builtins): cd break continue exec

Live with Daily CLI

Page 20: Live with cli(bash)

Live with Daily CLI

Introduction

Types of Commands in Bash

Types Command of in Bash

Bash built-in commands(builtins): cd break continue exec

External utilities: ls mv rm cp sed awk

Live with Daily CLI

Page 21: Live with cli(bash)

Live with Daily CLI

Introduction

Types of Commands in Bash

Types Command of in Bash

Bash built-in commands(builtins): cd break continue exec

External utilities: ls mv rm cp sed awk

Bash(in fact, most shells) also provides a programminglanguage: vars, flow control, quoting and functions.

Live with Daily CLI

Page 22: Live with cli(bash)

Live with Daily CLI

Introduction

Types of Commands in Bash

Types Command of in Bash

Bash built-in commands(builtins): cd break continue exec

External utilities: ls mv rm cp sed awk

Bash(in fact, most shells) also provides a programminglanguage: vars, flow control, quoting and functions.

Power of Bash: embed and combine them in your bash scriptsusing the Bash programming language.

Live with Daily CLI

Page 23: Live with cli(bash)

Live with Daily CLI

Customize Bash

Outline

1 Introduction

2 Customize BashCustomize the Bash PromptHow to Set Environment VariablesAliases

3 Bash Tricks

4 Great command line utils

Live with Daily CLI

Page 24: Live with cli(bash)

Live with Daily CLI

Customize Bash

Customize the Bash Prompt

Make a Pretty Bash Prompt

in ∼/.bashrc(cf Listing 1):

Listing 1: Customize the bash prompt

,� �1 PS1="\[\e]2;\w\a\n\[\e[32;1m\][\D{%Y-%m-%d} \t]\u@\h:\[\e

[33;1m\]\w\[\e[0m\]\n$"� �

Example

[2006-10-16 23:20:51]madsen@madsen:∼$

Live with Daily CLI

Page 25: Live with cli(bash)

Live with Daily CLI

Customize Bash

How to Set Environment Variables

How to Set Environment Variables

Environment variables affects many things.Use “export” to setthem.

Example

To add “∼/bin” to the commands searching path:$export PATH=$PATH:∼/binor to be permanent to your bash, append to your ∼/.bashrc:export PATH=$PATH:∼/bin

Live with Daily CLI

Page 26: Live with cli(bash)

Live with Daily CLI

Customize Bash

Aliases

Alias: shorter, easier, quicker

It’s more convenient once aliased.

Use “alias” or “alias -p” on the CLI to print all the aliasedcommands.

Live with Daily CLI

Page 27: Live with cli(bash)

Live with Daily CLI

Customize Bash

Aliases

Alias: shorter, easier, quicker

It’s more convenient once aliased.

Use “alias” or “alias -p” on the CLI to print all the aliasedcommands.Use alias newcmd=“oldcmd arguments” or alias

newcmd=’oldcmd arguments’ to set new aliases. Add in the∼/.bashrc file, to be permanent:

Live with Daily CLI

Page 28: Live with cli(bash)

Live with Daily CLI

Customize Bash

Aliases

Alias: shorter, easier, quicker

It’s more convenient once aliased.

Use “alias” or “alias -p” on the CLI to print all the aliasedcommands.Use alias newcmd=“oldcmd arguments” or alias

newcmd=’oldcmd arguments’ to set new aliases. Add in the∼/.bashrc file, to be permanent:

Convenient aliaes

alias ls=“ls –color -CF”alias mv=“mv -i”alias cp=“cp -i”alias rm=“cp -i”alias ..=“cd ..”alias mkdir=“mkdir -p”alias scp=“scp -pr”

Live with Daily CLI

Page 29: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Outline

1 Introduction

2 Customize Bash

3 Bash TricksMake “cd” More ConvenientBetter Use of Bash HistoryBrace ExpansionCommand SubstitutionUsing for Loops at the Command LineBash I/O RedirectionEscape Accidental Exiting

4 Great command line utilsLive with Daily CLI

Page 30: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Tab Completion

Make Use of Tab Completion

Enjoy intelligent tab completion.

Install bash completion(available athttp://www.caliban.org/bash/index.shtml#completion)

Live with Daily CLI

Page 31: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Tab Completion

Make Use of Tab Completion

Enjoy intelligent tab completion.

Install bash completion(available athttp://www.caliban.org/bash/index.shtml#completion)

Source it in your ∼/.bashrc or ∼/.bash profile:

Live with Daily CLI

Page 32: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Tab Completion

Make Use of Tab Completion

Enjoy intelligent tab completion.

Install bash completion(available athttp://www.caliban.org/bash/index.shtml#completion)

Source it in your ∼/.bashrc or ∼/.bash profile:

Source bash completion

if [ -f /etc/bash completion ]; then. /etc/bash completion

fi

Live with Daily CLI

Page 33: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Tab Completion

Make Use of Tab Completion

Enjoy intelligent tab completion.

Install bash completion(available athttp://www.caliban.org/bash/index.shtml#completion)

Source it in your ∼/.bashrc or ∼/.bash profile:

Source bash completion

if [ -f /etc/bash completion ]; then. /etc/bash completion

fi

Much smoother experience with bash :-P.

Live with Daily CLI

Page 34: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Tab Completion

Make Use of Tab Completion

Enjoy intelligent tab completion.

Install bash completion(available athttp://www.caliban.org/bash/index.shtml#completion)

Source it in your ∼/.bashrc or ∼/.bash profile:

Source bash completion

if [ -f /etc/bash completion ]; then. /etc/bash completion

fi

Much smoother experience with bash :-P.

Completion of arguments according to most commands(e.g.acroread, latex) intelligently.

Live with Daily CLI

Page 35: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Interface Editing Mode

Command Line Interface Editing Mode

emacs style: set -o emacs

Live with Daily CLI

Page 36: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Interface Editing Mode

Command Line Interface Editing Mode

emacs style: set -o emacs

vi style: set -o vi

Live with Daily CLI

Page 37: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Interface Editing Mode

Command Line Interface Editing Mode

emacs style: set -o emacs

vi style: set -o vi

I’ll list key bindings(Ctrl and Alt) of the emacs editing modefor reference.

Live with Daily CLI

Page 38: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Shortcuts(emacs mode)

Shortcuts, make life easier: Ctrl Key Binding

C-a - Jump to the start of the line

C-b - Move back a char

C-c - Terminate the command

C-d - Delete from under the cursor

C-e - Jump to the end of the line

C-f - Move forward a char

C-k - Delete to EOL

C-l - Clear the screen

C-r - Search the history backwards

Live with Daily CLI

Page 39: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Shortcuts(emacs mode)

Shortcuts, make life easier: Ctrl Key Binding(Cont)

C-u - Delete backward from cursor

C-x @ - Show possible hostname completions

C-z - Suspend/ Stop the command

C-i - Equal to the <TAB> key

C-m - <ENTER>

C-backspace - Delete basename of the string before the cursor.

C-x C-e Invoke an editor on the current command line, andexecute the result as shell commands. Bash attempts toinvoke $FCEDIT, $EDITOR, and emacs as the editor, in thatorder.

C-t Transpose characters.

C-w Delete backward one string.

Live with Daily CLI

Page 40: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Shortcuts(emacs mode)

Something more on C-r

C-r again to search further in the bash history with the sametext, if the current one found is not what you want.

When the proper history command typed is found,<ENTER> or C-m to execute it.

Or, after the proper history command typed is found, C-o toexecute it, and it finishes, the next command in the bashhistory after the current one found will be placed on thecommand line ready for you to use.

Volatile alias: $cd /a/rather/long/path #rlpThen later: $C-rrlp<Enter>

Env vars: HISTSIZE, HISTFILE, HISTFILESIZE(man bash fordetail).

Live with Daily CLI

Page 41: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Shortcuts(emacs mode)

Shortcuts, make life easier: Alt Key Binding

M-< - Move to the first line in the history

M-> - Move to the last line in the history

M-? - Show current completion list

M-* - Insert all possible completions

M-/ - Attempt to complete filename

M-. - Yank last argument to previous command

M-b - Move backward(abnormal in X terminal-emulator)

M-c - Capitalize the word(from the where the cursor is)

Live with Daily CLI

Page 42: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Line Shortcuts(emacs mode)

Shortcuts, make life easier: Alt Key Binding

M-d - Delete word

M-f - Move forward(abnormal in X terminal-emulator

M-u - Make word uppercase

M-l - Make word lowercase

M-n - Search the history forwards non-incremental

M-p - Search the history backwards non-incremental

M-backspace - Delete backward from cursor

M-t - Transpose words around

Live with Daily CLI

Page 43: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Make “cd” More Convenient

Make “cd” More Convenient

The env var CDPATH sets the searching path for the “cd”command(cf Listing: 2).

Listing 2: Set searching path for “cd”

,� �1 export CDPATH=.:..:$\sim$:/opt/src

� �

Then, names of sub-directories under these directories can be usedas something like bookmarks.

Live with Daily CLI

Page 44: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Change Bash History’s Saving Behavior

Change Bash History’s Saving Behavior

Different Bash sessions may conflict in the default case(append tovs. overwrite).

Env vars: HISTFILE, HISTFILESIZE, HISTSIZE,HISTIGNOREin ∼/.bashrc:

Live with Daily CLI

Page 45: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Change Bash History’s Saving Behavior

Change Bash History’s Saving Behavior

Different Bash sessions may conflict in the default case(append tovs. overwrite).

Env vars: HISTFILE, HISTFILESIZE, HISTSIZE,HISTIGNOREin ∼/.bashrc:

Set env vars affecting Bash history

export HISTIGNORE=”&:ls:ll:[fb]g:exit:svn up:cvs up”export HISTFILESIZE=2000export HISTSIZE=500

Live with Daily CLI

Page 46: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Change Bash History’s Saving Behavior

Appending, not writing, to the history file. Also, add to your∼/.bashrc:

Append history lines

shopt -s histappend

Make Bash append to rather than overwrite the history file ondisk.

Write before new prompt

export PROMPT COMMAND=’history -a’

Before displaying the next prompt, write the previous line todisk.

Saving history lines is a security problem. Trade-off. Make thedecision yourself:).

Live with Daily CLI

Page 47: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Reuse Bash History Commands

Reuse Bash History Commands

History makes intelligence.

Use !$ to repeat the last argument to the last command.

Live with Daily CLI

Page 48: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Reuse Bash History Commands

Reuse Bash History Commands

History makes intelligence.

Use !$ to repeat the last argument to the last command.

Use Alt+. to repeat the last argument to formercommands.(Recommendedand preferred, personally)

Live with Daily CLI

Page 49: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Reuse Bash History Commands

Reuse Bash History Commands

History makes intelligence.

Use !$ to repeat the last argument to the last command.

Use Alt+. to repeat the last argument to formercommands.(Recommendedand preferred, personally)

Repeat exactly the last command with all the originalarguments to it: “!xyz”(or even simpler, just “!!”).

Live with Daily CLI

Page 50: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Reuse Bash History Commands

Reuse Bash History Commands

History makes intelligence.

Use !$ to repeat the last argument to the last command.

Use Alt+. to repeat the last argument to formercommands.(Recommendedand preferred, personally)

Repeat exactly the last command with all the originalarguments to it: “!xyz”(or even simpler, just “!!”).

Examples:

Live with Daily CLI

Page 51: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Reuse Bash History Commands

Reuse Bash History Commands

History makes intelligence.

Use !$ to repeat the last argument to the last command.

Use Alt+. to repeat the last argument to formercommands.(Recommendedand preferred, personally)

Repeat exactly the last command with all the originalarguments to it: “!xyz”(or even simpler, just “!!”).

Examples:

Example

$mkdir /tmp/test$cd $!<Enter>$mkdir /tmp/test1$cd <Alt>+. $command –withcomplicatedarguments$!command<Enter>

Live with Daily CLI

Page 52: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Get Other Parts of Earlier Commands

Get Other Parts of Earlier Commands

This is not so intresting and useful. Use ! to get other parts ofearlier commands.

!-1 or !! is the whole previous command.

Live with Daily CLI

Page 53: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Get Other Parts of Earlier Commands

Get Other Parts of Earlier Commands

This is not so intresting and useful. Use ! to get other parts ofearlier commands.

!-1 or !! is the whole previous command.

!-1:0 or !:0 or !!:0 is the previous command name.

Live with Daily CLI

Page 54: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Get Other Parts of Earlier Commands

Get Other Parts of Earlier Commands

This is not so intresting and useful. Use ! to get other parts ofearlier commands.

!-1 or !! is the whole previous command.

!-1:0 or !:0 or !!:0 is the previous command name.

!ˆ or !:ˆ, !:2, !:3, , !$ or !:$ are the corresponding(with ˆ or $,the colon can be omitted) arguments of the previouscommand.

Live with Daily CLI

Page 55: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Get Other Parts of Earlier Commands

Get Other Parts of Earlier Commands

This is not so intresting and useful. Use ! to get other parts ofearlier commands.

!-1 or !! is the whole previous command.

!-1:0 or !:0 or !!:0 is the previous command name.

!ˆ or !:ˆ, !:2, !:3, , !$ or !:$ are the corresponding(with ˆ or $,the colon can be omitted) arguments of the previouscommand.

!* or !:* is all the arguments of the previous command.

Live with Daily CLI

Page 56: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Get Other Parts of Earlier Commands

Get Other Parts of Earlier Commands

This is not so intresting and useful. Use ! to get other parts ofearlier commands.

!-1 or !! is the whole previous command.

!-1:0 or !:0 or !!:0 is the previous command name.

!ˆ or !:ˆ, !:2, !:3, , !$ or !:$ are the corresponding(with ˆ or $,the colon can be omitted) arguments of the previouscommand.

!* or !:* is all the arguments of the previous command.

!-3:1, !-3:*

Live with Daily CLI

Page 57: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Get Other Parts of Earlier Commands

Get Other Parts of Earlier Commands

This is not so intresting and useful. Use ! to get other parts ofearlier commands.

!-1 or !! is the whole previous command.

!-1:0 or !:0 or !!:0 is the previous command name.

!ˆ or !:ˆ, !:2, !:3, , !$ or !:$ are the corresponding(with ˆ or $,the colon can be omitted) arguments of the previouscommand.

!* or !:* is all the arguments of the previous command.

!-3:1, !-3:*

Live with Daily CLI

Page 58: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Get Other Parts of Earlier Commands

Get Other Parts of Earlier Commands

This is not so intresting and useful. Use ! to get other parts ofearlier commands.

!-1 or !! is the whole previous command.

!-1:0 or !:0 or !!:0 is the previous command name.

!ˆ or !:ˆ, !:2, !:3, , !$ or !:$ are the corresponding(with ˆ or $,the colon can be omitted) arguments of the previouscommand.

!* or !:* is all the arguments of the previous command.

!-3:1, !-3:*

Example

What would this command, which seems weird and nonsense,$!-2:0 -R !ˆ !-3:2do??

Live with Daily CLI

Page 59: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Make Your SPACE Magic

Make Your SPACE Magic

Expand history when press SPACE in addition to its original spaceinserting function. Add the following lines to your ∼/.inputrc:

Magic SPACE

$if BashSpace: magic-space

$endif

Example

$ll *.txt$!:0<SPACE>

or$!!<SPACE>

Live with Daily CLI

Page 60: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Better History Command Searching

Better History Command Searching

Search history lines starting with a specific command. Add thefollowing lines to your ∼/.inputrc(cf Listing: 3):

Listing 3: Search only command

,� �1 set convert-meta on

2 ‘‘\M-p’’: history-search-backward

3 ‘‘\M-n’’: history-search-forward� �

NOTE: “set convert-meta on” would make scim functionabnormally in gnome-terminal and xfce4-terminal

Example

Type “sudo”, then M-p, it would iterate over all the previouscommand lines starting with “sudo”.

Live with Daily CLI

Page 61: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Brace Expansion

Using Brace Expansion

Save your time by typing long paths(or something else that’s long)only once.

e.g.

Live with Daily CLI

Page 62: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Brace Expansion

Using Brace Expansion

Save your time by typing long paths(or something else that’s long)only once.

e.g.

Example

$echo great\ {better,boost,loveqq,redgreen}\ boygreat better boy great boost boy great loveqq boy great redgreenboy

Live with Daily CLI

Page 63: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Brace Expansion

Using Brace Expansion

Save your time by typing long paths(or something else that’s long)only once.

e.g.

Example

$echo great\ {better,boost,loveqq,redgreen}\ boygreat better boy great boost boy great loveqq boy great redgreenboy

Backup configuration file:

Live with Daily CLI

Page 64: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Brace Expansion

Using Brace Expansion

Save your time by typing long paths(or something else that’s long)only once.

e.g.

Example

$echo great\ {better,boost,loveqq,redgreen}\ boygreat better boy great boost boy great loveqq boy great redgreenboy

Backup configuration file:

Example

$sudo cp /etc/apt/sources.list{,.bak}

Live with Daily CLI

Page 65: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Substitution

Command Substitution

Use the ouput of one command as the argument(s) of the othercommand.

Newer bash style: $(command). Easier to be nested and readand maintained.

Live with Daily CLI

Page 66: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Substitution

Command Substitution

Use the ouput of one command as the argument(s) of the othercommand.

Newer bash style: $(command). Easier to be nested and readand maintained.

Example

$now=$(date +%Y-%m-%d)

Live with Daily CLI

Page 67: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Substitution

Command Substitution

Use the ouput of one command as the argument(s) of the othercommand.

Newer bash style: $(command). Easier to be nested and readand maintained.

Example

$now=$(date +%Y-%m-%d)

Older sh style: `command` For backward compatiblity.

Live with Daily CLI

Page 68: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Command Substitution

Command Substitution

Use the ouput of one command as the argument(s) of the othercommand.

Newer bash style: $(command). Easier to be nested and readand maintained.

Example

$now=$(date +%Y-%m-%d)

Older sh style: `command` For backward compatiblity.

Example

$now=`date +%Y-%m-%d`

Live with Daily CLI

Page 69: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Using for Loops at the Command Line

Using for Loops at the Command Line

Refer to a bash programming book or manual details about the forloop or other loops in bash.There are two ways to write a casual for loop on the commandline: comma separated or newline separated.

To make thumbnails of your jpg files

Live with Daily CLI

Page 70: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Using for Loops at the Command Line

Using for Loops at the Command Line

Refer to a bash programming book or manual details about the forloop or other loops in bash.There are two ways to write a casual for loop on the commandline: comma separated or newline separated.

To make thumbnails of your jpg files

Example

$for file in `ls *.jpg` ; do convert $file -resize 140x140 tn $file;done

Live with Daily CLI

Page 71: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Using for Loops at the Command Line

Using for Loops at the Command Line

Refer to a bash programming book or manual details about the forloop or other loops in bash.There are two ways to write a casual for loop on the commandline: comma separated or newline separated.

To make thumbnails of your jpg files

Example

$for file in `ls *.jpg` ; do convert $file -resize 140x140 tn $file;done

Batch modify suffix:

Live with Daily CLI

Page 72: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Using for Loops at the Command Line

Using for Loops at the Command Line

Refer to a bash programming book or manual details about the forloop or other loops in bash.There are two ways to write a casual for loop on the commandline: comma separated or newline separated.

To make thumbnails of your jpg files

Example

$for file in `ls *.jpg` ; do convert $file -resize 140x140 tn $file;done

Batch modify suffix:

Example

$for i in *.xml ; do mv $i `basename $i .xml`.wsdl ; done

Live with Daily CLI

Page 73: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Using for Loops at the Command Line

Using for Loops at the Command Line(Cont.)

There are two ways to write a casual for loop on the commandline: comma separated or newline separated.

To make thumbnails of your jpg files

Example

$for file in `ls *.jpg`> do convert $file -resize 140x140 tn $file> done

> is called the secondary prompt.

Live with Daily CLI

Page 74: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

Live with Daily CLI

Page 75: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Live with Daily CLI

Page 76: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Redirecting gradually:

Live with Daily CLI

Page 77: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Redirecting gradually:

$find / -name “foo” > output.txt

Live with Daily CLI

Page 78: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Redirecting gradually:

$find / -name “foo” > output.txt$find / -name “foo” 2 > /dev/null

Live with Daily CLI

Page 79: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Redirecting gradually:

$find / -name “foo” > output.txt$find / -name “foo” 2 > /dev/null$find / -name foo > output.txt or $find / -name foo 1 >

output.txt

Live with Daily CLI

Page 80: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Redirecting gradually:

$find / -name “foo” > output.txt$find / -name “foo” 2 > /dev/null$find / -name foo > output.txt or $find / -name foo 1 >

output.txt$find / -name foo > output.txt 2 > output.txt

Live with Daily CLI

Page 81: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Redirecting gradually:

$find / -name “foo” > output.txt$find / -name “foo” 2 > /dev/null$find / -name foo > output.txt or $find / -name foo 1 >

output.txt$find / -name foo > output.txt 2 > output.txt$find / -name foo > output.txt 2 > &1

Live with Daily CLI

Page 82: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Bash I/O Redirection

Bash I/O Redirection

Redirect to save results, to make things clear, to open and closefiles for the current shell execution env and to . . . .

0: stand input stream; 1: stand output stream; 2 stand erroroutput stream

$find / -name “foo”The output you need is flooded by a lot of error messages dueto denied permission.

Redirecting gradually:

$find / -name “foo” > output.txt$find / -name “foo” 2 > /dev/null$find / -name foo > output.txt or $find / -name foo 1 >

output.txt$find / -name foo > output.txt 2 > output.txt$find / -name foo > output.txt 2 > &1$find / -name foo &> output.txt(same with above)

Live with Daily CLI

Page 83: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Useless Use of cat due to Redirection

Useless Use of cat due to Redirection

“The purpose of cat is to concatenate (or ’catenate’) files. If it’sonly one file, concatenating it with nothing at all is a waste oftime, and costs you a process.” – Randal L. SchwartzTo inspect what pages or files at your site have been visited, youcan

Example

$cat access.log ‖ awk ’print $7’

But, a nicer and neater way(due to shells’ allowing a file to be readbefore the command.):

Example

$awk ’print $7’ access.log$< access.log awk ’print $7’

Live with Daily CLI

Page 84: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Escape Accidental Exiting

Escape Accidental Exiting

Ctrl+D conveniently exits Bash. Too easy . . .

Live with Daily CLI

Page 85: Live with cli(bash)

Live with Daily CLI

Bash Tricks

Escape Accidental Exiting

Escape Accidental Exiting

Ctrl+D conveniently exits Bash. Too easy . . .

Specify that it must be pressed twice to exit, in your∼/.bashrc:export IGNOREEOF=1

Live with Daily CLI

Page 86: Live with cli(bash)

Live with Daily CLI

Great command line utils

Outline

1 Introduction

2 Customize Bash

3 Bash Tricks

4 Great command line utilsThe find && xargs PairBatch Renaming FilesNetwork toolsPrinting with lpr

Live with Daily CLI

Page 87: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

Live with Daily CLI

Page 88: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

-size(- less than the specified size; + greater than thespecified size;(neither, exactly the specified size)

Live with Daily CLI

Page 89: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

-size(- less than the specified size; + greater than thespecified size;(neither, exactly the specified size)

-user

Live with Daily CLI

Page 90: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

-size(- less than the specified size; + greater than thespecified size;(neither, exactly the specified size)

-user

-perm(- and;+ or; neither secified, match exactly)

Live with Daily CLI

Page 91: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

-size(- less than the specified size; + greater than thespecified size;(neither, exactly the specified size)

-user

-perm(- and;+ or; neither secified, match exactly)

-exec(not recommended). Make sure the escape the semicolonending arguments to exec: “\;”

Live with Daily CLI

Page 92: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

-size(- less than the specified size; + greater than thespecified size;(neither, exactly the specified size)

-user

-perm(- and;+ or; neither secified, match exactly)

-exec(not recommended). Make sure the escape the semicolonending arguments to exec: “\;”

man find

Live with Daily CLI

Page 93: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

-size(- less than the specified size; + greater than thespecified size;(neither, exactly the specified size)

-user

-perm(- and;+ or; neither secified, match exactly)

-exec(not recommended). Make sure the escape the semicolonending arguments to exec: “\;”

man find

Live with Daily CLI

Page 94: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Find files meeting your severe requirements

-name, -iname(case incensitive)

-size(- less than the specified size; + greater than thespecified size;(neither, exactly the specified size)

-user

-perm(- and;+ or; neither secified, match exactly)

-exec(not recommended). Make sure the escape the semicolonending arguments to exec: “\;”

man find

Example

$find / -name ”*.txt” -size +10k -user madsen -not -perm +o=r-exec chmod o+r

Live with Daily CLI

Page 95: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Using xargs

xargs - build and execute command lines from standard input.Compress all the files with some postfix in the current directory:

Example

$ls *.jpg | xargs tar cvfz hts.tar.gz

Live with Daily CLI

Page 96: Live with cli(bash)

Live with Daily CLI

Great command line utils

The find && xargs Pair

Combine the find and xargs

They produce power: act on special files.

Example

$find ./ -print0 -maxdepth 1 | xargs -0 grep -n -i “active”$find ./ -name “.svn” -type d | xargs rm -rf$find ./ -name “*.tar.gz” | xargs -i bash -c “echo {} && tar ztvf{} | grep ’beamer’ ”

Live with Daily CLI

Page 97: Live with cli(bash)

Live with Daily CLI

Great command line utils

Batch Renaming Files

Batch Renaming Files

There are many ways. To rename all files ending with “.aaa.bbb”to “.ccc”:

Example

$for i in `ls *.aaa.bbb`; do mv $i `echo $i | sed -e’s/aaa\.bbb$/ccc/’`; done

$rename ’s/\.aaa\.bbb$/\.ccc/’ *.aaa.bbb

Live with Daily CLI

Page 98: Live with cli(bash)

Live with Daily CLI

Great command line utils

Network tools

download: wget

It supports HTTP(+HTTP PROXY, HTTPS and FTP protocols.Some options:

Basic startup options: -b, -o logfile, -i input link file, -c

Live with Daily CLI

Page 99: Live with cli(bash)

Live with Daily CLI

Great command line utils

Network tools

download: wget

It supports HTTP(+HTTP PROXY, HTTPS and FTP protocols.Some options:

Basic startup options: -b, -o logfile, -i input link file, -c

Download options: -O output download file

Live with Daily CLI

Page 100: Live with cli(bash)

Live with Daily CLI

Great command line utils

Network tools

download: wget

It supports HTTP(+HTTP PROXY, HTTPS and FTP protocols.Some options:

Basic startup options: -b, -o logfile, -i input link file, -c

Download options: -O output download file

Recursive retrieval options: -m(-r -N -l inf –no-remove-listing),-r, -np, -k(–convert-links), -K(–backup-converted)

Live with Daily CLI

Page 101: Live with cli(bash)

Live with Daily CLI

Great command line utils

Network tools

download: wget

It supports HTTP(+HTTP PROXY, HTTPS and FTP protocols.Some options:

Basic startup options: -b, -o logfile, -i input link file, -c

Download options: -O output download file

Recursive retrieval options: -m(-r -N -l inf –no-remove-listing),-r, -np, -k(–convert-links), -K(–backup-converted)

HTTP options: -E(–html-extension)

Live with Daily CLI

Page 102: Live with cli(bash)

Live with Daily CLI

Great command line utils

Network tools

download: wget

It supports HTTP(+HTTP PROXY, HTTPS and FTP protocols.Some options:

Basic startup options: -b, -o logfile, -i input link file, -c

Download options: -O output download file

Recursive retrieval options: -m(-r -N -l inf –no-remove-listing),-r, -np, -k(–convert-links), -K(–backup-converted)

HTTP options: -E(–html-extension)

Recursive accept/reject options: -A acclist, -R rejlist

Live with Daily CLI

Page 103: Live with cli(bash)

Live with Daily CLI

Great command line utils

Network tools

download: wget

It supports HTTP(+HTTP PROXY, HTTPS and FTP protocols.Some options:

Basic startup options: -b, -o logfile, -i input link file, -c

Download options: -O output download file

Recursive retrieval options: -m(-r -N -l inf –no-remove-listing),-r, -np, -k(–convert-links), -K(–backup-converted)

HTTP options: -E(–html-extension)

Recursive accept/reject options: -A acclist, -R rejlist

get all mp3’s only from zixia:

Live with Daily CLI

Page 104: Live with cli(bash)

Live with Daily CLI

Great command line utils

Network tools

download: wget

It supports HTTP(+HTTP PROXY, HTTPS and FTP protocols.Some options:

Basic startup options: -b, -o logfile, -i input link file, -c

Download options: -O output download file

Recursive retrieval options: -m(-r -N -l inf –no-remove-listing),-r, -np, -k(–convert-links), -K(–backup-converted)

HTTP options: -E(–html-extension)

Recursive accept/reject options: -A acclist, -R rejlist

get all mp3’s only from zixia:

Example

wget -cr -np -A mp3 ftp://ftp.trueice.net

Live with Daily CLI

Page 105: Live with cli(bash)

Live with Daily CLI

Great command line utils

lftp: lftp

A Sophisticated File Transfer Program

The following is from “man lftp”

A file transfer program that allows sophisticated ftp, http andother connections to other hosts

It can handle seven file access methods - ftp, ftps, http, https,hftp, fish, sftp andfile

Live with Daily CLI

Page 106: Live with cli(bash)

Live with Daily CLI

Great command line utils

web browser: w3m

a text based Web browser

It’s a World Wide Web (WWW) text based client.

It runs in a terminal.

Tab browsing.

Less computing resources.

Live with Daily CLI

Page 107: Live with cli(bash)

Live with Daily CLI

Great command line utils

Printing with lpr

Printing with lpr

acroread and evince sucks on my dapper box.

Example

$lpr -P LaserJet-1022 -o page-set=odd -o page-ranges=1-108yqs.pdf$lpr -P LaserJet-1022 -o page-set=even -o outputorder=reverse -opage-ranges=1-108 yqs.pdfor if your printer supports doule-sided printing:$lpr -P LaserJet-1022 -o sides=two-sided-long-edgepage-ranges=1-108 yqs.pdf

Live with Daily CLI

Page 108: Live with cli(bash)

Live with Daily CLI

Summary

Summary

What’s shell

Live with Daily CLI

Page 109: Live with cli(bash)

Live with Daily CLI

Summary

Summary

What’s shell

Customize Bash(one kind of shell)

Live with Daily CLI

Page 110: Live with cli(bash)

Live with Daily CLI

Summary

Summary

What’s shell

Customize Bash(one kind of shell)

Bash tricks

Live with Daily CLI

Page 111: Live with cli(bash)

Live with Daily CLI

Summary

Summary

What’s shell

Customize Bash(one kind of shell)

Bash tricks

When using systems which aren’t configured as such . . .

Live with Daily CLI

Page 112: Live with cli(bash)

Live with Daily CLI

Summary

Summary

What’s shell

Customize Bash(one kind of shell)

Bash tricks

When using systems which aren’t configured as such . . .

Use your fingers to remember. Never try to memorize usingbrain.

Live with Daily CLI

Page 113: Live with cli(bash)

Live with Daily CLI

My Points

My Points

It’s boring to repeat typing the same thing.

Live with Daily CLI

Page 114: Live with cli(bash)

Live with Daily CLI

My Points

My Points

It’s boring to repeat typing the same thing.

Dialog boxes suck. Mouse sometimes sucks.

Live with Daily CLI

Page 115: Live with cli(bash)

Live with Daily CLI

My Points

My Points

It’s boring to repeat typing the same thing.

Dialog boxes suck. Mouse sometimes sucks.

CLI+Keyboard operation make power and productivity.

Live with Daily CLI

Page 116: Live with cli(bash)

Live with Daily CLI

My Points

My Points

It’s boring to repeat typing the same thing.

Dialog boxes suck. Mouse sometimes sucks.

CLI+Keyboard operation make power and productivity.

Communicate with others.

Live with Daily CLI

Page 117: Live with cli(bash)

Live with Daily CLI

My Points

My Points

It’s boring to repeat typing the same thing.

Dialog boxes suck. Mouse sometimes sucks.

CLI+Keyboard operation make power and productivity.

Communicate with others.

Read the manual.

Live with Daily CLI

Page 118: Live with cli(bash)

Live with Daily CLI

My Points

My Points

It’s boring to repeat typing the same thing.

Dialog boxes suck. Mouse sometimes sucks.

CLI+Keyboard operation make power and productivity.

Communicate with others.

Read the manual.

Use google.

Live with Daily CLI

Page 119: Live with cli(bash)

Live with Daily CLI

References

man bash

http://www.tldp.org/LDP/abs/html/

http://www.ukuug.org/events/linux2003/papers/bash tips/

http://www.ss64.com/bash/index.html

http://en.wikipedia.org

Live with Daily CLI

Page 120: Live with cli(bash)

Live with Daily CLI

Thanks

Thank you all!!

Live with Daily CLI