常用Bash命令

#查询挂载的磁盘分区数

lsblk -o MOUNTPOINTS | grep -E "/mnt/sd[a-z]+[1-9]$" |wc -l

#查询系统识别的磁盘

lsblk -l -o Name |grep -E "sd[a-z]*[a-z]$"|wc -l

 

#从指定目录 查询包含某文件类型的所有子目录-->并输出到指定目录

find /mnt -type d -exec sh -c 'find {} -maxdepth 1 -name "*.plot" | grep -q .' \; -print > /home/test/plot_dir_output.txt

 


#查询指定目录及子目录中 指定类型文件数量

find /path/to/directory -type f -name "*.jpg" | wc -l

 


#从指定目录 查询包含某文件类型的所有目录 格式化后输出-->并输出到指定目录

find /mnt -type d -print0 | while IFS= read -r -d ''; do
txt_count=$(find "$REPLY" -maxdepth 1 -name '*.fpt' -type f | wc -l)
if [[ $txt_count -gt 0 ]]; then
printf " - %s\n" "$REPLY" | tee -a /home/tuoluo/plot_dir_output.txt
fi
done

 

#从指定目录 查询包含某文件类型的所有目录 ->分批次 格式化后输出-->并输出到指定目录

#生成新文件前删除旧文件
find ./ -type f -name "plot*.txt" -delete
#循环生成
index=0 find /mnt -type d -print0 | while IFS= read -r -d ''; do txt_count=$(find "$REPLY" -maxdepth 1 -name '*.plot' -type f | wc -l) if [[ $txt_count -gt 0 ]]; then
#运算符前加 expr i=$( expr $i + 1) tmp=$(expr $i / 100) echo "==========this is third line $tmp============" printf " - %s\n" "$REPLY" | tee -a /home/test/plot_dir_output_$tmp.txt fi done

 

#脚本调用

#!/bin/bash

# 删除 /home/test 目录下的所有 .log 文件
find /home/tuoluo -type f -name "*.log" -delete

# 执行 mount_disks.sh 脚本以 root 权限
sudo ./mount_disks.sh

# 检查 mount_disks.sh 是否成功执行,如果成功,则执行 会话 脚本
if [ $? -eq 0 ]; then
   # 创建并启动 Screen 会话,使用 root 权限执行/home/tuoluo/ smh-miner下的命令
   screen -dmS smh-miner sudo bash -c "cd /home/tuoluo/smh/smh-miner &&  echo "yes"| ./h9*64 "
   screen -x smh-miner

  
else
    echo "start  smh-miner failed"
fi

批量执行多个会话:

#!/bin/bash

# 创建并启动 Screen 会话,使用 root 权限执行/home/test/1下的命令
chmod +x  /home/test/1/1
screen -dmS session1 sudo bash -c "cd /home/test/1 && ./1"

chmod +x  /home/test/2/2
# 创建并启动 Screen 会话,使用 root 权限执行/home/test/2下的命令
screen -dmS session2 sudo bash -c "cd /home/test/2 && ./2"

chmod +x  /home/test/3/3
# 创建并启动 Screen 会话,使用 root 权限执行/home/test/3下的命令
screen -dmS session3 sudo bash -c "cd /home/test/3 && ./3"
#screen -x  session3

  

posted @ 2024-04-05 21:09  zhihua  阅读(60)  评论(0)    收藏  举报