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 |
#!/bin/bash # 脚本作用,封装grep指令,不输出冒号,减少复制打开的障碍,同时最后 # 只输出找到的文件的集合(不重复) black='\033[0;30m' red='\033[0;31m' green='\033[0;32m' brown='\033[0;33m' blue='\033[0;34m' purple='\033[0;35m' cyan='\033[0;36m' gray='\033[0;37m' darkGray='\033[1;30m' lightRed='\033[1;31m' lightGreen='\033[1;32m' yellow='\033[1;33m' lightBlue='\033[1;34m' lightPurple='\033[1;35m' lightCyan='\033[1;36m' white='\033[1;37m' noColor='\033[0m' numOfParameters=$# parameters=$@ checkInput() { if [ $1 -lt 1 ]; then echo echo -e ${brown}'No enough parameters'${noColor} echo exit fi } listFileWhichContainStr() { str=$1 restStr="${@:2}" echo -e ${yellow}'Detail: '${noColor} # -Il to ignore binary file find -type f ! -iname tags -exec grep -Il "$str" $restStr --color=always {} + | awk -F: '{print $1, $2}' | uniq echo echo echo -e ${yellow}'unique file list: |
bash
echo with color
Shell脚本里加入如下定义:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
black='\033[0;30m' red='\033[0;31m' green='\033[0;32m' brown='\033[0;33m' blue='\033[0;34m' purple='\033[0;35m' cyan='\033[0;36m' gray='\033[0;37m' darkGray='\033[1;30m' lightRed='\033[1;31m' lightGreen='\033[1;32m' yellow='\033[1;33m' lightBlue='\033[1;34m' lightPurple='\033[1;35m' lightCyan='\033[1;36m' white='\033[1;37m' noColor='\033[0m' |
1 2 3 4 |
# 使用时在字符串前连接上需要的颜色,结尾处取消颜色设置,注意echo加上 -e 参数 echo -e {red}"str"${noColor} |
…
Continue reading...Shell 按比例缩放图片大小
一般屏幕大小是1920*1080,以下是将目录下的所有.jpg图片转换为该大小,注意是按比例缩放,并不是最终大小一定是1920*1080
1 2 3 |
for f in `ls *.jpg`;do convert -resize '1920x1080' $f $f;done |
…Continue reading...
Bash 学习笔记
1. 字符串分割
${str:startIndex:length}
2.字符索引
expr index ‘c’ $str
3.执行脚本参数
$1 – $9 – The first 9 arguments to the Bash script. (As mentioned above.)
4.参数数量
$# – How many arguments were passed to the Bash script.
5.所有传参名
$@ – All the arguments supplied to the Bash script.
6.字符串长度
${#string} — 获取字符串长度
7.…
Continue reading...find和其他命令搭配使用
1.find和mv搭配
1 2 3 |
find . -regex './[0-9]+\.[jpg|png|jpeg]+$' -exec mv -t num {} + |
搜索当前文件夹下的照片并移至文件夹num/下,+号是指plus version of find, 带上这个符号才可使用-exec,{}是find结果自动填充的地方,-t是mv的参数,表示target directory.
2.find和file搭配
1 2 3 |
find . -type f -size +10k -exec file {} + |
找出当前文件夹下所有大于10k的文件并显示其文件类型等相关信息,如果是比指定大小要小,用符号减,即-10k
3.find和多种命令搭配,删除当前文件夹下同名编译出的c文件
1 2 3 |
find . -regex "./.*.c" | awk -F/ '{print $2}'|awk -F. '{print $1}'|xargs rm > /dev/null 2>&1 |
4.find中的正则匹配不能直接匹配回车符,会被视为n
解决办法,采用如下输入法:
1 2 3 4 |
find . -type f -regex "./[a-z]/.* 130 ↵ .*" |
手动给它真正的回车,这个命令匹配的是文件夹./a ./b ~ ./z 下中间带有回车符的文件名。
5.find查找空文件
1 2 3 |
find . -type f -empty |
…
Continue reading...Bash截取字符串批量重命名文件
文件状态:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
╰─$ ls 【爱启航】词汇讲义01-2020晓艳CARRY班.pdf 【爱启航】词汇讲义02-2020晓艳CARRY班.pdf 【爱启航】词汇讲义03-2020晓艳CARRY班(和第2讲有重合).pdf 【爱启航】词汇讲义04-2020晓艳CARRY班.pdf 【爱启航】词汇讲义05-2020晓艳CARRY班.pdf 【爱启航】词汇讲义06-2020晓艳CARRY班.pdf 【爱启航】词汇讲义07-2020晓艳CARRY班.pdf 【爱启航】词汇讲义08-2020晓艳CARRY班.pdf 【爱启航】词汇讲义09-2020晓艳CARRY班.pdf 【爱启航】词汇讲义10-2020晓艳CARRY班.pdf 【爱启航】词汇讲义11-2020晓艳CARRY班.pdf 【爱启航】词汇讲义12-2020晓艳CARRY班.pdf 【爱启航】词汇讲义13、14-2020晓艳CARRY班.pdf 【爱启航】词汇讲义15-2020晓艳CARRY班.pdf 【爱启航】词汇讲义16-2020晓艳CARRY班.pdf 【爱启航】词汇讲义17-2020晓艳CARRY班.pdf 【爱启航】词汇讲义18-2020晓艳CARRY班.pdf 【爱启航】词汇讲义19-2020晓艳CARRY班.pdf |
以下为bash脚本内容, 将其写入文件newname.sh.
1 2 3 4 5 6 7 8 9 10 11 |
#!/bin/bash for f in `ls *.pdf` do prefix="${f:5:6}"; #从第五个字符开始,截取长度为6 suffix=".pdf"; newName=$prefix$suffix; mv $f $newName; done |
终端执行下列命令赋予执行权限,再sh运行脚本:
1 2 3 4 |
chmod +x newname.sh sh newname.sh |
批量重命名成功:
1 2 3 4 5 6 7 |
╰─$ ls newname.sh 词汇讲义04.pdf 词汇讲义08.pdf 词汇讲义12.pdf 词汇讲义17.pdf 词汇讲义01.pdf 词汇讲义05.pdf 词汇讲义09.pdf 词汇讲义13.pdf 词汇讲义18.pdf 词汇讲义02.pdf 词汇讲义06.pdf 词汇讲义10.pdf 词汇讲义15.pdf 词汇讲义19.pdf 词汇讲义03.pdf 词汇讲义07.pdf 词汇讲义11.pdf 词汇讲义16.pdf |
…
Continue reading...