命令和应用示例

  • folder size, directory size, and sort
du -s your/path/* | sort -n -r
du -hd 1 . | sort -hr
# -s 是foldersize, 我是加了*才按文件夹输出,不然就是输出整个目录对应的整个文件夹
# -h 是human readable, 就是让数字用1M , 1G这样显示, 而不是1,024, 1,048,576这么显示
#    据说大部分GNU程序都是这样
# -k 不用-h可以用-k,单位成了kilobytes
# -n 是numeric
# -r 是reverse
# -d 目录层数
du -a your/path/ | sort -nr | hear -n 10
# -a 文件和文件夹都算上,这里就没有*什么事了,直接给路径
# head限制输出,取前n个
  • word count, line count ls -la | wc -l
  • 移除man pages: sudo apt-get remove -y --purge man-db
  • 查dns相关
nslookup google.com 15.64.64.53
host google.com
host -t A google.com 8.8.8.8
  • 查看dns服务器: /etc/resolv.conf
  • ip address 类似于ifconfig, 但不需要安装
  • 检查使用的哪个shell: echo $0
  • 检查可用的shell: cat /etc/shells
  • 切换shell: chsh -s /bin/sh
  • 用dnsmasq注意tcp/udp 53端口
  • bash/shell 脚本, 赋值时等号左右不能有空格, 做字符串比较时必须有空格 <<<???
  • 命令行查看天气预报: curl http://wttr.in/
  • 打印可读的man命令man ksh | col -b > ksh.txt
    • 直接>ksh.txt会有大量控制符
  • 确认系统使用systemd: ps -p 1 -o comm=
  • 查看用户ID id root
  • 查看版本 uname -a
  • 查看版本(简单)uname -r
  • 查看cpuuname -m
  • 查看linux发行版的名称及版本号cat /etc/issue
  • 内存和cpu等使用情况top //然后按1
  • 查看内存情况free -m
  • 更改用户主目录usermod -d newdir username
  • 删除文件夹 rm -rf dirname
  • 删除文件 rm filename
  • 生成多级文件夹 mkdir -p data/ghost -p 参数可以生成该路径中任何一个不存在的文件夹
  • 进入root sudo -i (exit退出)
  • 当前文件夹全文搜索: grep -r “keywords" *
  • 打印环境变量: printenv Name
  • 同步.bash_profile, .bashrc if [ -f ~/.bashrc ]; then source ~/.bashrc fi
  • 设代理(只影响当前终端) export https_proxy=http://proxyserveraddress:3128
    • 取消代理 unset https_proxy

添加启动项

/etc/systemd/system/编写个.service的启动项配置文件, 然后用systemctl来管理, 具体见系统服务

其它方法

  • vim /etc/rc.local
  • vim /etc/init.d/rc.local
  • 也可以在/etc/init.d/里面添加对应的脚本文件
    • 这样重启的话用/etc/init.d/xxxx restart
  • 或在如下目录放置会sh脚本,系统启动后会自动执行/etc/profile.d/
    • 有改动后要更新:
      • update-rc.d -f shadowsocks defaults
    • 移除后要记得remove
      • update-rc.d -f shadowsocks remove

总结

  • To run a daemon or any command as root before login,
    • add it to /etc/rc.local
    • or add a cron job (crontab -e) with @reboot as its time field.
    • You can also write a proper init script based on /etc/init.d/skeleton.
  • To run a command after login,
    • add it to ~/.bash_profile (provided you're using bash). Contrary to ~/.bashrc, this file will be executed by login shells only.
  • To run a command after login of any new user,
    • add an appropriate .bash_profile to /etc/skeland new users will get this file automatically on account creation.

常用目录

  • /usr/lib/systemd/system/ 系统安装的软件默认启动脚本目录

  • /etc/systemd/system/ 用户根据自己需要建立的启动脚本目录

  • /etc/sysconfig/ 服务初始化选项目录

  • /var/lib/ 服务运行时产生的数据存储目录

  • /etc/xxx/ 各服务配置目录

  • 服务的输出用如下命令观察: sudo journalctl -fu home-assistant@pi

  • zsh没读.bash_profile:
    • bash 或 bash -l命令, 看提示, 比如要求你chsh -s /bin/zsh, 运行后即可

curl, wget等

带cookie下载

* 保存: 
wget -qO- --keep-session-cookies --save-cookies cookies.txt --post-data 'umail=walker.wzy%40gmail.com&upass=walker&remember=1&login_submit=' https://www.pianoteq.com/user_area
* 下载: 
wget --load-cookies cookies.txt https://www.pianoteq.com//downloads/gen/dl191022191024y7C5N32Sq5/pianoteq_stage_setup_v660.dmg
  • 快速curl post数据的例子
curl -X POST -H "Content-Type:application/json" \
-d '{"email": "[email protected]", "password": "secret"}' \
http://localhost:3000/api/users
  • 先定义变量怎么玩:
ACCESS_TOKEN=6Nb2ti5QEXIoDBS5FQGWIz4poRFiBCMMYJbYXSGHWuulOuy0GTEuGx2VCEVvbpBK
VERB=POST # any verb is allowed
curl -X VERB -H "Authorization: $ACCESS_TOKEN" \
http://localhost:3000/api/users/logout
  
# Query Parameter
curl -X VERB http://localhost:3000/api/users/logout?access_token=$ACCESS_TOKEN

问题是, 这些变量定义在哪? 当前环境?

用户和组

添加组, 把用户加入组, 并生效:

sudo group add xxxx
sudo gpasswd -a $USER xxxx
  • 登出再登入可立即生效, 或: newgrp xxxx
  • 更改用户组/权限sudo chown -R $(whoami) /usr/local/share/man/
  • 改用户路径 usermod -m -d /path/to/you/select username (-m的意思是移动, -d是设置)
  • 改用户组 chown -R user:group path/to/file/or/dir
    • ↑↑↑把path/to/file/or/dir的所有权改为user用户, 及group组(:group可省略)
    • -R是影响所有文件
  • 添加用户useradd -m walker -s /usr/sbin/nologin
    • -m: 同时创建用户目录 /home/walker
    • -s: shell 上例中创建了一个不能登录的用户
  • 修改密码 passwd walker
  • 查看当前用户 whoami

挂载

  • 挂载NFS
sudo mkdir /home/pi/TVShows
sudo nano /etc/fstab
    192.168.0.50:/nfs/TVShows /home/pi/TVShows nfs nouser,atime,auto,rw,dev,exec,suid 0    0
sudo mount -a
sudo rpcbind start 或 service rpcbind restart/start
# 卸载
umount /my/path -l

如果存在权限问题, 要在服务端设置下UID

  • 查看挂接的设备 df -lh

防火墙

ufw status
sudo ufw allow 'Nginx Full'
sudo ufw delete allow 'Nginx HTTP'

停止一个程序

#!/bin/bash
echo "Stopping Kcptun..."
PID=`ps -ef | grep server_linux_amd64 | grep -v grep | awk '{print $2}'`
if [ "" !=  "$PID" ]; then
echo "killing $PID"
kill -9 $PID
fi
echo "Kcptun stoped."

# 或

ps -ef | grep supervisord
kill -s SIGTERM 2503 (如果查到的pid是2503)

复制移动和重命名

  • 查找后复制/移动 find then mv:
find path_A -name "*AAA*" -print0 | xargs -0 -I {} mv {} path_B
  • print0 是把结果从换行变成\0
  • -0是以\0为分隔符, 所以-0 = -d '\0`
  • -I {} 是的意思是以{}为占位符, 因为move a b这个命令, 我们的a是动态的. 或
find path_A -maxdepth 1 -name "*AAA*" -exec mv {} path_B \; 
# 1, 去掉-maxdepth则是递归
# 2, 如果是递归, 最好不要把mv后的路径设在本路径以下, 否则会在移动文件后继续索引到(即结果集是动态的)

扩展: 搜索一批扩展名:

find . -regex ".*\.\(avi\|mp4\|wmv\)" # 在一些系统里, 可能需要加-E来支持:
find -E . regex ".*\.(avi|mp4|wmv)" # 不需要频繁转义, 反而更简洁
  • 批量重命名
for filename in *.png; do mv "$filename" "hd_$filename"; done;

以上,见xargsRename

xargs 应用

  • 上面的批量操作都用了,查看下
  • 删除结果里的所有文件: find ./ -name ".svn" | xargs rm -Rf

查ssl 证书

echo | openssl s_client -showcerts -servername www.google.com -connect www.google.com:443 2>/dev/null | openssl x509 -inform pem -noout -text

kcptun/shadowsocks几个命令

iptables -L | grep 29900
systemctl status shadowsocks-libev
supervisorctl status kcptun

# kcptun 配好后打开防火墙
iptables -I INPUT -p udp --dport 29900 -j ACCEPT
# 保存iptables —> ubuntu
service iptables save
service iptables restart
# 或
sudo apt-get install iptables-persistent
sudo /etc/init.d/iptables-persistent save 
sudo /etc/init.d/iptables-persistent reload
# 或 (Ubuntu 16.04 Server)
sudo netfilter-persistent save
sudo netfilter-persistent reload

pip把文件安装到哪了?

pip3 show you-get | grep Location

CPU信息 Linux系统中的CPU信息存在于/proc/cpuinfo文件中,如果想了解全部的信息,可以直接查看这个文件。 有多少个物理CPU? cat /proc/cpuinfo | grep 'physical id' | sort | uniq |wc -l 有多少个虚拟CPU? cat /proc/cpuinfo | grep ^processor | sort | uniq |wc -l CPU是几个核心的? cat /proc/cpuinfo | grep 'cpu cores' | uniq

解压文件

unzip xxx.zip  //当前位置解压zip文件,rar文件怎么办?
# rar-free
sudo apt-get install unrar-free
unrar e some.rar
# 7zip
sudo apt-get install p7zip-full
7z e some.rar
# The e in both cases means extract obviously.
# //也可以用官方rar:
wget http://www.rarlab.com/rar/rarlinux-....tar.gz
tar zxvf rar....tar.gz
cd rar
make
make install
  • apt uninstall a app apt-get autoremove rar
  • 查看端口被什么程序占用 lsof -i:8080
  • 结束占用端口的进程: killall <name>

进程

netstat -apn | grep <portnumber>
ps aux | grep <portnumber>

# 列出所有守护进程
service --status-all

# 杀进程  (kill -s 9 <pid>)
kill -9 <pid> 

iptables

  • 命令后加-D, 可以把应用程序退到后台

  • 查看哪些端口被打开 netstat -anp

  • 关闭端口号:

iptables -A INPUT -p tcp --drop 端口号 -j DROP
iptables -A OUTPUT -p tcp --dport 端口号 -j DROP 
  • 打开端口号:iptables -A INPUT -p tcp --dport 端口号 -j ACCEPT
  • 最后两行总是:
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
iptables -A INPUT -j DROP
  • drop后再打开就没意义了, 所以要把-A(append)换成-I(insert),插入到第一行
  • 查看生效的input链 iptables -nvx -L INPUT
  • 查看iptables vi /etc/sysconfig/iptables
  • 你可以通过iptables-save 和iptables-restore命令来保存配置。
  • 查看端口映射 iptables -t nat -nL

任务管理

常用任务管理命令

  • jobs //查看任务,返回任务编号n和进程号
  • bg %n //将编号为n的任务转后台运行
  • fg %n //将编号为n的任务转前台运行
  • ctrl+z //挂起当前任务
  • ctrl+c //结束当前任务

网卡

  • 设置网卡
sudo nano /etc/network/interfaces

iface eth0 inet dhcp
# 替换为
iface eth0 inet static
address 192.168.1.100 (此处 192.168.1.100为分配的固定ip)
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8
# 重启网络
sudo /etc/init.d/networking stop
sudo /etc/init.d/networking start

安装php7

$ sudo apt-get install software-properties-common
$ sudo add-apt-repository ppa:ondrej/php
$ sudo apt-get update
$ sudo apt-get install -y php7.0
$ sudo apt-get install php7.0-curl php7.0-cgi php7.0-fpm libapache2-mod-php7.0 php7.0-mysql php7.0-curl php7.0-json

启动vi/vim出错

  • VIM: E558: Terminal entry not found in terminfo
  • TERM=xterm vim 这样启动
  • 或者直接设成环境变量 export TERM=xterm
  • 可以echo $TERM输出查看

增加mac的bcopy等命令

alias pbcopy='xclip -selection clipboard'
alias pbpaste='xclip -selection clipboard -o'

系统语言环境:

  • 查看: locale
  • 查看安装语言(字符集): locale -a
  • 安装: locale-gene en_US.UTF-8
  • 也有这种做法:
    • sudo apt-get install -y language-pack-en-base
    • sudo LC_ALL=en_US.UTF-8

shell里中文乱码

  • locale -a 查看可用的字符集
  • export LC_ALL=zh_CN.utf8(GBK, GB18030)….取决于上一步查到的字符集
  • locale 查看被环境所应用的字符集

自动补全, 忽略大小写

编辑~/.inputrc(没有的话,就新建一个),在最后加一行: set completion-ignore-case on

补充:

~/.inputrc中加入:

\e[A”: history-search-backward
“\e[B”: history-search-forward  

能够让你在命令行输入字符之后,就可以用方向键Up,Down来搜索以该串字符开头的历史命令。

或者在~/.bashrc中加入:

bind '"\e[A": history-search-backward' 
bind '"\e[B": history-search-forward'

也能达到同样的效果。

根据进程查程序位置, 配置文件位置 Get the path of running Apache

$ ps -ef | grep apache
apache   12846 14590  0 Oct20 ?        00:00:00 /usr/sbin/apache2
Append -V argument to the path 

$ /usr/sbin/apache2 -V | grep SERVER_CONFIG_FILE
-D SERVER_CONFIG_FILE="/etc/apache2/apache2.conf”

Reference: http://commanigy.com/blog/2011/6/8/finding-apache-configuration-file-httpd-conf-location

通过curl检查服务器健康程度

curl -sf https://wuhan.guahao-inc.com:8443 -o /dev/null || echo "down"
s: silent S: show error f:带错误码失败 o:输出位置

文件类型

# 通过查mime-type

file -b -mime-type myppicture.heic
# 输出:
ISO Media, HEIF Image HEVC Main or Main Still Picture Profile

file -b -mime-type anotherpicture.png 
# 输出:
JPEG image data, JFIF standard 1.01, aspect ratio, density 216x216, segment length 16, Exif Standard: [TIFF image data, big-endian, direntries=5, orientation=upper-left, xresolution=74, yresolution=82, resolutionunit=2], baseline, precision 8, 1170x2532, components 3

# 通过查16进制文件头:

hexdump -n 16 -C mypicture.heic
# 输出
00000000  00 00 00 28 66 74 79 70  68 65 69 63 00 00 00 00  |...(ftypheic....|
00000010

hexdump -n 16 -C anotherpicture.png
# 输出
00000000  ff d8 ff e0 00 10 4a 46  49 46 00 01 01 00 00 d8  |......JFIF......|
00000010

都不会受错误的扩展名影响

帮你自动输入交互命令里的Yes

如下命令会停在你要选择Y还是n的地方(Y大写表示默认选择Y):

pip install ffmpeg-downloader
ffdl install --add-path

这么做:

yes | poetry run ffdl install --add-path