bash代码片段snippets

张开发
2026/4/8 4:26:55 15 分钟阅读

分享文章

bash代码片段snippets
文章目录bash代码片段批量改文件extentionread读取多列数据去除tree输出的特殊字符移除重复行按指定field排序按起始10个字符去重复od输出二进制文件数据文件分割一份文件复制两次计算文本文件每行的md5生成所有文件md5sumffmpeg批量提取多个视频中图像printout.shpadding.pldispatch.shfind数字列表glob通配符匹配数学计算sortwatchteecygpathcygwin下straceconvt.sh 剔除OpenFOAM头部注释wget下载所有pdf文件设置用户sudo权限获取脚本所在绝对路径OCR CaptureAssociative Arrays命令行参数getoptsunload所有已加载模块BoomerAMG参数比较两个文件夹内同名数据文件top监控进程资源使用情况tar和zstd并行打包压缩数据7z打包压缩分割提取文件名功能FFMPEG超级压缩视频文件du命令按大小排序rsync 排除目录和文件字符串操作How-to: Redirection and Process Substitution网络资源bash代码片段批量改文件extention# Rename all *.txt to *.textforfilein*.txt;domv--$file${file%.txt}.textdoneread读取多列数据whilereadx z y;doofxag.1/$x.pngifxag/$y.pngcp$if$ofdonedb.log去除tree输出的特殊字符tree-L2|seds/\xc2\xa0/ /g~/Downloads/1.txtdu-sh-t100M *移除重复行#整行比较awk!seen[$0]file#第一个field比较awk!seen[$1]file按指定field排序#按第二个field排序sort-k2,2file按起始10个字符去重复uniq-w10fileod输出二进制文件数据od-x-v-An--width100./x.binod.txt文件分割cat../xtotal.pad|split-a4-d-l52一份文件复制两次n0;forxinx*;doyprintfy%04d$n((nn1))cp$x$yyprintfy%04d$n((nn1))cp$x$ydone计算文本文件每行的md5catod.txt|perl -MDigest::MD5md5_hex -nlEsay md5_hex($_)db_x.txt生成所有文件md5summd5sum x*ffmpeg批量提取多个视频中图像n1;forxinround.0/*;doecho$x;fmtframe${n}.%04d.png;ffmpeg-i$x-vffps2$fmt;((nn1));doneprintout.sh#!/bin/bashhexpattern0-9a-fpattern$LZSKGHVRMTBCDEFinput$1cat$input|seds/\s//g|tr$hexpattern$pattern|awkBEGIN{print} {print , $0}padding.pl#!/usr/bin/perl$pad9999;$nword_per_line50;$nline_per_page52;$num_of_lines0;while(){arraysplit;$nwordarray;while($nword$nword_per_line){push(array,$pad);$nword1;}$strjoin( ,array);print$str,\n;$num_of_lines1;}while($num_of_lines%$nline_per_page){print$str,\n;$num_of_lines1;}dispatch.sh#!/bin/bash#set -xfor((n0;n200;n20));do((sn))((en19))dprintf%03d-%03d\n$s $e[-e$d]rm-rf$dechodelete$dmkdir-p$dfor((is;ie;i));doxprintfx%03d\n$i[-e$x]||exitmv$x$d/.donedonefindfind path prunefind.-path./backup/*-prune-o-path./.git/*-prune-otypef-o-iname*.f90|xargswc-l|sort-n数字列表forfinx00{01..11};doecho$fdoneforfinx00{1..9}{a..b};doecho$fdoneglob通配符匹配命令行通配符单个字符匹配glob-expansion)#!/bin/bashechoMatching Directories:fordirin[p-xP-X]*;doif[-d$dir];thenls-d$dirfidone#合并所有分区的cells文件(剔除注释行和数量行)forfinpart.*/cells;doecho$fsed/^\*\*.*/d$f|tail-n2cellsdone数学计算awkBEGIN{print 0.015/0.022}sortAligning columns using awk#第三列排序(数字逆序)sort-nr-k3,3 timers.txttimers.sort.txt#列格式化column-ttimers.sort.txtwatchwatchtail-n15mylogfile.txttee#stdoutstderr重定向到teexxxx|teerun.loogcygpathcygpathD:\xxxx#只有C系统盘才有short name?cygpath-msC:\Program Files (x86)\Windows Photo Viewercygpath-helpcygwin下strace用strace可以跟踪检查动态库问题(程序会没有任何信息退出);如果strace到是系统库问题可以尝试用老版本替换当前库(有问题的库可能是因为版本太新)convt.sh 剔除OpenFOAM头部注释sed-n/^\/\/.*\/\//,$pfaceZones|tail-n2wget下载所有pdf文件wget-r-A.pdf[url]设置用户sudo权限sudousermod-a-Gsudousername获取脚本所在绝对路径MYDIR$(dirname$(readlink-f$0))OCR Captureforxinxa[a-d].dir;doecho$x;cd$x;/tmp/db_line_compare db_yyy.tx /tmp/$x/db_xxxx.txt21.log;cd..;doneforxinxa[a-d].dir;doecho$x;cd$x;/tmp/get_lines1.log xtotal.padxround;cd..;doneforxinx*.dir;doecho$x;cd$x;catxxx|perl -MDigest::MD5md5_hex -nlEsay md5_hex($_)db_xxxx.txt;cd..;doneAssociative ArraysAssociative Arraysshell Bash的数组与关联数组的实现命令行参数#!/usr/bin/basharg1$1arg2$2arg3$3#把第4个起所有的命令行内容都给optionsshift3options$echooptions$optionsecho$optionsecho$optionsgetoptsbash: getoptsunload所有已加载模块How do I split a string on a delimiter in Bash?if[$LOADEDMODULES];thenIFS:read-raMMM$LOADEDMODULESformin${MMM[]};domodule unload$m;donefiBoomerAMG参数declare-Aamg_opts amg_opts[strong_threshold]0.6amg_opts[max_level]25amg_opts[coarsen_type]PMISamg_opts[agg_nl]1amg_opts[agg_num_paths]1amg_opts[truncfactor]0.3amg_opts[interp_type]extiamg_opts[print_statistics]3amg_opts[print_debug]1AMG_OPTIONSforkin${!amg_opts[]};dov${amg_opts[$k]}#echo $k, $vAMG_OPTIONS$AMG_OPTIONS-eqn_PP_pc_hypre_boomeramg_$k$vdone##export PETSC_OPTIONS-verbose 3 -timing -timing_prof -eqn_PP_ksp_view -eqn_PP_ksp_type gmres $AMG_OPTIONS exportPETSC_OPTIONS-verbose 3 -timing -timing_prof -eqn_PP_ksp_view -eqn_PP_ksp_type gmres 比较两个文件夹内同名数据文件forxinmesh.dir/part.0/*;doybasename$x;diff$xmesh.dir_new_omp/part.0/$y;donetop监控进程资源使用情况mpirun-np64$UNS_SOLVER$CASE_DIRBCGS_2222MPIRUN_PID$!sleep10top-ppgrep-d bcgs|awk{print $1}-d60-n10000-btop.logwait$MPIRUN_PIDtar和zstd并行打包压缩数据#并行打包压缩tar-Izstd -19 -T4-cfdata.tar.zst data/tarcf - data/|zstd-19-T4data.tar.zst#解包tar-Izstd -d-xfdata.tar.zst7z打包压缩7z a-t7z-m0lzma2-mx9-mfb273-md1024m-ms64m-mmton 极限压缩.7z 目录/分割提取文件名功能fullfile/path/to/example.tar.gz# 提取文件名含扩展名filename${fullfile##*/}echo文件名:$filename# 输出: example.tar.gz# 提取最后一个扩展名对于多重扩展名extension${filename##*.}echo最后扩展名:$extension# 输出: gz# 提取主文件名去除最后一个扩展名basename${filename%.*}echo主文件名:$basename# 输出: example.tar# 如果需要去除所有扩展名保留最基础文件名basename_all${filename%%.*}echo基础文件名:$basename_all# 输出: exampleFFMPEG超级压缩视频文件ffmpeg-iinput.mp4-c:vlibx264-crf35-presetveryfast-vfscale480:-2-r15-c:alibmp3lame-b:a64k output_low.mp4du命令按大小排序du-sh*|sort-hrsync 排除目录和文件rsync-av--excludeRESULT*/--exclude*.runaxxx /backup/xxx字符串操作How-to: Redirection and Process SubstitutionHow-to: Redirection and Process SubstitutionWhat’s the difference between , and in bash?网络资源Awesome BashBash-Snippetslinux-shell-command-exit-codeshow-do-i-write-standard-error-to-a-file-while-using-tee-with-a-pipeBash内置变量

更多文章