bash代码片段snippets

bash代码片段


bash 复制代码
# Rename all *.txt to *.text
for file in *.txt; do
    mv -- "$file" "${file%.txt}.text"
done
bash 复制代码
while read x z y; do
  of="xag.1/$x.png" 
  if="xag/$y.png"
  cp $if $of	  
done < db.log 
bash 复制代码
tree -L 2 | sed 's/\xc2\xa0/ /g' > ~/Downloads/1.txt
bash 复制代码
du -sh -t 100M *

移除重复行

bash 复制代码
#整行比较
awk '!seen[$0]++' file
#第一个field比较
awk '!seen[$1]++' file

按指定field排序

bash 复制代码
#按第二个field排序
sort -k2,2 file

按起始10个字符去重复

bash 复制代码
uniq -w 10 file 

od输出二进制文件数据

bash 复制代码
od -x -v -A n --width=100 ./x.bin >od.txt

计算文本文件每行的md5

bash 复制代码
cat od.txt | perl -MDigest::MD5=md5_hex -nlE'say md5_hex($_)' >db_x.txt

文件分割

bash 复制代码
cat ../xtotal.pad | split -a 4 -d -l 52

一份文件复制两次

bash 复制代码
n=0;
for x in x*; do 
  y=`printf "y%04d" $n` && ((n=n+1)) && cp $x $y
  y=`printf "y%04d" $n` && ((n=n+1)) && cp $x $y
done

生成所有文件md5sum

bash 复制代码
md5sum x*

ffmpeg批量提取多个视频中图像

bash 复制代码
 n=1; for x in round.0/*;do echo $x;  fmt=frame${n}.%04d.png; ffmpeg -i $x -vf "fps=2" $fmt; ((n=n+1)); done

printout.sh

bash 复制代码
#!/bin/bash

hexpattern="0-9a-f"
pattern='$LZSKGHVRMTBCDEF'
input=$1

cat $input | sed 's/\s//g' | tr $hexpattern $pattern  | awk  'BEGIN{print} {print " ", $0}'

padding.pl

perl 复制代码
#!/usr/bin/perl

$pad='9999';
$nword_per_line=50;
$nline_per_page=52;

$num_of_lines=0;
while(<>)
{
    @array=split;

    $nword=@array;

    while($nword < $nword_per_line)
    {
        push(@array,$pad);
        $nword+=1;
    }
    $str=join(" ",@array);
    print $str,"\n";

    $num_of_lines+=1;
}

while($num_of_lines%$nline_per_page)
{
    print $str, "\n";
    $num_of_lines+=1;
}

dispatch.sh

bash 复制代码
#!/bin/bash

#set -x

for((n=0;n<200;n+=20)); do
        ((s=n))
        ((e=n+19))
        d=`printf '%03d-%03d\n' $s $e`
        [ -e $d ] && rm -rf $d && echo "delete $d"
        mkdir -p $d
        for ((i=s;i<=e;i++));do
                x=`printf 'x%03d\n' $i`
                [ -e $x ] || exit
                mv $x $d/.
        done
done

find path prune

bash 复制代码
find . -path './backup/*' -prune -o -path './.git/*' -prune -o type f -o -iname '*.f90' | xargs wc -l | sort -n 

数字列表

bash 复制代码
for f in x00{01..11};do 
  echo $f 
done 

for f in x00{1..9}{a..b};do 
  echo $f
done 

单个字符匹配(glob-expansion)

bash 复制代码
#!/bin/bash

echo "Matching Directories:"

for dir in [p-xP-X]*; do
    if [ -d "$dir" ]; then
        ls -d "$dir"
    fi
done

watch

bash 复制代码
watch tail -n 15 mylogfile.txt

tee

bash 复制代码
#stdout&stderr重定向到tee 
xxxx |& tee run.loog 

cygpath

bash 复制代码
cygpath "D:\xxxx"
网络资源

Awesome Bash

相关推荐
普通人61 分钟前
PHP文件读写完全指南
开发语言·php·1024程序员节
坤坤子吖1 小时前
Linux网络编程:TCP服务器的多进程与多线程实现
linux·服务器·开发语言·网络·c++·笔记·学习
一晌小贪欢1 小时前
Python基础:第 09 篇 · while 循环:重复直到不满足
开发语言·c++·python·数据可视化·面向对象·python办公
xiaoye-duck2 小时前
《C++ 项目:手写大模型接入 SDK》LLMManager架构设计与实现
开发语言·网络·c++·语言模型
一条大祥脚2 小时前
CuTe 与 CuTe DSL:从 C++ 模板元编程到 Python DSL 的 GPU 编程演进
开发语言·c++·python·cutlass·cute·cute dsl
FRing033 小时前
vue2干货总结8(路由模块封装、router-link、声明式导航、路由重定向、编程式导航)
开发语言·前端·vue.js·vue2
旖旎夜光3 小时前
【C++ 基础入门】C++ 多态(上):虚函数、重写、override/final 与抽象类
开发语言·c++·笔记·学习
starzy19903 小时前
深入理解Scala嵌套函数:作用域封装之道
开发语言·scala
Sakura_j_h_4 小时前
从零写一个 MCP stdio Server:用 Python 校验博客文章格式
开发语言·python·自动化·ai工具·mcp·json-rpc
白杨尚青5 小时前
C++进阶篇(五):红黑树——四条规则、三种插入情况与旋转变色
开发语言·数据结构·c++·stl·红黑树·平衡二叉树