LingYanSi / blog

博客
https://github.com/LingYanSi/blog/issues
9 stars 0 forks source link

shell/bash相关 #127

Open LingYanSi opened 3 years ago

LingYanSi commented 3 years ago

获取某个文件夹下某种类型文件的大小

find src  | grep -E "\.(js|png|jepg|jpg|css|less)$" | xargs du -ch | grep "total"

获取某种类型文件的个数

find src  | grep -E "\.(js|png|jepg|jpg|css|less)$" | wc
LingYanSi commented 3 years ago

按照文件大小对文件进行排序,-sk表示显示文件的kb,sort -n 表示按照文件名排序

 find src  | grep -E "\.(js)$" | xargs du -sk | sort -n

倒序输出

find src | grep -E "\.(js)$" | xargs du -sk | sort -n | tail
LingYanSi commented 3 years ago

获取cpu信息 cat /proc/cpuinfo

LingYanSi commented 3 years ago

使用 sort -V 对版本号进行排序

之前

image

之后

image

参考资料https://www.gnu.org/software/coreutils/manual/html_node/Version-sort-is-not-the-same-as-numeric-sort.html#:~:text=Numeric%20sort%20('%20sort%20%2Dn,8.5%20%2C%20and%20appears%20before%20first.

image

LingYanSi commented 2 years ago

grep

获取指定文件件下的所有域名地址

 grep -RhoE "https?://[^/?'\"$]+" src | sort | uniq
LingYanSi commented 2 years ago

包工头查看指定分支、指定时间后,commit提交人数排行,不能代表一切但有一定的价值

git log --format='%an' --after="2021-01-01" | sort | uniq -c | sort
LingYanSi commented 2 years ago

mac命令复制到剪切板 pbcopy,与其对应的是 pbpaste

cat search.log | cat | sort -V | pbcopy
LingYanSi commented 2 years ago

sed

https://stackoverflow.com/questions/30003570/how-to-use-gnu-sed-on-mac-os-10-10-brew-install-default-names-no-longer-su

在mac中请不要使用系统自带的sed命令,转而使用gun-sed,安装命令为 brew install gnu-sed,然后调用gsed

系统自带的sed在使用 sed -i 的时候会出现不符合预期的问题(与tldr sed上的文档不一致)


  - Replace the first occurrence of a string in a file, and print the result:
    sed 's/find/replace/' filename

  - Replace all occurrences of an extended regular expression in a file:
    sed -E 's/regex/replace/g' filename

  - Replace all occurrences of a string in a file, overwriting the file (i.e. in-place):
    sed --in-place='' 's/find/replace/g' filename

  - Replace only on lines matching the line pattern:
    sed '/line_pattern/s/find/replace/' filename

  - Print only text between n-th line till the next empty line:
    sed -n 'line_number,/^$/p' filename

  - Apply multiple find-replace expressions to a file:
    sed -e 's/find/replace/' -e 's/find/replace/' filename

  - Replace separator / by any other character not used in the find or replace patterns, e.g., #:
    sed 's#find#replace#' filename
LingYanSi commented 2 years ago

在nodejs文件中调用字符串shell命令的问题

背景:想替换一个文件内的 \/\/ 字符串,发现踩进一个坑里面了。

const url1 = 'https:\/\/www.exx.com'
execSync(`find tmp | grep -E '\\.(js|css|html|php)' | xargs gsed -i 's#${url1}#xxxx#' `, { stdio: 'inherit' })

按照上面的写法,无论如何也无法替换成功,很着迷。仔细思考了一下,其实还是转译字符的问题

需要注意转译字符

首先在js字符串中 \ 需要表示为 \\
在bash中同理 \ 需要表示为 \\
那么在js字符串中的bash命令的\ 需要写成 \\\\ 四倍的快乐,谁用谁知道

如果直接调用一个shell脚本就没有这么多事情了

const url = 'https://www.exx.com'
execSync(`find tmp | grep -E '\\.(js|css|html|php)' | xargs gsed -i 's#${url}#xxxx#g' `, { stdio: 'inherit' })

const url1 = 'https:\\\\/\\\\/www.exx.com'
execSync(`find tmp | grep -E '\\.(js|css|html|php)' | xargs gsed -i 's#${url1}#xxxx#' `, { stdio: 'inherit' })

ps: 对于sed,见上面的文档。其不是按照/来进行分割的,而是根据s后面的一个字符作为分割字符,一般书写为s/old/new/形式,但其实也可以写成s#olf#new# 或者 s$old$new$

LingYanSi commented 2 years ago

cp文件夹时,忽略指定的子目录或文件

cp是无法满足需求的,可以使用rsync代替。rsync不仅可以用在不同服务间同步文件,也可以本机上同步文件

rsync -av icp/* icp-xx/ --exclude .git
LingYanSi commented 2 years ago

引号转译问题

 find src | grep -Ei "/view.js$" | xargs grep -Eoh '^allPage\[[^]]+' | awk -F "[\"']" '{print $2}' | sort | uniq -n

对单引号添加\是不行的 '["\']',必须换个姿势对"添加\

LingYanSi commented 2 years ago

awk

LingYanSi commented 2 years ago

define a list, you can use it as $@

list=(src app-*)

单行输出文本信息,需要注意的是的需要在结尾处输出

echo -ne 'something \r';
sleep 1
echo -ne 'something1 \r';
sleep 1
echo 'something done';

使用dirname方法可以获取指定文件路径的父级文件夹路径

BASEDIR=$(dirname "$0")
echo "$BASEDIR"