command line
the useful command line on linux
the test command platform is ubuntu 20.11.3 of armbian
common
in bash , use the Tab to complete arguments or list all available commands and ctrl - r to search through command histor
in bash , use the ctrl-w to delete last wrd, and ctrl-u to delete the all line. ctrl-e to move cursor to end of line. ctrl-k to delete cursor to the end line, ctrl-l to clear the screen
use the history to see recent commands. follow with !n to excute again. !$ for last argument and !! for last command.
use the cd to get directory, access files relative to you home directory with the ~ prefix, in sh scripts refer to eht home dirctory as $HOME
use the cd - can to go back to previous working directory
pstree -p is a helpful display of the process tree
use the pgrep and pkill to find or signal processes by name (-f is helpful)
use nohup or disown to keep running a background process forever
check what processes are listening via
netstat -lntp
orss -plat
orlsof -iTCP -sTCP:LISTEN -p -n
use the
lsof
andfuser
for open sockets and filesuse the
uptime
orw
to know how long time the system been runninguse the alias to create shortcuts for commonly used commands. for example
alias ll='ls -latr'
create a new aliasll
, save aliases, shell settings ,and functions you commonly use in~/.bashrc
put the settings of enviroment variables as well as commands tha should be excuted when you login in
~/.bash_profile
in bash scripts, use
set -x
(or the variantset -v
which logs raw input, includeing unexpanded variables and comments) for debugging out
put , use stric model unless you hanve a good reason not to: useset -e
to abort on error (nonzero exit code) . useset -u
to detect unset variable usages. considerset -o popefail
too, to abort on errors withon ppes. for more involved scripts also usetrap
on EXIT or ERR, a useful habit is start a script like this, which will make it detect an abort on common error and print a message:
1 | set -e pipefail |
- in bash scripts, subshells are convenient ways to group commands, A common example is to temporatily move to a different working directory
1 | # do somethin in current dir |
- the output of a command can be treated like a file via
<(some command)
(know as process substitution) for example compare local /etc/hosts with a remote one:
1 | diff /etc/hosts <(ssh somehost cat /etc/hosts) |
a simple web server for all files in the current directory
python -m http.server 8888
for switching the shell to another user use
su username
orsu - username
the default username to root.
Processing files and date
to locate a file by name in the current directory
find . -iname "*something*"
to find a file anywhere by name, uselocate somethin
user the
grep -r
to search throuth source or date file. includingto conver HTML to text:
lynx -dump -stdin
for markdown , HTML and all kinds for document conversion, try
pandoc
, for example to convert a markdown document to word format:pandoc README.md --form markdown --to docx -o tmp.docx
use
xmlstarlet
to handle XML ,it is old but good.for JSON use jq, for YAML use shyaml, for Excel or CSV files use
scvkit
.you can set a specitic command;s environment by refixing its invocation with the enviroment var iable settings, as in
TZ=Asia/Shanghai date
date and time : to get the current date and time in the helful ISO 8601 format. use
date -u +"%Y-%m-%dT%H:%M:%SZ"
, to manipluate date and time expressions , ue dateadd, datadiff, strtime.use
zless
zmore
zcat
andzgrep
to operate on compressed fils.
System debugging
for web debugging
curl
andcurl -I
are handy, or theirwget
equivalents, or the more modernhttpie
use the
top
orhtop
wo show current cpu/disk status,iostat
andiotop
use theiostat -mxz 15
for basic cpu and detiled per partion disk stas and performance insightuse
netstat
andss
can show network donnection details.for a quick overview of what’s happening on a system.
dstat
is expecially useful, for broadest overview with details useglances
to know memory status, run and understand the output of
free
andvmstat
, in particular be aware the cached values is memeory held by the linux kernel as file cache, so effectively counts toward the free valuefor looking at why a disk is full
ncdu
saves time over the usual commands liedu -sh *
to find which socket or process is using band width try
iftop
ornethogs
the
ab
tool is helpful for quick-and-dirty checking of web server performance . for more complex load testing. trysiege
for more serious network debugging
wireshark
tshark
orngrep