1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179
| #!/bin/bash current_time = $(date "+%Y-%m-%d %H:%M:%S")
function replace_str_in_file(){ sed -i "s/$1/$2/g" $3 }
replace_str_in_file 'result.displayType= "YourName"' 'result.displayType="${DISPLAY_NAME}"' applocalInfo.lua
wget 下载整站 wget -r -p -np -k https://raw.githubusercontent.com/
通过ssh备份 tar zcvf - back/ | ssh root@www.jb51.net tar xzf - -C /root/back/
kill pstree -ap 10277 |grep -oP '[0-9]{4,6}'|xargs kill -9
导出最后一列非空数据 awk -f "|" '{if($NF!="")print $NF}'
行前或行后插入 sed 'p;s/^.*$/----/' file awk '{print $0;print "----"}' file
sed 's/^/new/g' file sed 's/$/new/g' file
ssh-keygen -y -f ~/.ssh/id_rsa && cat ~/.ssh/id_rsa.pub | ssh root@host "cat - >> ~/.ssh/authorized_keys"
简单列出当前目录文件 python -m SimpleHTTPServer python3 -m http.server
查看内存使用情况 ps aux --sort=rss |sort -k 6 -rn
查看ip ifconfig |awk -F"[ ]+|[:]" 'NR==2 {print $4}'
查找并杀进程 pgrep nginx|xargs kill pidof nginx|xargs kill
删除注释 sed -i 's/#.*$//g' filename
查看代码,除掉注释和空行 egrep -v "^#|^$" filename sed '/#.*$/d; /^ *$/d'
按大小列出文件 ls|xargs du -h|sort -rn
在somefile.sh 文件里加上set+x set-x 1. 用 && || 简化if else 判断是否为空文件 if [[ -s $file ]]; then
echo "not empty"
fi 获取文件大小 stat -c %s $file
stat --printf='%s\n' $file
wc -c $file
rsync 备份 rsync -r -t -v /source_folder /destination_folder
rsync -r -t -v /source_folder [user@host:/destination_folder
为所有txt文件加上.bak rename '.txt' '.txt.bak' *.txt 去掉所有的bak rename '*.bak' '' *.bak
空格替换成下划线 find path -type f -exec rename 's/ /_/g' {} \; 文件名改成大写 find path -type f -exec rename 'y/a-z/A-Z/' {} \; 文件名改成小写 find path -type f -exec rename 'y/A-Z/a-z/' {} \;
for ((i=0; i < 10; i++)); do echo $i; done
for line in $(cat a.txt); do echo $line; done
for f in *.txt; do echo $f; done
while read line ; do echo $line; done < a.txt
cat a.txt | while read line; do echo $line; done
删除空行
cat a.txt | sed -e '/^$/d'
(echo "abc"; echo ""; echo "ddd";) | awk '{if (0 != NF) print $0;}'
字典结构 hput() {
eval "hkey_$1"="$2"
}
hget() {
eval echo '${'"hkey_$1"'}'
}
$ hput k1 aaa
$ hget k1
aaa 遍历数组 array=( one two three )
for i in ${array[@]}
do
echo $i
done 获取路径和文件名
$ dirname ‘/home/lalor/a.txt'
/home/lalor
$ basename ‘/home/lalor/a.txt'
a.txt
获取文件名和扩展 var=hack.fun.book.txt
echo ${var%.*}
hack.fun.book
echo ${var%%.*}
hack
echo ${var#.*}
fun.book.txt
echo ${var##.*}
txt
清理僵尸进程 ps -eal | awk '{ if ($2 == "Z"){ print $4}}' | kill -9
创建函数库
将函数定一个在另一个文件,然后通过source 命令加载到当前文件
在命令行使用函数
将函数定义在~/.bashrc 中即可
|