5
一些常用的Shell命令总结
source link: https://syaning.github.io/2021/05/16/shell-commands/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
一些常用的Shell命令总结
May 16, 2021
格式化输出
# http://linuxcommand.org/lc3_adv_tput.php
# for example:
# echo "${red}Error ${normal}something wrong"
if test -t 1; then # if terminal
ncolors=$(which tput > /dev/null && tput colors) # supports color
if test -n "$ncolors" && test $ncolors -ge 8; then
termcols=$(tput cols)
bold="$(tput bold)"
underline="$(tput smul)"
standout="$(tput smso)"
normal="$(tput sgr0)"
black="$(tput setaf 0)"
red="$(tput setaf 1)"
green="$(tput setaf 2)"
yellow="$(tput setaf 3)"
blue="$(tput setaf 4)"
magenta="$(tput setaf 5)"
cyan="$(tput setaf 6)"
white="$(tput setaf 7)"
fi
fi
Generate Random String
参考 bash generate random alphanumeric string.
# bash generate random 32 character alphanumeric string (upper and lowercase) and
NEW_UUID=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1)
# bash generate random 32 character alphanumeric string (lowercase only)
cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 32 | head -n 1
# Random numbers in a range, more randomly distributed than $RANDOM which is not
# very random in terms of distribution of numbers.
# bash generate random number between 0 and 9
cat /dev/urandom | tr -dc '0-9' | fold -w 256 | head -n 1 | head --bytes 1
如果报错 head: illegal option -- -
,加环境变量 LC_CTYPE=C
即可。
Crontab
- crontab guru - The quick and simple editor for cron schedule expressions.
* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)
# list cron jobs
$ crontab -l
# edit crontab file
$ crontab -e
$ EDITOR=vi crontab -e
# add a cron job
$ (crontab -l; echo "*/1 * * * 1-5 echo hello") | crontab -
# add a cron job with no duplication
$ croncmd="*/1 * * * 1-5 echo hello"
$ (crontab -l | grep -v -F "$croncmd"; echo "$croncmd") | crontab -
# remove a cron job
$ crontab -l | grep -v -F "$croncmd" | crontab -
Logrotate
# /etc/logrotate.d/xxx
/path/to/your/logfile {
daily # or weekly/monthly/yearly
size 100M # or 100/100k/100G
rotate 7
compress
delaycompress
missingok
notifempty
create 644 root root
postrotate
/usr/bin/killall -HUP rsyslogd
endscript
}
Troubleshooting: Logrotate not rotating file after file size exceeds the limit
Generate SSH Key
$ ssh-keygen -t rsa -b 4096 -C "[email protected]"
Set Alive Interval
# ~/.ssh/config
Host *
ServerAliveInterval 30
Host example
HostName 1.1.1.1
User testuser
SSL/TLS
Generate self-signed certificate
# genrate private key
$ openssl genrsa -out server.key 2048
# generate certificate signing request
$ openssl req -new -sha256 -key server.key -out server.csr
# generate self-signed certificate
$ openssl x509 -req -days 365 -in server.csr -signkey server.key -out server.crt
Hardware
$ arch
$ dpkg --print-architecture
# list cpu info
$ lscpu
# cpu info
$ cat /proc/cpuinfo
Memory
# memory usage
$ free
$ free -m
# memory info
$ cat /proc/meminfo
# memory statistics
$ vmstat -s
# file system disk usage
$ df -h
# list block devices
$ lsblk
Other
# list hardware configuration
$ lshw
$ lshw -short
# list usb
$ lsusb
# list pci
$ lspci
Recommend
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK