常用的 Linux 开发 Shell 脚本

Ubuntu 用户管理

  • 在 Ubuntu 中创建具有管理员权限的新用户:
    sudo adduser test  # 替换 'test' 为你想要的用户名
    sudo adduser test 
    

常用的 Linux 开发 Shell 脚本

Git 使用

删除未跟踪的文件
  • 删除未跟踪的文件:

    git clean -f
    
  • 删除未跟踪的目录:

    git clean -fd
    
  • 删除未跟踪的文件和目录,包括在 .gitignore 中指定的文件(慎用):

    git clean -xfd
    
  • 在使用 git clean 前,建议使用 -n 参数预览将要删除的文件:

    git clean -nxfd
    
解决 Git 冲突
1. git stash    
2. git pull  
3. git stash pop  # 如有冲突,解决冲突  
4. git push
克隆项目及其子模块
git clone --recursive https://github.com/onnx/onnx-tensorrt.git
放弃本地修改
# 将本地修改重置为最后一次提交的状态
git fetch --all
git reset --hard origin/master
还原文件至特定版本
git checkout <commit_hash> <filename>
git checkout <filename>
删除所有未暂存的修改
git checkout -f
强制重置所有更改,包括暂存区的修改
git reset --hard 
git clean -xdf 
修改 Git 远程地址
git remote set-url origin http://myinferofflinevideo.git

文件操作

  • 复制文件并排除特定类型:

    ls /src/ | grep -v .gz | xargs -i cp -r /src/{} /dst
    
  • 在 grep 搜索中排除特定目录:

    grep -r "test" ./* --exclude-dir=.git
    
  • 搜索特定文件类型:

    find . -name "*.c" | xargs grep "test" -n --color
    

系统信息和监控

  • 查看 GLIBCXX 版本:

    strings /usr/lib64/libstdc++.so.6 | grep GLIBCXX
    
  • 启动 rsync 守护进程:

    /usr/bin/rsync --daemon --config=/etc/rsyncd.conf
    
  • 实时监视 Nvidia GPU 内存使用情况:

    watch -n 5 nvidia-smi
    
  • 检查 CPU 信息:

    # 物理 CPU 数量
    cat /proc/cpuinfo | grep "physical id" | sort | uniq | wc -l
    
    # 每个物理 CPU 的核心数
    cat /proc/cpuinfo | grep "cpu cores" | uniq
    
    # 总逻辑 CPU 数量
    cat /proc/cpuinfo | grep "processor" | wc -l
    

Docker

容器管理
  • 停止所有 Docker 容器:

    docker rm -f $(docker ps -a -q)
    docker rmi -f $(docker images -a -q)
    
  • 查看容器的 IP 地址:

    docker inspect --format='{{.NetworkSettings.IPAddress}}' <container_id>
    
  • 导入和导出 Docker 镜像:

    docker save -o centos7.tar centos
    docker load --input centos7.tar
    
  • 后台运行程序

    nohup ./testapp >/dev/null 2>&1 &
    

FFmpeg 命令

  • 使用 FFmpeg 进行流媒体推送:

    for((;;)); do \
        ./objs/ffmpeg -re -i ./doc/source.200kbps.768x320.flv \
        -vcodec copy -acodec copy \
        -f flv -y rtmp://192.168.1.170/live/livestream; \
        sleep 1; \
    done
    
  • 将 MP4 转换为 H.264:

    ffmpeg -i input.mp4 -vcodec copy -an -vbsf h264_mp4toannexb -f h264 output.h264
    
  • 调试和解码命令:

    ffprobe -v quiet -show_packets -select_streams v test.ts 2>x | grep pts_time | wc -l
    ffprobe -v quiet -show_packets -select_streams v test.ts 2>x | grep pts
    ffmpeg -i test.ts
    ffplay -stats -f h264 test.h264
    

cURL 测试和压力测试

  • 使用 cURL 进行 POST 请求测试:

    curl -d "leaderboard_id=7778a8143f111272&score=19&app_key=8d49f16fe034b98b&_test_user=test01" "http://172.16.102.208:8089/wiapi/score"
    
  • 使用 Apache Bench (ab) 进行压力测试:

    ab -n 10 -c 1 -p get_fea-img_base64_array.json -T application/json http://10.40.27.152:8090/xxxx
    

其他常用命令和技巧

  • 循环执行脚本:

    while true; do date; echo 'hello'; sleep 1; done
    
  • 查看端口占用情况:

    netstat -tunpl | grep 22
    lsof -i:xxx
    ps -ef | grep xxx
    
  • 使用多 CPU 编译:

    processor_num=$(cat /proc/cpuinfo | grep processor | wc -l)
    make -j $processor_num
    
  • 查看系统信息:

    cat /proc/cpuinfo
    cat /proc/meminfo
    df -lh
    uname -a
    
  • 创建多个目录:

    mkdir -p /etc/confd/{conf.d,templates}
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

橘色的喵

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值