以下为本人工作常用的一些bash指令总结,希望能够有帮助,直接ctrl+F关键词查询需要的指令,不定时更新~~
1. 循环读取文本中内容:
while read p;do
echo "ha ---$p"
done<text
2. 指定后缀的文件名写入文本
ls -R /mnt/data2/*.jpg > file.txt //绝对路劲
ls -R *.jpg > file.txt //相对路劲
3. pkill command,通过command杀掉所有的命令
ps -ef | grep 'python xxx.py'| awk '{print $2}' #打印python xxx.py的pid
ps -ef | grep 'python xxx.py'| awk '{print $2}' | xargs kill -9 #杀掉所有打印出来的pid
kill htop 'python' #杀掉所有的python命令
4. 对文件进行排序
ls -Sl #文件大小从大到小排列
ls -Slr #从小到大排序
ls -Sl -h #以M或者k为单位
5. 删除文件种指定的某些行
sed -i "1,100d" ttt #删除ttt文件的1-100行
6.分割文本
split -l 1000 large_file.txt stxt # 按照行分割,并分割后的文本以stxt开头 10g大概 410,630,810行
split -b 100M data.bak sdata #按照大小分割,并分割后的文本以sdata开头
7压缩解压文件
nohup tar -zcvf counts_lack_46.tar.gz 2019_02_27_700g_counts/ > 1 &
tar -xvf filenaem
8. 后台执行进程与转至前台
ctrl z #暂停指令
bg # 指令后台运行
fg #后台转前台grep
9. 在当前文件夹以及其子文件夹下寻找某个文件
find . -name A #在当前文件夹及该文件夹下寻找文件A。
10. 查看一级目录下所有文件的大小
du -h --max-depth=1 ##最大层级为1的文件的大小
11. vim 批量替换文本
%s#abc#def#g #(用def替换文件中所有的abc)
sed -i "s/token1/token2/g" 11.txt #不打开文件夹,外部出转换,将token1转为token2。
#参考: https://www.cnblogs.com/nkwy2012/p/6365714.html
12. bash函数时间
function getTiming() {
start=$1
end=$2
start_s=$(echo $start | cut -d '.' -f 1)
start_ns=$(echo $start | cut -d '.' -f 2)
end_s=$(echo $end | cut -d '.' -f 1)
end_ns=$(echo $end | cut -d '.' -f 2)
time=$(( ( 10#$end_s - 10#$start_s ) * 1000 + ( 10#$end_ns / 1000000 - 10#$start_ns / 1000000 ) ))
echo "$time ms"
}
start_time=$(date +%s.%N)
##-----------------
# 执行命令
##-----------------
end_time=$(date +%s.%N)
runtime=$(getTiming $start_time $end_time)
echo "counts runtime: "$runtime>>$dict_file/log
13. 多进程(包括了循环读取文件)
tmp_fifofile="/tmp/$$.fifo"
mkfifo $tmp_fifofile
exec 6<>$tmp_fifofile
rm -rf $tmp_fifofile
thread=10 #此处控制开几个进程
for((i=0;i<$thread;i++))
do
echo ""
done >&6
while read p;do
start_one_time=$(date +%s.%N)
read -u6
{
./generate_dict $src_file$p $split_file$p.seg $dict_file$p.dict
echo "">&6
}&
done</data4/lmtools/temp_file/filelist
wait
exec 6>&-
exit 0
14. 查看一个字符串在某个文档中出现的次数
grep -o '停止' tielu.norm.seg | wc -l
15. 查看glib二进制文件中不可见的版本支持信息
strings /usr/lib64/libc.so.6 | grep GLIBC
16.切换gcc版本:
scl enable devtoolset-5
17.linux文件个数
ls -l | grep "^-"|wc -l
ls | wc -l
18.查看当前运行的进程
jobs -l
19.内存查看指令
free -m -s2 #每两秒显示一次
watch free -g
20.查看占用内存最多的进程
ps -aux | sort -k4nr | head -n 10 #前10个进程占用最多的
21. 查看cpu信息
cat /proc/cpuinfo
22. 在首行加入内容
sed -i '1s/^/hah\n/' 1.txt # 1s/^/内容\n/
23.遍历指定目录下得所有文件
#!/bin/bash
function getdir(){
for element in `ls $1`
do
dir_or_file=$1"/"$element
if [ -d $dir_or_file ]
then
getdir $dir_or_file
else
echo $dir_or_file
fi
done
}
root_dir="$1"
getdir $root_dir
24. 字符串切片
参考: https://www.cnblogs.com/haona_li/p/10334057.html
echo ${p##*/} 对于变量p中得字符,贪婪匹配,从开头到 最后一次出现"/" 得位置将被删除:
例如: /data4/text_train_dict20181026/9f.all.txt.ext.norm.t2s.norm.seg ---> all.txt.290G.norm.t2s.norm.seg
25. 目录生成
mkdir -p 确保目录生成,如果不存在,则新建
26. 可以删除空行
awk 'NF > 0' data #对于data文件,选区行中字符数>0的行数
27. 在shell中整数比较和字符串的比较
#https://www.cnblogs.com/happyhotty/articles/2065412.html
## 数值比较
-eq # 等于
-ne #不等于
-gt #大于
## 字符串比较
== #等于
!= #不等于
> # 大于,ascii大小的比较
< # 小于,ascii大小的比较
28. 判断条件与
if [ $num -eq 1 ] && [ $xx -eq 3 ];then
echo "jjjjjjjj"
fi
29. 查看中间的某几行;https://www.cnblogs.com/CoolJayson/p/7512814.html
cat filename | tail -n +3000 | head -n 1000
cat 100test | head -n 40 |tail -n +20 > 20_40
30. 打乱顺序
shuf char_10000000 -o char_10000000_shuf
31.统计文件个数 https://www.cnblogs.com/azhqiang/p/7871843.html
ls -l |grep "^-"|wc -l
32. 组合条件 https://www.jianshu.com/p/4ec50fdaf388
#在dianli_500000中包含东大,但不包含东大二路的个数
grep "东大" dianli_500000 |grep -v "东大二路"|wc -l
#在dianli_500000中包含东大,但不包含东大二路和东大路的个数
egrep "东大" dianli_500000 |grep -v "东大二路"|grep -v "东大路"|wc -l
33. 去除包含某个字符串的行
sed -e "/abc/d" a.txt > b
34. 指令查询进程信息
ps -aux | grep conv_hclg # 会有两个结果
35. 批量转音频文件
for f in `ls wav_precision`; do sox wav_precision/$f wav_precision_use/$f; done
36. 字符串是否相等...
vira="mm"
if [ "$vira"x == "mm"x ]; then
echo hclghclghclg----------- $HCLG_path
fi
if [ "$vira"x == "m"x ]; then
echo hclghclghclg----------- $HCLG_path
fi
37. n天里新增的文件
find /mnt/data3/wuchunsheng/ -ctime -9 -ls
38. 解压压缩文件中的某个文件
unzip -l abc.jar
unzip -j abc.jar data/2.txt -d /tmp/data_in_abc
39. 去除<feff>
sed -i 's/\xEF\xBB\xBF//g' filename
40. 去掉win下文件到linux下是出现的^M【有时候不奏效】
www.tool-box.cn/post-94.html
“^”:用ctrl + v 输入
"M":用ctrl + M 输入
这样用:%s/^M//g就可以去掉^M了
41. 查看进程内存
cat /proc/$pid/statm
42.查看gcc位置
which gcc
which g++
/usr/local/bin/g++ -v
43. 去除掉一个文件中,包含某个字符的行。
sed -e '/xxx/d' a.txt > y
44. 在一个文件中的第一行插入内容
sed -i "1ientity_zh,entity_id" processed_entity_test_1_17
45. 查询端口占用问题
lsof -i:5000 #查看某个端口的进程
ll /proc/7404 ##查看这个进程的代码,运行自哪里
lrwxrwxrwx. 1 wuchunsheng wuchunsheng 0 1月 19 12:05 cwd -> /mnt/data3/wuchunsheng/code/nlper/ES