Skip to content

shell

○ 执行脚本

sh
# 脚本中的所有命令以 root 权限运行,启动新 shell 执行脚本
sudo sh script.sh
sudo bash script.sh

# 脚本中的所有命令以 root 权限运行,脚本需添加可执行权限
chmod +x script.sh
sudo ./script.sh

# 脚本中的所有命令以 root 权限运行,使用当前用户的环境变量
sudo -E ./script.sh

○ 历史命令执行

  • ctrl+r: 搜索命令执行
  • history | grep ${keyword} 搜索命令,!${number} 执行序号对应的命令

○ 结束程序

sh
lsof -i:7890
ss -tuln | grep :7890
netstat -tuln | grep :7890

ps -ef | grep ${command_keyword}

kill -9 <PID>

○ 修改文件所属用户和组

mihomo 程序需要修改所属用户组为 wheelroot 后才能正常启用

sh
# 仅改用户 user1
# 仅改组 :group1
sudo chown user1:group1 example.txt

○ 查看 ip 地址

sh
ip addr

○ ssh 连接

sh
ssh root@${ip} -p ${port}

○ 查找并删除文件

sh
# -mindepth 1 -maxdepth 1,只查找一级子内容,不找自身和更深层级的子目录
find ~/work/github-project -mindepth 1 -maxdepth 1 -not -name '.git' -exec rm -rf {} \;

○ 查找文本中匹配的行

sh
# 打印 README.md 文件中,带有 pnpm 的行
awk '/pnpm/ {print}' README.md
awk '/error/ {print}' 2025.log

○ 替换文件中的文本内容

https://felo.ai/search/oVzmbYWjhM4SP26SnWGyBT

sh
# 写入文本
echo "add content" > target.txt
cat source.txt > target.txt
# 追加文本
echo "add content" >> target.txt
cat source.txt >> target.txt
访问量 PV:Blocked用户数 UV:Blocked