2. Me, Myself and I.
❖ Ex (win) sysadmin, ex (lousy) php dev
❖ Rails developer @dlabs, still a bit lousy, getting better
❖ @miha_mencin, radiokills@github
3. Its all about history
$ history
…
7550 git commit -a -m 'renaming pg to postgresql in database.yml template'
7551 git push origin master
7552 git push origin postgres
7553 rake update web web1 testing
7554 bundle
7555 rake update web web1 testing
7556 bundle update
7557 rake update web web1 testing
…
$ cat ~/.zsh_history
…
: 1378713558:0;diff
: 1378713783:0;git commit -a -m 'duplicated_payment_details'
: 1378713798:0;git push origin duplicated_payment_details:
: 1378713805:0;git push origin duplicated_payment_details
: 1378716721:0;git checkout -b alter_service_center_payment
: 1378716812:0;rails g migration add_bank_details_to_service_center bank_account bank_sorting_code
: 1378719452:0;git commit -a -m 'migration, validation, form, DIP-257'
: 1378720670:0;git merge diplicated_payment_detail
…
4. Controll your history
$ echo $HISTSIZE
1000
$ echo $HISTFILESIZE
2000
$ echo $HISTFILE
/home/miha/.zsh_history
$ vi ~/.zshrc
…
export HISTSIZE=10000
export HISTFILESIZE=2000
export HISTIGNORE=cd:cat:dir
…
Command line pr0n mode? Yes we can!
$ <space>vi love-letter-to-my-mistress.txt
5. Use your history
[CTRL] + r = (fuzzy) search through history
$ vi ~/.ssh/known_hosts
bc-i-search: vi
$ history
…
7550 git commit -a -m 'renaming pg to postgresql in database.yml template'
7551 git push origin master
7552 git push origin postgres
7553 rake update web web1 testing
7554 bundle
7555 rake update web web1 testing
7556 bundle update
7557 rake update web web1 testing
…
$ !7556
Fetching gem metadata from https://rubygems.org/...
➜ !7556
➜ bundle update
Fetching gem metadata from https://rubygems.org/...
8. sudo Re-run last command
$ nginx -s reload
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2014/10/19 12:09:52 [warn] 1800#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
2014/10/19 12:09:52 [notice] 1800#0: signal process started
2014/10/19 12:09:52 [alert] 1800#0: kill(1007, 1) failed (1: Operation not permitted)
…
$ sudo nginx -s reload
$ sudo $(fc -ln -1)
$ sudo !!
“!!” is called ‘expansion’. !22124 is also expansion.
9. More about expansions
$ cd ~
We all know it
$ !!
Repeats last command
$ !2123
Repeats command 2123 from history
$ !$
Expands to last argument. For example:
$ mkdir -p /home/miha/awesome_folder/awesome_subfolder/data
$ cd !$
$ pwd
/home/miha/awesome_folder/awesome_subfolder/data
$
(Similar thing can be achieved by pressing [Esc] and then [.]
11. Brace expansions
$ echo {1,2,3}
1 2 3
$ echo file-{a,b,c}
file-a file-b file-c
$ echo {1..5}
1 2 3 4 5
$ echo {2..-3}
2 1 0 -1 -2 -3
$ echo {a..e}
a b c d e // does not work in zsh
➜ setopt BRACE_CCL
➜ echo {a-e}
a b c d e
14. About pipes
Pipes let you use the output of a program as the input of another one
$ ls -l | grep filea1
-rw-r--r-- 1 miha staff 0 Oct 21 22:19 filea1.py
-rw-r--r-- 1 miha staff 0 Oct 21 22:19 filea1.rb
Redirection makes it possible to control where the output of a command goes to,
and where the input of a command comes from. (via bash-hackers.org)
$ ls -l | grep filea1 > myfiles.txt
$ cat mayflies.txt
-rw-r--r-- 1 miha staff 0 Oct 21 22:19 filea1.py
-rw-r--r-- 1 miha staff 0 Oct 21 22:19 filea1.rb
15. Useful piping
Task: Copy a mysql database over the ssh
local1$ ssh our-db-serv.net
dbserv$ mysqldump my_remote_db > my_remote_db.sql
local1$ scp our-db-serv.net:/path/to/my_remote_db.sql .
local1$ mysql my_local_db < my_remote_db.sql
But… It is so much work… And I need coffee
$ ssh our-db-serv.net 'mysqldump my_remote_db' > my_remote_db.sql
$ mysql my_local_db < my_remote_db.sql
Still… can we do better? Yes we can!
$ ssh our-db-serv.net 'mysqldump my_remote_db' | mysql my_local_db
16. Not everyone’s happy
Umm, yeah…. If you’d just make it - let’s say - twice as fast…
That’d be great
18. But… we really (almost) can.
$ ssh our-db-serv.net 'mysqldump my_remote_db | gzip -c' | gunzip | mysql my_local_db
But… Is it happening at all?
$ ssh our-db-serv.net 'mysqldump my_remote_db | gzip -c' | pv | gunzip | mysql my_local_db
6MiB 0:00:13 [2.35MiB/s] [ <=> ]
(In some cases we could use tee)
$ ls | tee files.txt
How much coffee can I have in meantime?
$ ssh …| pv -s 200m | gunzip | mysql my_local_db
35.9MiB 0:00:15 [2.34MiB/s] [========> ] 17% ETA 0:01:08
That is not enough time for coffee break :(
$ ssh …| pv -s 200m -L128K | gunzip | mysql my_local_db
5.38MiB 0:00:43 [ 125kiB/s] [> ] 2% ETA 0:25:57
19. Piping through ruby
$ echo "My String" | ruby -e "puts gets.downcase”
> my string
# upcase.rb
ARGF.each do |line|
puts line.upcase
end
$ ls | ruby upcase.rb
FILE1.TXT
FILE2.TXT
FILE3.TXT
FILE4.TXT
UPCASE.RB
20. Aliases
A Bash alias is essentially nothing more than a keyboard shortcut, an abbreviation,
a means of avoiding typing a long command sequence
$ alias
…
l='ls -lah'
la='ls -lAh'
ll='ls -lh'
ls='ls -G'
lsa='ls -lah'
md='mkdir -p
…
Alias scopes
$ sudo ll
sudo: ll: command not found
$ alias sudo=‘sudo ‘
$ sudo ll
-rw-r--r-- 1 miha staff 0B Oct 22 00:30 file1.txt
-rw-r--r-- 1 miha staff 0B Oct 22 00:30 file2.txt
-rw-r--r-- 1 miha staff 0B Oct 22 00:30 file3.txt
21. zsh specific aliases
Zsh specific aliases
-s (suffix aliases)
$ alias -s rb=subl
$ somefile.rb
# will open somefile.rb in sublime text
# we can set multiple filetypes also
$ alias -s {mkv,avi}=vlc
-g (gloabal aliases)
$ alias -g G=“| grep -i”
$ ls G file
file1.txt
file2.txt
file3.txt
file4.txt
22. Let’s have some fun
$ nginx -s reload
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2014/10/22 01:08:01 [warn] 2282#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
2014/10/22 01:08:01 [notice] 2282#0: signal process started
2014/10/22 01:08:01 [alert] 2282#0: kill(1038, 1) failed (1: Operation not permitted)
$ alias please='$(fc -ln -1)'
$ nginx -s reload
#error msg..
$ sudo please
$ #yeey
But… also…
$ alias bitch=‘sudo ‘
23. Let’s have some fun
$ nginx -s reload
nginx: [alert] could not open error log file: open() "/var/log/nginx/error.log" failed (13: Permission denied)
2014/10/22 01:08:01 [warn] 2282#0: the "user" directive makes sense only if the master process runs with super-user privileges, ignored in /etc/nginx/nginx.conf:1
2014/10/22 01:08:01 [notice] 2282#0: signal process started
2014/10/22 01:08:01 [alert] 2282#0: kill(1038, 1) failed (1: Operation not permitted)
24. More of zsh awesomeness
Tab completion I
Tab completion II
$ cd /u/l/b # +[TAB] =
$ cd /usr/local/bin
Tab completion III
$ git # +[TAB] =
add -- add file contents to index
am -- apply patches from a mailbox
apply -- apply patch to files and/or to index
archimport -- import an Arch repository into git
archive -- create archive of files from named tree
bisect -- find, by binary search, change that introduced a bug
blame -- show what revision and author last modified each line of a file
branch -- list, create, or delete branches
bundle -- move objects and refs by archive
cat-file -- provide content or type information for repository objects
25. More of zsh awesomeness
cd between subfolders
$ cd /home/miha/projects/dipstix/app/assets/javascripts
# Upsie, wrong project
$ cd dipstix playon
$ pwd
/home/miha/projects/playon/app/assets/javascripts
Autocorrection - can be annoying
$ spec
zsh: correct 'spec' to 'rspec' [nyae]?
$ unsetoption correct_all
Recursive listing
$ ls **/action_card*
-rw-r--r-- 1 miha staff 530B Jul 31 14:48 app/admin/action_card_priorities.rb
-rw-r--r-- 1 miha staff 1.8K Sep 30 12:07 app/admin/action_cards.rb
-rw-r--r-- 1 miha staff 1.5K Aug 13 10:00 app/controllers/api/v1/action_cards_controller.rb
-rw-r--r-- 1 miha staff 1.2K Aug 20 15:01 app/models/action_card.rb
26. More of zsh awesomeness
Edit your variables
$ vared PATH
/Users/miha/.rvm/gems/ruby-2.1.1@firely-api/bin:/Users/miha/.rvm/gems/ruby-2.1.1@global/bin:/Users/miha/.rvm/rubies/ruby-
2.1.1/bin:/Users/miha/.rvm/bin:/usr/local/heroku/bin:/usr/local/texlive/2014basic/bin/x86_64-
darwin:/usr/local/bin:/usr/local/sbin:/Users/miha/.bin:/Applications/Postgres.app/Contents/Versions/9.3/bin:/usr/local/bin:/usr/local/sbin:/
usr/bin:/bin:/usr/sbin:/sbin:/Applications/Postgres.app/Contents/Versions/9.3/bin:/bin
oh-my-zsh
oh-my-zsh is an open source, community-driven framework
for managing your Zsh configuration.
It comes bundled with a ton of helpful functions,
helpers, plugins, themes, and a few things that make you shout…
$ curl -L http://install.ohmyz.sh | sh