SHELL 命令

nohup 后台进程

1指标准输出;2指错误输出,所以写成这样:command 1>output 2>error & ,正常的输出在output文件里,错误的输出在error文件里, command >output 2>error &

(1可以省略)

忽略错误输出:command 1>output 2>/dev/null &

忽略标准输出:command 1>/dev/null 2>error &

忽略全部输出:command 1>/dev/null 2>/dev/null &

忽略全部输出command >/dev/null 2>&1 &

错误输出和标准输出在一个文件:command 1>output 2>&1 &

错误输出和标准输出在一个文件:command 1>output 2>output & (X这种方式错误)

这样的文件输出,每次运行会覆盖现有的文件,如果我们希望追加而不是覆盖,那么就用»符号,这样命令就是: command 1»output 2»error &

如果想退出窗口乃至退出登录仍然保持程序运行,再加上nohup,形如:nohup command 1>output 2>&1 &

nohup sh run.sh 1>run_info.log 2>run_error.log &

查看系统

显示系统版本

uname -m
# armv7l
uname -a # 显示所有信息
Linux pi 5.10.17-v7l+ #1414 SMP Fri Apr 30 13:20:47 BST 2021 armv7l GNU/Linux
cat /proc/version
# Linux version 5.4.0-77-generic (buildd@lgw01-amd64-028) (gcc version 9.3.0 (Ubuntu 9.3.0-17ubuntu1~20.04)) #86-Ubuntu SMP Thu Jun 17 02:35:03 UTC 2021

查看IP地址

  • 方式一: ifconfig | grep “inet ” | grep -v 127.0.0.1
  • 方式二:ifconfig en1 有时候是 en0
  • 外网地址 curl ifconfig.me

文件命令

  • 查看文件夹 du -h –max-depth=1 your_dest_dir
  • 查看文件夹大小 du -sh <目录> (查看目录大小)
  • 查看文件详细内容 ls -lht
  • 从远程服务器拷贝: rcp
  • 复制到远程服务器: scp

查找文件 locating linux files

find . -name thisfile.txt
find /home -name *.jpg
find . -type f -empty
find ./ -type f -name "*0325*" // 文件名中含有 0325 的文件

查找并删除

find . -type f -name "*.DS_Store*" -exec rm -f {} \;

TAR 压缩解压

下面五个参数基本够用,更详细参数移步tar 命令详解

  • z : 使用gzip压缩,否则仅仅打包不压缩。
  • c : 创建新文件。
  • v : verbose 处理过程输出信息
  • f : 指定备份文件
  • x : 解压

压缩大文件显示进度

download

# mac
tar -cf - questions.sql | pv -s $(($(du -sk questions.sql | awk '{print $1}') * 1024)) | gzip > question.tar.gz
# ubuntu
tar -cf - questions.sql | pv -s $(du -sk questions.sql | awk '{print $1}') | gzip > question.tar.gz

进程以及命令

查找

ps -ef #all process
ps -fC sshd # sshd 是名字
pgrep -fl ssh* #按名字查找,使用通配符
pgrep -fa mongo* # 显示所有信息!!!!!
pgrep mongod #按名字精确查找
ps -aux | grep mongo

pgrep -l 列出程序名和进程ID; -o 进程起始的ID; -n 进程终止的ID;

进程控制

  • 将任务放在后台运行:command + &
  • 将任务丢到后台暂停:ctrl-z
  • 查看后台所有任务状态:jobs -l (会列出 1、2、3序列号)
  • 将后台的任务拿到前台处理:fg %jobnumber (指序列号,而不是任务id)
  • 将后台的任务变成运行中:bg %jobnumber
  • 管理后台当中的任务:kill -signal %jobnumber

kill -9 <pid> 可以强制终止一个进程(是数字9,而不是字母g)

查看命令的目录

whereis ttyd
ttyd: /usr/local/bin/ttyd

查看用户

用户ID id -u <username>

定时任务

$ crontab -l #查看任务
$ crontab -e #编辑任务
$ crontab -r #删除任务

语法规则: 分钟(0-59) 小時(0-23) 日期(1-31) 月份(1-12) 星期(0-6)0和7都代表星期天

查找当前目录中三天前的文件并删除

find ./ -mtime +3 | xarg rm

隐藏文件夹

主要是mac系统上使用。快速命令,打开访达窗口,然后 ⌘ + ⇧ + . 就可以隐藏/显示。


//显示隐藏文件
defaults write com.apple.finder AppleShowAllFiles -bool true
//不显示隐藏文件
defaults write com.apple.finder AppleShowAllFiles -bool false

systemctl

# 列出所有已经加载的systemd units
systemctl
systemctl | grep docker.service
# 列出所有service
systemctl list-units --type=service
systemctl --type=service
#列出所有active状态(运行或退出)的服务
systemctl list-units --type=service --state=active
# 列出所有正在运行的服务
systemctl list-units --type=service --state=running
systemctl list-units --type service --state running,failed
systemctl list-unit-files --state=enabled

Htop

交互命令 1)

  • shift + m 按内存大小排序
    • M:按照内存使用百分比排序,对应MEM%列;
    • P:按照CPU使用百分比排序,对应CPU%列;
    • T:按照进程运行的时间排序,对应TIME+列;
    • K:隐藏内核线程;
    • H:隐藏用户线程;
  • shift + h 收缩线程
  • q 退出

端口映射

// 查看已经连接的服务端口(ESTABLISHED)
netstat -a
// 查看所有的服务端口(LISTEN,ESTABLISHED)
netstat -ap
// 查看指定端口,可以结合grep命令:
netstat -ap | grep 8080
// 也可以使用lsof命令:
lsof -i:8888
// 若要关闭使用这个端口的程序,使用kill + 对应的pid
kill -9 PID号

CURL RESTfull

用CURL 发送 post 命令

参数 -d 指定数据

curl -X POST -d "name=John&age=30" https://example.com/api

还可以指定数据类型

curl -X POST -H "Content-Type: application/json" -d '{"name":"John","age":30}' https://example.com/api

发送邮件

curl --url "smtps://smtp.example.com:465" \
     --ssl-reqd \
     --mail-from "[email protected]" \
     --mail-rcpt "[email protected]" \
     --user "username:password" \
     --upload-file - <<EOF

Subject: Test Email
From: [email protected]
To: [email protected]

Dies ist eine Test-E-Mail, die von curl gesendet wurde.
EOF
it/server/shell_command.txt · 最后更改: 2023-10-12 19:05 由 goldentianya
回到顶部
CC Attribution-Share Alike 4.0 International
Driven by DokuWiki Recent changes RSS feed Valid CSS Valid XHTML 1.0