SlideShare a Scribd company logo
The Art of Command Line
2021-02-22
Kenta Yamamoto (@i05)
1
Unix Philosophy - "The Art of UNIX Programming" (2003)
2
$ history | awk '{ print $2 }' | sort | uniq -c | awk '$1>1' | sort -r | head
791 g
172 cat
115 git
83 echo
71 brew
70 yarn
68 ll
50 dig
49 gcloud
43 jj
Make each program do one thing well. To do a new job, build
afresh rather than complicate old programs by adding new
features.
“
“
3
For example, you have to sort the output before uniq because they
only do one thing:
$ history | awk '{ print $2 }' | sort | uniq -c | awk '$1>1' | sort -r | head
4
Execute a shell script:
#!/usr/bin/env bash
# command-ranking.sh
set -euo pipefail
history | awk '{ print $2 }' | sort | uniq -c | awk '$1>1' | sort -r
$ chmod u+x ./command-ranking.sh
$ bash ./command-ranking.sh | head
5
Interface - standard streams:
STDIN
STDOUT
STDERR
$ time curl -IL https://example.com/ 2>&1 > file.txt
$ time curl -IL https://example.com/
...
curl -IL https://example.com/ 0.02s user 0.01s system 5% cpu 0.467 total
6
Parameters - $0 , $1 , $# , $@
#!/usr/bin/env bash
# sample.sh
cat -
echo "file=$0, 1st-arg=$1, 2nd-arg=$2, arg-num=$# args=$@"
$ echo "Hello" | bash $(pwd)/sample.sh foo bar baz
Hello
file=/Users/kyamamoto/src/bitbucket.org/i05/support/sample.sh, 1st-arg=foo, 2nd-arg=bar, arg-num=3 args=foo bar baz
7
Sounds of Silence - $?
$ touch ~/tmp/sample.txt
$ echo $?
0
$ mv no-such-file.txt /tmp/
mv: cannot stat 'no-such-file.txt': No such file or directory
$ echo $?
1
8
Subroutine - $(command)
$ curl "https://jpnic.rdap.apnic.net/ip/$(dig +short myip.opendns.com @resolver1.opendns.com)" 
| jq .
9
Construct argument list - xargs :
$ find . -type f -name "*" ! -path '*/.svn/*' ! -path '*/.git/*' -print0 
| xargs -0 grep --ignore-case --line-number 'foo'
Parallel execution - xargs :
$ find . -type f -not -name '*.bz2' -print0 
| xargs -0 -L 1 -P $(sysctl -n hw.ncpu) bzip2
10
Multibyte encoding - nkf :
$ find . -type f -name "*.txt" | xargs nkf --guess
$ find . -type f -print0 | xargs -0 nkf --overwrite -w
11
Multiline:
TEXT_LIST=$(cat << EOF
__HERE_DOCUMENT__
__HERE_DOCUMENT__
EOF
)
12
Runs everywhere:
case "${OSTYPE}" in
# Mac
darwin*)
[ -f ~/.zshrc.osx ] && . ~/.zshrc.osx
;;
# Linux
linux*)
[ -f ~/.zshrc.linux ] && . ~/.zshrc.linux
;;
esac
13
Pitfall
Mac - BSD
or consider installing brew install gnu-sed instead
$ LANG=C find . -type f -name "*" ! -path '*/.svn/*' ! -path '*/.git/*' -print0 
| xargs -0 sed -i '~' -e 's///gi'
Linux - GNU
$ find . -type f -name "*" ! -path '*/.svn/*' ! -path '*/.git/*' -print0 
| xargs -0 sed -i -e 's///gi'
14
Pitfall
Mac - BSD
$ LANG=C git ls-files -z | xargs -0 sed -i '' -e 's///gi'`
Linux - GNU
$ git ls-files -z | xargs -0 sed -i -e 's///gi'
15
Git subcommands:
$ git grep 'Could not find the specified attachment on the server'
16
Release your brain resource:
Easily navigate directories - go with fewer cd s!
rupa/z, or
wting/autojump
Remote repository management - let the tool decide where to
locate the cloning repositories
x-motemen/ghq
17
Recommended commands:
jq
awk
rsync
time
watch
18
Linter:
koalaman/shellcheck: ShellCheck, a static analysis tool for shell
scripts
19
$ echo "Thank you!"
20
21

More Related Content

PDF
Gogo shell
PDF
LINE iOS開発で実践しているGit tips
DOCX
RedHat/CentOs Commands for administrative works
PPTX
Comets notes
KEY
My life as a beekeeper
PPT
Unix 5 en
KEY
Clojure + MongoDB on Heroku
PPT
01 linux basics
Gogo shell
LINE iOS開発で実践しているGit tips
RedHat/CentOs Commands for administrative works
Comets notes
My life as a beekeeper
Unix 5 en
Clojure + MongoDB on Heroku
01 linux basics

What's hot (20)

TXT
Find and zip files
PDF
Fast and cost effective geospatial analysis pipeline with AWS lambda
PDF
The Lesser Known Features of ECMAScript 6
PDF
37562259 top-consuming-process
PDF
File Space Usage Information and EMail Report - Shell Script
PDF
The Unbearable Lightness: Extending the Bash shell
PDF
Docker tips & tricks
PDF
Bash Script Disk Space Utilization Report and EMail
PDF
Go初心者がGoでコマンドラインツールの作成に挑戦した話
PDF
Gitosis on Mac OS X Server
PDF
KubeCon EU 2016: Custom Volume Plugins
PDF
GoLang & GoatCore
PDF
ES6 - Level up your JavaScript Skills
PDF
Apache Hadoop for System Administrators
PDF
JIP Pipeline System Introduction
PPTX
Naughty And Nice Bash Features
PDF
Plone Conference 2008 Lightning Talk Static Zope Rpx
PDF
Introduction to Resque
PDF
Character_Device_drvier_pc
Find and zip files
Fast and cost effective geospatial analysis pipeline with AWS lambda
The Lesser Known Features of ECMAScript 6
37562259 top-consuming-process
File Space Usage Information and EMail Report - Shell Script
The Unbearable Lightness: Extending the Bash shell
Docker tips & tricks
Bash Script Disk Space Utilization Report and EMail
Go初心者がGoでコマンドラインツールの作成に挑戦した話
Gitosis on Mac OS X Server
KubeCon EU 2016: Custom Volume Plugins
GoLang & GoatCore
ES6 - Level up your JavaScript Skills
Apache Hadoop for System Administrators
JIP Pipeline System Introduction
Naughty And Nice Bash Features
Plone Conference 2008 Lightning Talk Static Zope Rpx
Introduction to Resque
Character_Device_drvier_pc
Ad

Similar to The Art of Command Line (2021) (20)

PDF
Im trying to run make qemu-nox In a putty terminal but it.pdf
PDF
Workshop on command line tools - day 2
PPTX
Linux Shell Scripting
PDF
One-Liners to Rule Them All
PDF
Apache Hadoop Shell Rewrite
PPT
ShellAdvanced shell scripting programm.ppt
PPT
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
ODP
DevChatt 2010 - *nix Cmd Line Kung Foo
DOCX
Introduction to Unix - POS420Unix  Lab Exercise Week 3 BTo.docx
ODP
Perl one-liners
KEY
Advanced Shell Scripting
PDF
Shell实现的windows回收站功能的脚本
PDF
Shell scripting
PDF
Unleash your inner console cowboy
DOCX
Really useful linux commands
PDF
2010 Smith Scripting101
PDF
ODP
Unix tips and tricks
PDF
BASH Variables Part 1: Basic Interpolation
PDF
Neoito — *NIX kungfu for web devs
Im trying to run make qemu-nox In a putty terminal but it.pdf
Workshop on command line tools - day 2
Linux Shell Scripting
One-Liners to Rule Them All
Apache Hadoop Shell Rewrite
ShellAdvanced shell scripting programm.ppt
ShellAdvanced aaäaaaaaaaaaaaaaaaaaaaaaaaaaaa
DevChatt 2010 - *nix Cmd Line Kung Foo
Introduction to Unix - POS420Unix  Lab Exercise Week 3 BTo.docx
Perl one-liners
Advanced Shell Scripting
Shell实现的windows回收站功能的脚本
Shell scripting
Unleash your inner console cowboy
Really useful linux commands
2010 Smith Scripting101
Unix tips and tricks
BASH Variables Part 1: Basic Interpolation
Neoito — *NIX kungfu for web devs
Ad

More from Kenta Yamamoto (10)

PDF
Vulnerability Detection Based on Git History
PDF
[論文紹介] VCC-Finder: Finding Potential Vulnerabilities in Open-Source Projects ...
PDF
文字コードとセキュリティ
PDF
良いUrlを設計する
PDF
私たちは何を Web っぽいと感じているのか
PDF
Tips for bash script
PDF
優れたビデオゲームに共通する不変の法則
PDF
20110805 ui14課題2
KEY
20110804 ui14課題
KEY
東日本大震災後の訪日外国人数の変移_2011.3
Vulnerability Detection Based on Git History
[論文紹介] VCC-Finder: Finding Potential Vulnerabilities in Open-Source Projects ...
文字コードとセキュリティ
良いUrlを設計する
私たちは何を Web っぽいと感じているのか
Tips for bash script
優れたビデオゲームに共通する不変の法則
20110805 ui14課題2
20110804 ui14課題
東日本大震災後の訪日外国人数の変移_2011.3

Recently uploaded (20)

PDF
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
PDF
Adobe Illustrator 28.6 Crack My Vision of Vector Design
PDF
Which alternative to Crystal Reports is best for small or large businesses.pdf
PDF
Understanding Forklifts - TECH EHS Solution
PPTX
history of c programming in notes for students .pptx
PPTX
ai tools demonstartion for schools and inter college
PDF
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
PDF
medical staffing services at VALiNTRY
PPTX
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
PDF
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
PPTX
Odoo POS Development Services by CandidRoot Solutions
PDF
Digital Strategies for Manufacturing Companies
PPTX
Operating system designcfffgfgggggggvggggggggg
PPTX
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
PDF
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
PPTX
L1 - Introduction to python Backend.pptx
PDF
How Creative Agencies Leverage Project Management Software.pdf
PDF
How to Choose the Right IT Partner for Your Business in Malaysia
PDF
Navsoft: AI-Powered Business Solutions & Custom Software Development
PDF
How to Migrate SBCGlobal Email to Yahoo Easily
Why TechBuilder is the Future of Pickup and Delivery App Development (1).pdf
Adobe Illustrator 28.6 Crack My Vision of Vector Design
Which alternative to Crystal Reports is best for small or large businesses.pdf
Understanding Forklifts - TECH EHS Solution
history of c programming in notes for students .pptx
ai tools demonstartion for schools and inter college
T3DD25 TYPO3 Content Blocks - Deep Dive by André Kraus
medical staffing services at VALiNTRY
Oracle E-Business Suite: A Comprehensive Guide for Modern Enterprises
SAP S4 Hana Brochure 3 (PTS SYSTEMS AND SOLUTIONS)
Odoo POS Development Services by CandidRoot Solutions
Digital Strategies for Manufacturing Companies
Operating system designcfffgfgggggggvggggggggg
Lecture 3: Operating Systems Introduction to Computer Hardware Systems
Raksha Bandhan Grocery Pricing Trends in India 2025.pdf
L1 - Introduction to python Backend.pptx
How Creative Agencies Leverage Project Management Software.pdf
How to Choose the Right IT Partner for Your Business in Malaysia
Navsoft: AI-Powered Business Solutions & Custom Software Development
How to Migrate SBCGlobal Email to Yahoo Easily

The Art of Command Line (2021)

  • 1. The Art of Command Line 2021-02-22 Kenta Yamamoto (@i05) 1
  • 2. Unix Philosophy - "The Art of UNIX Programming" (2003) 2
  • 3. $ history | awk '{ print $2 }' | sort | uniq -c | awk '$1>1' | sort -r | head 791 g 172 cat 115 git 83 echo 71 brew 70 yarn 68 ll 50 dig 49 gcloud 43 jj Make each program do one thing well. To do a new job, build afresh rather than complicate old programs by adding new features. “ “ 3
  • 4. For example, you have to sort the output before uniq because they only do one thing: $ history | awk '{ print $2 }' | sort | uniq -c | awk '$1>1' | sort -r | head 4
  • 5. Execute a shell script: #!/usr/bin/env bash # command-ranking.sh set -euo pipefail history | awk '{ print $2 }' | sort | uniq -c | awk '$1>1' | sort -r $ chmod u+x ./command-ranking.sh $ bash ./command-ranking.sh | head 5
  • 6. Interface - standard streams: STDIN STDOUT STDERR $ time curl -IL https://example.com/ 2>&1 > file.txt $ time curl -IL https://example.com/ ... curl -IL https://example.com/ 0.02s user 0.01s system 5% cpu 0.467 total 6
  • 7. Parameters - $0 , $1 , $# , $@ #!/usr/bin/env bash # sample.sh cat - echo "file=$0, 1st-arg=$1, 2nd-arg=$2, arg-num=$# args=$@" $ echo "Hello" | bash $(pwd)/sample.sh foo bar baz Hello file=/Users/kyamamoto/src/bitbucket.org/i05/support/sample.sh, 1st-arg=foo, 2nd-arg=bar, arg-num=3 args=foo bar baz 7
  • 8. Sounds of Silence - $? $ touch ~/tmp/sample.txt $ echo $? 0 $ mv no-such-file.txt /tmp/ mv: cannot stat 'no-such-file.txt': No such file or directory $ echo $? 1 8
  • 9. Subroutine - $(command) $ curl "https://jpnic.rdap.apnic.net/ip/$(dig +short myip.opendns.com @resolver1.opendns.com)" | jq . 9
  • 10. Construct argument list - xargs : $ find . -type f -name "*" ! -path '*/.svn/*' ! -path '*/.git/*' -print0 | xargs -0 grep --ignore-case --line-number 'foo' Parallel execution - xargs : $ find . -type f -not -name '*.bz2' -print0 | xargs -0 -L 1 -P $(sysctl -n hw.ncpu) bzip2 10
  • 11. Multibyte encoding - nkf : $ find . -type f -name "*.txt" | xargs nkf --guess $ find . -type f -print0 | xargs -0 nkf --overwrite -w 11
  • 13. Runs everywhere: case "${OSTYPE}" in # Mac darwin*) [ -f ~/.zshrc.osx ] && . ~/.zshrc.osx ;; # Linux linux*) [ -f ~/.zshrc.linux ] && . ~/.zshrc.linux ;; esac 13
  • 14. Pitfall Mac - BSD or consider installing brew install gnu-sed instead $ LANG=C find . -type f -name "*" ! -path '*/.svn/*' ! -path '*/.git/*' -print0 | xargs -0 sed -i '~' -e 's///gi' Linux - GNU $ find . -type f -name "*" ! -path '*/.svn/*' ! -path '*/.git/*' -print0 | xargs -0 sed -i -e 's///gi' 14
  • 15. Pitfall Mac - BSD $ LANG=C git ls-files -z | xargs -0 sed -i '' -e 's///gi'` Linux - GNU $ git ls-files -z | xargs -0 sed -i -e 's///gi' 15
  • 16. Git subcommands: $ git grep 'Could not find the specified attachment on the server' 16
  • 17. Release your brain resource: Easily navigate directories - go with fewer cd s! rupa/z, or wting/autojump Remote repository management - let the tool decide where to locate the cloning repositories x-motemen/ghq 17
  • 19. Linter: koalaman/shellcheck: ShellCheck, a static analysis tool for shell scripts 19
  • 20. $ echo "Thank you!" 20
  • 21. 21