Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Live with cli(bash)
1. Live with Daily CLI
Live with Daily CLI
Madsen
sen.zhang@gmail
Insitute of Computing Technology
November 6, 2006
Live with Daily CLI
2. Live with Daily CLI
Powered by
This slide is powered by
Linux
GNU
GNU Emacs
TEX and LTEX
A
LTEX beamer
A
Live with Daily CLI
3. Live with Daily CLI
Introduction
Outline
1 Introduction
What’s CLI
What’s “shell”
Shell Types
Types of Commands in Bash
2 Customize Bash
3 Bash Tricks
4 Great command line utils
Live with Daily CLI
4. Live with Daily CLI
Introduction
About What
What’s This About
Some tricks and tips to customize and make better use of the
Bash.
Live with Daily CLI
5. Live with Daily CLI
Introduction
About What
What’s This About
Some tricks and tips to customize and make better use of the
Bash.
*NOT* about bash programming.
Live with Daily CLI
6. Live with Daily CLI
Introduction
What’s CLI
What’s CLI
Command Line, On a display screen, the space following a
prompt (such as $) where a text instruction to a computer or
device is typed.
Live with Daily CLI
7. Live with Daily CLI
Introduction
What’s CLI
What’s CLI
Command Line, On a display screen, the space following a
prompt (such as $) where a text instruction to a computer or
device is typed.
CLI, a user interface in which you type commands consisting
of text instead of choosing them from a menu or selecting an
icon.
Live with Daily CLI
8. Live with Daily CLI
Introduction
What’s CLI
What’s CLI
Command Line, On a display screen, the space following a
prompt (such as $) where a text instruction to a computer or
device is typed.
CLI, a user interface in which you type commands consisting
of text instead of choosing them from a menu or selecting an
icon.
A significant minority of computer users prefer to use CLIs,
some due to visual disability, but most because they feel that
CLIs provide an environment with enhanced productivity.
Live with Daily CLI
9. Live with Daily CLI
Introduction
What’s CLI
What’s CLI
Command Line, On a display screen, the space following a
prompt (such as $) where a text instruction to a computer or
device is typed.
CLI, a user interface in which you type commands consisting
of text instead of choosing them from a menu or selecting an
icon.
A significant minority of computer users prefer to use CLIs,
some due to visual disability, but most because they feel that
CLIs provide an environment with enhanced productivity.
What on earth can CLI do then?
Live with Daily CLI
10. Live with Daily CLI
Introduction
What’s CLI
What’s CLI
Command Line, On a display screen, the space following a
prompt (such as $) where a text instruction to a computer or
device is typed.
CLI, a user interface in which you type commands consisting
of text instead of choosing them from a menu or selecting an
icon.
A significant minority of computer users prefer to use CLIs,
some due to visual disability, but most because they feel that
CLIs provide an environment with enhanced productivity.
What on earth can CLI do then?
CLI can run many many programs, perform specific task: Web
browsing(w3m), music playing(mpg321), displaying tasks(top)
receiving and sending emails(mutt+esmpt+fetchmail),
BBS(ssh), editing files(emacs,vi), file management(mc), file
transfer(scp, lftp), downloading(wget), and so many . . .
Live with Daily CLI
11. Live with Daily CLI
Introduction
What’s “shell”
What’s “shell”
“shell”: any program that users use to type commands to the
system.
Live with Daily CLI
12. Live with Daily CLI
Introduction
What’s “shell”
What’s “shell”
“shell”: any program that users use to type commands to the
system.
hard outer covering of eggs, of nut-kernels, of some seeds and
fruits, and of animals such as oysters, snails, crabs and
tortoises.
Live with Daily CLI
13. Live with Daily CLI
Introduction
What’s “shell”
What’s “shell”
“shell”: any program that users use to type commands to the
system.
hard outer covering of eggs, of nut-kernels, of some seeds and
fruits, and of animals such as oysters, snails, crabs and
tortoises.
It hides the details of the underlying operating system behind
the shell’s interface
Live with Daily CLI
14. Live with Daily CLI
Introduction
What’s “shell”
What’s “shell”
“shell”: any program that users use to type commands to the
system.
hard outer covering of eggs, of nut-kernels, of some seeds and
fruits, and of animals such as oysters, snails, crabs and
tortoises.
It hides the details of the underlying operating system behind
the shell’s interface
“shell” provides a kind of CLI.
Live with Daily CLI
15. Live with Daily CLI
Introduction
What’s “shell”
What’s “shell”
“shell”: any program that users use to type commands to the
system.
hard outer covering of eggs, of nut-kernels, of some seeds and
fruits, and of animals such as oysters, snails, crabs and
tortoises.
It hides the details of the underlying operating system behind
the shell’s interface
“shell” provides a kind of CLI.
The word “shell” also refers to a particular program, namely
the Bourne shell or (shortly) sh or Bash.
Live with Daily CLI
16. 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.(View
the shell types at http://en.wikipedia.org/wiki/Unix shell and
http://www.freebsdsoftware.org.).
Live with Daily CLI
17. 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.(View
the shell types at http://en.wikipedia.org/wiki/Unix shell and
http://www.freebsdsoftware.org.).
Bash is Bourne-like. “Bash” is an acronym for ‘Bourne-Again
Shell’.
Live with Daily CLI
18. 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.(View
the shell types at http://en.wikipedia.org/wiki/Unix shell and
http://www.freebsdsoftware.org.).
Bash is Bourne-like. “Bash” is an acronym for ‘Bourne-Again
Shell’.
Focus on Bash and some utilities only today.
Live with Daily CLI
19. 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
20. 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
21. 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 programming
language: vars, flow control, quoting and functions.
Live with Daily CLI
22. 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 programming
language: vars, flow control, quoting and functions.
Power of Bash: embed and combine them in your bash scripts
using the Bash programming language.
Live with Daily CLI
23. Live with Daily CLI
Customize Bash
Outline
1 Introduction
2 Customize Bash
Customize the Bash Prompt
How to Set Environment Variables
Aliases
3 Bash Tricks
4 Great command line utils
Live with Daily CLI
25. Live with Daily CLI
Customize Bash
How to Set Environment Variables
How to Set Environment Variables
Environment variables affects many things.Use “export” to set
them.
Example
To add “∼/bin” to the commands searching path:
$export PATH=$PATH:∼/bin
or to be permanent to your bash, append to your ∼/.bashrc:
export PATH=$PATH:∼/bin
Live with Daily CLI
26. 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 aliased
commands.
Live with Daily CLI
27. 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 aliased
commands.
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
28. 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 aliased
commands.
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
29. Live with Daily CLI
Bash Tricks
Outline
1 Introduction
2 Customize Bash
3 Bash Tricks
Make “cd” More Convenient
Better Use of Bash History
Brace Expansion
Command Substitution
Using for Loops at the Command Line
Bash I/O Redirection
Escape Accidental Exiting
4 Great command line utils
Live with Daily CLI
30. Live with Daily CLI
Bash Tricks
Tab Completion
Make Use of Tab Completion
Enjoy intelligent tab completion.
Install bash completion(available at
http://www.caliban.org/bash/index.shtml#completion)
Live with Daily CLI
31. Live with Daily CLI
Bash Tricks
Tab Completion
Make Use of Tab Completion
Enjoy intelligent tab completion.
Install bash completion(available at
http://www.caliban.org/bash/index.shtml#completion)
Source it in your ∼/.bashrc or ∼/.bash profile:
Live with Daily CLI
32. Live with Daily CLI
Bash Tricks
Tab Completion
Make Use of Tab Completion
Enjoy intelligent tab completion.
Install bash completion(available at
http://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
33. Live with Daily CLI
Bash Tricks
Tab Completion
Make Use of Tab Completion
Enjoy intelligent tab completion.
Install bash completion(available at
http://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
34. Live with Daily CLI
Bash Tricks
Tab Completion
Make Use of Tab Completion
Enjoy intelligent tab completion.
Install bash completion(available at
http://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
35. 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
36. 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
37. 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 mode
for reference.
Live with Daily CLI
38. 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
39. 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, and
execute the result as shell commands. Bash attempts to
invoke $FCEDIT, $EDITOR, and emacs as the editor, in that
order.
C-t Transpose characters.
C-w Delete backward one string.
Live with Daily CLI
40. 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 same
text, 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 to
execute it, and it finishes, the next command in the bash
history after the current one found will be placed on the
command line ready for you to use.
Volatile alias: $cd /a/rather/long/path #rlp
Then later: $C-rrlp<Enter>
Env vars: HISTSIZE, HISTFILE, HISTFILESIZE(man bash for
detail).
Live with Daily CLI
41. 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
42. 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
44. 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 to
vs. overwrite).
Env vars: HISTFILE, HISTFILESIZE, HISTSIZE,
HISTIGNORE
in ∼/.bashrc:
Live with Daily CLI
45. 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 to
vs. overwrite).
Env vars: HISTFILE, HISTFILESIZE, HISTSIZE,
HISTIGNORE
in ∼/.bashrc:
Set env vars affecting Bash history
export HISTIGNORE=”&:ls:ll:[fb]g:exit:svn up:cvs up”
export HISTFILESIZE=2000
export HISTSIZE=500
Live with Daily CLI
46. 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 on
disk.
Write before new prompt
export PROMPT COMMAND=’history -a’
Before displaying the next prompt, write the previous line to
disk.
Saving history lines is a security problem. Trade-off. Make the
decision yourself:).
Live with Daily CLI
47. 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
48. 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 former
commands.(Recommendedand preferred, personally)
Live with Daily CLI
49. 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 former
commands.(Recommendedand preferred, personally)
Repeat exactly the last command with all the original
arguments to it: “!xyz”(or even simpler, just “!!”).
Live with Daily CLI
50. 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 former
commands.(Recommendedand preferred, personally)
Repeat exactly the last command with all the original
arguments to it: “!xyz”(or even simpler, just “!!”).
Examples:
Live with Daily CLI
51. 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 former
commands.(Recommendedand preferred, personally)
Repeat exactly the last command with all the original
arguments 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
52. 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 of
earlier commands.
!-1 or !! is the whole previous command.
Live with Daily CLI
53. 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 of
earlier commands.
!-1 or !! is the whole previous command.
!-1:0 or !:0 or !!:0 is the previous command name.
Live with Daily CLI
54. 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 of
earlier 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 previous
command.
Live with Daily CLI
55. 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 of
earlier 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 previous
command.
!* or !:* is all the arguments of the previous command.
Live with Daily CLI
56. 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 of
earlier 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 previous
command.
!* or !:* is all the arguments of the previous command.
!-3:1, !-3:*
Live with Daily CLI
57. 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 of
earlier 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 previous
command.
!* or !:* is all the arguments of the previous command.
!-3:1, !-3:*
Live with Daily CLI
58. 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 of
earlier 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 previous
command.
!* 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:2
do??
Live with Daily CLI
59. Live with Daily CLI
Bash Tricks
Make Your SPACE Magic
Make Your SPACE Magic
Expand history when press SPACE in addition to its original space
inserting function. Add the following lines to your ∼/.inputrc:
Magic SPACE
$if Bash
Space: magic-space
$endif
Example
$ll *.txt
$!:0<SPACE>
or
$!!<SPACE>
Live with Daily CLI
61. 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
62. 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} boy
great better boy great boost boy great loveqq boy great redgreen
boy
Live with Daily CLI
63. 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} boy
great better boy great boost boy great loveqq boy great redgreen
boy
Backup configuration file:
Live with Daily CLI
64. 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} boy
great better boy great boost boy great loveqq boy great redgreen
boy
Backup configuration file:
Example
$sudo cp /etc/apt/sources.list{,.bak}
Live with Daily CLI
65. Live with Daily CLI
Bash Tricks
Command Substitution
Command Substitution
Use the ouput of one command as the argument(s) of the other
command.
Newer bash style: $(command). Easier to be nested and read
and maintained.
Live with Daily CLI
66. Live with Daily CLI
Bash Tricks
Command Substitution
Command Substitution
Use the ouput of one command as the argument(s) of the other
command.
Newer bash style: $(command). Easier to be nested and read
and maintained.
Example
$now=$(date +%Y-%m-%d)
Live with Daily CLI
67. Live with Daily CLI
Bash Tricks
Command Substitution
Command Substitution
Use the ouput of one command as the argument(s) of the other
command.
Newer bash style: $(command). Easier to be nested and read
and maintained.
Example
$now=$(date +%Y-%m-%d)
Older sh style: `command` For backward compatiblity.
Live with Daily CLI
68. Live with Daily CLI
Bash Tricks
Command Substitution
Command Substitution
Use the ouput of one command as the argument(s) of the other
command.
Newer bash style: $(command). Easier to be nested and read
and maintained.
Example
$now=$(date +%Y-%m-%d)
Older sh style: `command` For backward compatiblity.
Example
$now=`date +%Y-%m-%d`
Live with Daily CLI
69. 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 for
loop or other loops in bash.
There are two ways to write a casual for loop on the command
line: comma separated or newline separated.
To make thumbnails of your jpg files
Live with Daily CLI
70. 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 for
loop or other loops in bash.
There are two ways to write a casual for loop on the command
line: 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
71. 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 for
loop or other loops in bash.
There are two ways to write a casual for loop on the command
line: 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
72. 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 for
loop or other loops in bash.
There are two ways to write a casual for loop on the command
line: 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
73. 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 command
line: 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
74. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
Live with Daily CLI
75. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to denied permission.
Live with Daily CLI
76. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to denied permission.
Redirecting gradually:
Live with Daily CLI
77. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to denied permission.
Redirecting gradually:
$find / -name “foo” > output.txt
Live with Daily CLI
78. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to denied permission.
Redirecting gradually:
$find / -name “foo” > output.txt
$find / -name “foo” 2 > /dev/null
Live with Daily CLI
79. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to 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
80. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to 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
81. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to 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
82. 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 close
files for the current shell execution env and to . . . .
0: stand input stream; 1: stand output stream; 2 stand error
output stream
$find / -name “foo”
The output you need is flooded by a lot of error messages due
to 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
83. 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’s
only one file, concatenating it with nothing at all is a waste of
time, and costs you a process.” – Randal L. Schwartz
To inspect what pages or files at your site have been visited, you
can
Example
$cat access.log awk ’print $7’
But, a nicer and neater way(due to shells’ allowing a file to be read
before the command.):
Example
$awk ’print $7’ access.log
$< access.log awk ’print $7’
Live with Daily CLI
84. Live with Daily CLI
Bash Tricks
Escape Accidental Exiting
Escape Accidental Exiting
Ctrl+D conveniently exits Bash. Too easy . . .
Live with Daily CLI
85. 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
86. Live with Daily CLI
Great command line utils
Outline
1 Introduction
2 Customize Bash
3 Bash Tricks
4 Great command line utils
The find && xargs Pair
Batch Renaming Files
Network tools
Printing with lpr
Live with Daily CLI
87. 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
88. 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 the
specified size;(neither, exactly the specified size)
Live with Daily CLI
89. 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 the
specified size;(neither, exactly the specified size)
-user
Live with Daily CLI
90. 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 the
specified size;(neither, exactly the specified size)
-user
-perm(- and;+ or; neither secified, match exactly)
Live with Daily CLI
91. 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 the
specified size;(neither, exactly the specified size)
-user
-perm(- and;+ or; neither secified, match exactly)
-exec(not recommended). Make sure the escape the semicolon
ending arguments to exec: “;”
Live with Daily CLI
92. 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 the
specified size;(neither, exactly the specified size)
-user
-perm(- and;+ or; neither secified, match exactly)
-exec(not recommended). Make sure the escape the semicolon
ending arguments to exec: “;”
man find
Live with Daily CLI
93. 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 the
specified size;(neither, exactly the specified size)
-user
-perm(- and;+ or; neither secified, match exactly)
-exec(not recommended). Make sure the escape the semicolon
ending arguments to exec: “;”
man find
Live with Daily CLI
94. 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 the
specified size;(neither, exactly the specified size)
-user
-perm(- and;+ or; neither secified, match exactly)
-exec(not recommended). Make sure the escape the semicolon
ending 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
95. 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
96. 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
97. 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
98. 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
99. 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
100. 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
101. 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
102. 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
103. 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
104. 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
105. 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 and
other connections to other hosts
It can handle seven file access methods - ftp, ftps, http, https,
hftp, fish, sftp andfile
Live with Daily CLI
106. 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
107. 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-108
yqs.pdf
$lpr -P LaserJet-1022 -o page-set=even -o outputorder=reverse -o
page-ranges=1-108 yqs.pdf
or if your printer supports doule-sided printing:
$lpr -P LaserJet-1022 -o sides=two-sided-long-edge
page-ranges=1-108 yqs.pdf
Live with Daily CLI
108. Live with Daily CLI
Summary
Summary
What’s shell
Live with Daily CLI
109. Live with Daily CLI
Summary
Summary
What’s shell
Customize Bash(one kind of shell)
Live with Daily CLI
110. Live with Daily CLI
Summary
Summary
What’s shell
Customize Bash(one kind of shell)
Bash tricks
Live with Daily CLI
111. 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
112. 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 using
brain.
Live with Daily CLI
113. Live with Daily CLI
My Points
My Points
It’s boring to repeat typing the same thing.
Live with Daily CLI
114. 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
115. 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
116. 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
117. 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
118. 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
119. 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
120. Live with Daily CLI
Thanks
Thank you all!!
Live with Daily CLI