Professional Documents
Culture Documents
Get Power From Command Line
Get Power From Command Line
Mosky
Financial Freedom
Power Freedom
Mosky
Architect, Pinkoi
• Practicing Python 3
• The mediums.
5. Developing on CLI
fi
What commands?
• POSIX
• Ubuntu 22.04 LTS Server
• Visit the links, but don't read; scan them to index in your brain.
Introduction to command line
“Command Notes”
• Tags.
• Like →
Get your terminal emulator
• Windows Terminal
• GNOME Terminal / Konsole
Connect to ...
• WSL
• Ubuntu
• Or,
The container
• Get Docker
The container
• Get Docker
Checkpoint: Log into the container
• Hints:
-
-
-
-
-
-
-
-
-
• $ <Up><C-w>Ctrl-W!
• macOS Shortcuts – Mosky’s
• $ <Up><M-C-h>H! Notes
• Bash Keystrokes
Or <C-p>, if not
from the docker.
– Mosky’s Notes
• See also:
<Delete> or
• $ <Up><C-h>? <Backspace>.
• Windows Shortcuts – Mosky’s
The <M> is Notes
• Also works in
<Opt> or <Alt>.
Python shell and
many other places.
• $ <Up><your keystrokes>
Hey, Ctrl-K!
• Hints:
Quoting
Pipe, AND, OR
• $ echo 'Hello, World!' | grep Hello && echo Found! || echo Not found.
• $ echo 'Hello, World!' | grep Hey && echo Found! || echo Not found.
• explainshell.com
• $ echo 'Hello, World!' | grep Hello && echo Found! || echo Not found.
Loops
• $ while true; do date; docker container top ubuntu c; echo; sleep 3; done
Redirections
le descriptor:name = 0:stdin, 1:stdout, 2:stderr
• $ cd ~
• $ cat hello.txt
*
*
Expansions & Substitutions
• $ cd ~ • $ echo [abcx].txt
-
>
• $ echo ${name/.txt/.csv}
• Shell Style Guide § Variable
• $ echo `echo 'Hello, Echo!'` expansion
• $ echo $(echo 'Hello, Echo!')
=
=
-
-
>
-
>
-
-
>
>
-
>
Job Control
• $ kill %2
i
=
=
=
Aliases
• $ type ll • $ cd /usr/bin
• $ alias hi=history •$
• $ hi 10
.
.
-
-
.
.
History
• $ history a
• <C-r>, <C-s>: search history.
• $ history n
• $ <a long long command> #TAG
• $ history help | grep ' -[an]'
• $ <C-r>#TAG
-
-
-
-
• $ which echo
/usr/bin/echo
• Hints:
i
fi
Text processing
TSV
Create TSV le using cat
• $ cd
fi
/
/
TSV
Create TSV le using vim
• $ cd
• ~$ vi cmds.tsv
• (vim) :x<CR> # goes the command line mode, write and quit.
fi
/
/
• If any garbled text:
• or,
-
• $ wc l cmds.tsv # wc: word count • $ grep <cmds.tsv cat
# grep cat cmds.tsv
• $ cat cmds.tsv
• grep: g/re/p in the ed
• $ head cmds.tsv command, globally search for a
• $ tail cmds.tsv regular expression and print
matching lines.
• Hints:
• $ seq 1 3
• $ seq 1 3 | column
• $ seq 1 3 | column t
-
-
• $ head <cmds.tsv -1
# $ head n 1 cmds.tsv
-
-
-
• $ awk <cmds.tsv -F $'\t' '{ print $2 }' | head
-
-
• See also:
• Hints:
• “Text processing”
• “Mandatory”
=
=
&
&
-
TSV (cont.)
• $ vimdiff a b
• $ diff a b
-
-
• $ alias bat=batcat
-
-
-
cURL
• $ curl https://google.com
• $ curl v https://google.com
• $ curl -L https://google.com
-
JSON with jq
• # apt install jq y
• $ cd
• ~$ curl https://raw.githubusercontent.com/hazmole/TheGiddyLimit.github.io/master/
data/bestiary/bestiary mm.json >monsters_zh_tw.json
• $ wc l monsters.json # 70811
-
-
-
-
-
>
-
-
• $ jq <monsters.json
• $ jq <monsters.json | less
• $ jq <monsters.json -C | less -R
• $ bat monsters.json
-
-
-
-
-
• $ sort <dragons.tsv # Sort by HP.
-
-
Checkpoint:
What is the first undead monster in the book?
• $ jq ??? | tail -1
???
• Hints:
• “undead”
• “page”
JSON with gron
• $ bat dragons.json
• $ gron dragons.json
. * [a z] ^$ ? + |
$ grep
BRE . * [a z] ^$ ? + |
$ grep -G
ERE . * [a z] ^$ ? + | $ grep -E
Glob . * [a z] ^$ ? + |
-
-
-
-
-
Test the regex types
-
-
-
i
• See also:
• Regex cheatsheet for BRE, ERE, Vim, Python's re, PCRE (Perl).
• jq Manual
• tomnomnom/gron – GitHub
• yq for YAML.
Checkpoint: The regex type of less may be?
• Hints:
• $ cp dragons.tsv cats.tsv
• $ cp dragons.tsv cats_2.tsv
-
*
-
-
*
*
*
-
• $ bat dragons.csv
-
-
• $ csvcut <dragons.csv c Name,HP
• $ csvsql <dragons.csv query "select avg(HP) from stdin where Name like
'Adult%'"
-
-
-
-
-
-
-
>
-
• See also:
• Tutorial – csvkit
• $ time sleep 1
-
-
-
-
System management
File management
• $ cd • $ ls
• $ mkdir p x/y/z • $ ll t
# List and sort by time.
• $ cp b c
• $ ll -S h
• $ rm b # List and sort by size.
-
-
-
i
• $ cd
• $ ls mydir_link
• $ ll mydir_link
• $ ll mydir_backup
-
-
• $ rmdir mydir/x/y/z
# OK!
• $ rm r mydir/
• $ tree
-
>
-
-
-
-
.
.
.
Checkpoint:
Describe one difference between cp r and cp a
• Hints:
• $ cp help | grep
-
-
.
.
.
-
-
Terminology
CN 程序 进程 线程 并发性 并⾏性
Process management
• $ ps f # f: "forest"
• $ vi test.txt # and <C-z>.
• $ ps u | grep vi
• $ ps u | grep [v]i
• $ ps u | sed n e 1p e /[v]i/p
-
• $ pgrep vi # <pid> • $ fg
-
-
-
-
-
-
-
>
-
>
-
-
>
-
>
Monitoring
• $ free h
• $ top
• $ htop
• $ less +F ~/my.log
# Try <C-c> ` i` `/http` <F>.
-
-
-
• The commands which are useful but not relevant to containers:
• $ sudo iotop
• $ sudo vi /var/log/syslog
• $ sudo vi /var/log/auth.log
• Hints:
• $ tty
-
-
-
-
-
-
-
• $ sudo apt install sysstat y # or, • $ man sar | grep 'Report ' -B 1
• $ sar
# Report today's CPU stats. • sysstat/sysstat – GitHub
-
-
-
Container management
• Why?
• Once you pack your app as a
container image, it can be run
anywhere seamlessly.
fi
• (host)$ docker container commit ubuntu c ubuntu cli power
# $ docker container commit
• $ docker image ls
# $ docker images
=
=
=
=
=
=
-
.
.
.
-
-
• $ docker image save ubuntu cli power >ubuntu cli power.tar
• $ docker image ls
• $ docker image ls
=
=
=
-
-
-
-
-
=
-
=
-
-
=
-
-
-
-
=
=
=
-
-
-
.
-
-
-
-
-
-
-
-
Work on a remote machine
Run programs after disconnect
• $ tmux a # a: attach
• (tmux) <C-c><C-d>
• $ tmux ls
• (local)$ ll ~/.ssh/id_rsa
• If nothing there:
• Simply generate a new pair when you have a little doubt about the old pair.
-
• (remote)# su $new_user
-
-
Connect to a remote host
• If there is a warning and you're sure you changed something in the remote:
ssh -A: Allow the remote to use the local key pair
• The ubuntu-c works since the docker run --network enables the DNS
resolution between containers.
>
:
>
:
• (local)$ ls ~/sshfs_container
• Open http://localhost:4321/.
• localhost:4321
→ localhost:2222 → ubuntu-c:22
→ ubuntu-c:1234 → http.server 1234
Checkpoint:
Access the HTTP server by http://localhost:8765/
• Open http://localhost:8765/ and there should be a page.
• Hints:
• (remote)# iftop
• Open http://ubuntu-c/.
• For accessing the services in a private network by private DNS and standard ports!
• If the server has another public IP, try Google “my ip”.
• $ cp ~/.bashrc ~/backup_bashrc
• $ vi ~/.bashrc • $ vi ~/.bashrc
.
.
-
-
1. / n
• Search; Next.
2. i <Esc> :x
3. dd
• Delete a line.
ffi
Spell checking
• (vim) /sp<CR>
• (vim) 1<CR>
• (vim) :x<CR>
• See also:
• Hints:
Developing
Slak: Collect data from Slack like a pro. ⚡
1. System Tools: the common tools which are 2. Project Tools: the tools of this project, good to
supposed to be installed in the system.
be managed within the project by, for example,
pipenv.
• $ cd
-
-
-
• ~$ cd slak
• $ for cmd in python python3 slak; • $ for cmd in python python3 slak;
do type $cmd; done do type $cmd; done
-
i
• $ pytest cov
• $ cd htmlcov
• Open http://localhost:8000/.
-
-
-
-
-
• (my mac)$ for cmd in flake8 mypy black xxx; do type $cmd; done
flake8 is hashed (/usr/local/bin/flake8)
mypy is aliased to `\mypy ignore missing imports'
black is aliased to `black line length 79 skip string normalization'
bash: type: xxx: not found
• $ for cmd in flake8 mypy black xxx; do type $cmd >/dev/null; done
bash: type: xxx: not found
-
-
-
-
-
-
-
-
-
-
-
-
-
• $ cd ~/slak
• $ mypy slak.py
• $ black slak.py
• The di erent languages have di erent toolsets; however, the concepts and
the categories are similar, e.g.:
• See also:
• Git Documentation
• Cheat Sheets
• Reference Manual
• # apt install fd f nd y
• $ alias fd=fdf nd
• $ fd server # fd: f nd
i
-
i
i
-
i
-
i
-
-
*
• $ cd /usr/lib/python3.10
-
-
• A 10 MB–level solution.
• A GB-level solution.
-
The end
for-linuxunix-users/3443131449033542/
• “The Dragon”
• “The Terminal”
https://unsplash.com/photos/F6920BvzrZE
https://en.wikipedia.org/wiki/
Computer_terminal#/media/ • “The Miniatures”
File:DEC_VT100_terminal_transparent.png
https://unsplash.com/photos/Q4Honp3Pyqs