From 3dfb4f90a3f474c3938b5400dba76350b93640a3 Mon Sep 17 00:00:00 2001 From: jaywcjlove <398188662@qq.com> Date: Fri, 30 Sep 2022 00:26:09 +0800 Subject: [PATCH] feat: add `find.md` & `sed.md` cheatsheet. --- README.md | 2 + docs/find.md | 392 +++++++++++++++++++++++++++++++++++++++++++++ docs/sed.md | 344 +++++++++++++++++++++++++++++++++++++++ scripts/create.mjs | 4 +- 4 files changed, 740 insertions(+), 2 deletions(-) create mode 100644 docs/find.md create mode 100644 docs/sed.md diff --git a/README.md b/README.md index c19b765..7916acd 100644 --- a/README.md +++ b/README.md @@ -29,6 +29,8 @@ Quick Reference [Cron](./docs/cron.md)<!--rehype:style=background: rgb(239 68 68/var(\-\-bg\-opacity));--> [Git](./docs/git.md)<!--rehype:style=background: rgb(215 89 62/var(\-\-bg\-opacity));--> +[find](./docs/find.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));--> +[Sed](./docs/sed.md)<!--rehype:style=background: rgb(16 185 129/var(\-\-bg\-opacity));--> <!--rehype:class=home-card--> ## 其它 diff --git a/docs/find.md b/docs/find.md new file mode 100644 index 0000000..7c2a452 --- /dev/null +++ b/docs/find.md @@ -0,0 +1,392 @@ +Find 备忘清单 +=== + +这是 Linux find 命令备忘单的快速参考列表,包含常用选项和示例。 + +入门 +---- + +### 用法 + +```shell +$ find [path...] [options] [expression] +``` + +通配符 + +```shell +$ find . -name "*.txt" +$ find . -name "2020*.csv" +$ find . -name "json_*" +``` + +---- + +- [Find 备忘清单](https://gist.github.com/gr1ev0us/3a9b9d9dbdd38f6379288eb2686fc538) _(gist.github.com)_ + +### 参数示例 +<!--rehype:wrap-class=col-span-2--> + +| 参数 | 示例 | 描述 | +|-------|-------|-------| +| `-type` | find . -type d | 仅查找目录 | +| `-name` | find . -type f -name "*.txt" | 按名称查找文件 | +| `-iname` | find . -type f -iname "hello" | 按名称查找文件(不区分大小写) | +| `-size` | find . -size +1G | 查找大于 1G 的文件 | +| `-user` | find . -type d -user jack | 查找杰克的文件 | +| `-regex` | find /var -regex '.\*/tmp/.\*[0-9]*.file' | 将正则表达式与查找一起使用 | +| `-maxdepth` | find . -maxdepth 1 -name "a.txt" | 在当前目录和子目录中 | +| `-mindepth` | find / -mindepth 3 -maxdepth 5 -name pass | 在子目录级别 2 和 4 之间 | +<!--rehype:className=show-header--> + + +### 类型 + +| | | +|-----------|----------------------| +| `-type d` | 目录 | +| `-type f` | 文件 | +| `-type l` | 符号链接 | +| `-type b` | 缓冲块 | +| `-type c` | 无缓冲字符 | +| `-type p` | 命名管道 | +| `-type s` | 插座 | + +### 大小 + +| | | +|-----------|---------------------------| +| `-size b` | 512 字节块(默认) | +| `-size c` | 字节 | +| `-size k` | 千字节 | +| `-size M` | 兆字节 | +| `-size G` | 千兆字节 | +| `-size T` | 太字节_(仅限 BSD)_ | +| `-size P` | PB _(仅 BSD)_ | + +### 大小 +/- + +查找所有大于 10MB 的文件 + +```shell +$ find / -size +10M +``` + +查找所有小于 10MB 的文件 + +```shell +$ find / -size -10M +```` + +查找所有正好为 10M 的文件 + +```shell +$ find / -size 10M +``` + +查找 100MB 和 1GB 之间的大小 + +```shell +$ find / -size +100M -size -1G +``` + +像往常一样,`+` 和 `-` 前缀表示大于和小于。 + +### 名称 + +在当前目录中使用名称查找文件 + +```shell +$ find . -name tecmint.txt +``` + +查找主目录下的文件 + +```shell +$ find /home -name tecmint.txt +``` + +使用名称查找文件并忽略大小写 + +```shell +$ find /home -iname tecmint.txt +``` + +使用名称查找目录 + +```shell +$ find / -type d -name tecmint +``` + +使用名称查找php文件 + +```shell +$ find . -type f -name tecmint.php +``` + +查找目录下的所有php文件 + +```shell +$ find . -type f -name "*.php" +``` + +### 权限 + +查找权限为 777 的文件。 + +```shell +$ find . -type f -perm 0777 -print +``` + +查找未经许可的文件 777. + +```shell +$ find / -type f ! -perm 777 +``` + +查找 SUID 集文件。 + +```shell +$ find / -perm /u=s +``` + +查找 SGID 集文件。 + +```shell +$ find / -perm /g=s +``` + +查找只读文件。 + +```shell +$ find / -perm /u=r +``` + +查找可执行文件。 + +```shell +$ find / -perm /a=x +``` + +### 所有者和组 + +根据用户查找单个文件 + +```shell +$ find / -user root -name tecmint.txt +``` + +根据用户查找所有文件 + +```shell +$ find /home -user tecmint +``` + +根据组查找所有文件 + +```shell +$ find /home -group developer +``` + +查找用户的特定文件 + +```shell +$ find /home -user tecmint -iname "*.txt" +``` + +### 多个文件名 + +```shell +$ find . -type f \( -name "*.sh" -o -name "*.txt" \) +``` +<!--rehype:className=wrap-text --> + +查找带有 `.sh` 和 `.txt` 扩展名的文件 + +### 多个目录 + +```shell +$ find /opt /usr /var -name foo.scala -type f +``` +<!--rehype:className=wrap-text --> + +查找具有多个目录的文件 + +### 空的 + +```shell +$ find . -type d -empty +``` + +删除目录中的所有空文件 + +```shell +$ find . -type f -empty -delete +``` + + +查找日期和时间 +------------- + +### 方法 +<!--rehype:wrap-class=col-span-2--> + +| Option | Description | +|---------|-----------------------------------------------------------------| +| `atime` | 访问时间(上次文件<yel>打开</yel>) | +| `mtime` | 修改时间(上次文件<yel>内容被修改</yel>) | +| `ctime` | 更改时间(上次文件 <yel>inode 已更改</yel>) | + +#### 示例 + +| Option | Description | +|-----------------|------------------------------------------------------------| +| `-mtime +0` | 24 小时前修改 | +| `-mtime 0` | 从现在到 1 天前修改 | +| `-mtime -1` | 不到 1 天前修改(与 `-mtime 0` 相同) | +| `-mtime 1` | 24 至 48 小时前修改 | +| `-mtime +1` | 超过 48 小时前修改 | +| `-mtime +1w` | 上次修改时间超过 1 周前 | +| `-atime 0` | 从现在到 24 小时前最后一次访问 | +| `-atime +0` | 访问时间超过 24 小时 | +| `-atime 1` | 在 24 至 48 小时前访问 | +| `-atime +1` | 访问时间超过 48 小时 | +| `-atime -1` | 不到 24 小时前访问过(与 `-atime 0` 相同) | +| `-ctime -6h30m` | 文件状态在过去 6 小时 30 分钟内发生变化 | + +### 更多示例 + +查找最近 50 天修改的文件 + +```shell +$ find / -mtime 50 +``` + +查找最近 50 天访问的文件 + +```shell +$ find / -atime 50 +``` + +查找最近 50-100 天修改的文件 + +```shell +$ find / -mtime +50 –mtime -100 +``` + +查找最近 1 小时内更改的文件 + +```shell +$ find / -cmin -60 +``` + +查找最近 1 小时内修改过的文件 + +```shell +$ find / -mmin -60 +``` + +查找最近 1 小时内访问过的文件 + +```shell +$ find / -amin -60 +``` + +查找并 +-------- +<!--rehype:body-class=cols-2--> + +### 查找和删除 +<!--rehype:wrap-class=row-span-3--> + +查找并删除多个文件 + +```shell +$ find . -type f -name "*.mp3" -exec rm -f {} \; +``` + +查找和删除单个文件 + +```shell +$ find . -type f -name "tecmint.txt" -exec rm -f {} \; +``` + +查找和删除 100mb 文件 + +```shell +$ find / -type f -size +100m -exec rm -f {} \; +``` + +查找特定文件并删除 + +```shell +$ find / -type f -name *.mp3 -size +10m -exec rm {} \; +``` + +### 查找和替换 + +```shell +$ find ./ -type f -exec sed -i 's/find/replace/g' {} \; +$ find ./ -type f -readable -writable -exec sed -i "s/old/new/g" {} \; +``` + +参见:[sed](./sed.md) 命令 + +### 查找和重命名 + +```shell +$ find . -type f -name 'file*' -exec mv {} {}_renamed \; +$ find . -type f -name 'file*' -exec sh -c 'x="{}"; mv "$x" "${x}.bak"' \; +``` + +### 查找和移动 + +```shell +$ find . -name '*.mp3' -exec mv {} /tmp/music \; +``` + +查找并将其移动到特定目录 + +### 查找和复制 + +```shell +$ find . -name '*2020*.xml' -exec cp -r "{}" /tmp/backup \; +``` + +查找并将其复制到特定目录 + +### 查找并连接 + +```shell +$ find download -type f -iname '*.csv' | xargs cat > merged.csv +$ find download -type f -name '*.gz' -exec cat {} \; > output +``` + +### 查找和排序 + +```shell +$ find . -printf "%T+\t%p\n" | sort +$ find . -printf "%T+\t%p\n" | sort -r +``` + +### 查找和 chmod +<!--rehype:wrap-class=row-span-2--> + +查找文件并将权限设置为 644。 + +```shell +$ find / -type f -perm 0777 -print -exec chmod 644 {} \; +``` + +查找目录并将权限设置为 755。 + +```shell +$ find / -type d -perm 777 -print -exec chmod 755 {} \; +``` + +### 查找并 tar + +```shell +$ find . -type f -name "*.java" | xargs tar cvf myfile.tar +$ find . -type f -name "*.java" | xargs tar rvf myfile.tar +``` diff --git a/docs/sed.md b/docs/sed.md new file mode 100644 index 0000000..7a1df8c --- /dev/null +++ b/docs/sed.md @@ -0,0 +1,344 @@ +Sed 备忘清单 +==== + +入门 +---- + +### Sed 用法 + +语法 + +```shell +$ sed [options] command [input-file] +``` + +带管道 + +```shell +$ cat report.txt | sed 's/Nick/John/g' +``` + +```shell +$ echo '123abc' | sed 's/[0-9]+//g' +``` + +### 选项示例 +<!--rehype:wrap-class=col-span-2--> + +| 参数 | 示例 | 描述 | +|-------|-------|-------| +| `-i` | sed -ibak 's/On/Off/' php.ini | 直接备份和修改输入文件 | +| `-E` | sed -E 's/[0-9]+//g' input-file | 使用扩展正则表达式 | +| `-n` | sed -n '3 p' config.conf | 禁止默认图案空间打印 | +| `-f` | sed -f script.sed config.conf | 执行 sed 脚本文件 | +| `-e` | sed -e 'command1' -e 'command2' input-file | 执行多个 sed 命令 | +<!--rehype:className=show-header--> + +### 多个命令 + +```shell +$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g' +Hello World +``` + +使用 `-e` 执行多个 sed 命令 + +### Sed 脚本 + +```shell +$ echo 's/h/H/g' >> hello.sed +$ echo 's/w/W/g' >> hello.sed +$ echo "hello world" | sed -f hello.sed +Hello World +``` + +使用 `-f` 执行 sed 脚本文件 + +### Examples + +```shell +$ sed 's/old/new/g' file.txt +$ sed 's/old/new/g' file.txt > new.txt +$ sed 's/old/new/g' -i file.txt +$ sed 's/old/new/g' -i.backup file.txt +``` + +Sed 命令 +----------- + +### 命令 +<!--rehype:wrap-class=col-span-2--> + +| Command | Example | Description | +|---------|----------------------------------------|-----------------------------| +| `p` | sed -n '1,4 p' input.txt | Print lines 1-4 | +| `p` | sed -n -e '1,4 p' -e '6,7 p' input.txt | Print lines 1-4 and 6-7 | +| `d` | sed '1,4 d' input.txt | Print lines except 1-4 | +| `w` | sed -n '1,4 w output.txt' input.txt | Write pattern space to file | +| `a` | sed '2 a new-line' input.txt | Append line after | +| `i` | sed '2 i new-line' input.txt | Insert line before | +<!--rehype:className=show-header--> + +### 空间命令 + +| Command | Description | +|---------|--------------------------------------------------------------| +| `n` | 打印模式空间,空模式空间,读取下一行 | +| `x` | 用保持空间交换模式空间 | +| `h` | 复制模式空间以保持空间 | +| `H` | 追加模式空间以保持空间 | +| `g` | 将保持空间复制到模式空间 | +| `G` | 将保持空间附加到模式空间 | + +### Flags + +```shell +$ sed 's/old/new/[flags]' [input-file] +``` + +--- + +| Flag | Description | +|----------|--------------------------------------------| +| `g` | 全球替代 | +| `1,2...` | 替换第 n 次出现 | +| `p` | 仅打印替换的行 | +| `w` | 仅将替换的行写入文件 | +| `I` | 搜索时忽略大小写 | +| `e` | 在命令行中替换并执行 | + + +### 循环命令 + +| Command | Description | +|-----------|--------------------------------------------------------------------| +| `b label` | 分支到标签(用于循环) | +| `t label` | 仅在成功替换时分支到标签(用于循环) | +| `:label` | b 和 t 命令的标签(用于循环) | +| `N` | 将下一行追加到模式空间 | +| `P` | 多行打印第一行 | +| `D` | 删除多行中的第一行 | + +### 杂项标志 + +| Flag | Description | +|----------------|---------------| +| `/ \| ^ @ ! #` | 替换分隔符可以是任何字符 | +| `&` | 获取匹配的模式 | +| `( ) \1 \2 \3` | 使用 `(` 和 `)` 进行分组。<br>使用 `\1`、`\2` 替换来引用组 | + +Sed 示例 +---------- + +### 替换文本 +<!--rehype:wrap-class=row-span-2--> + +替换所有出现的字符串 + +```shell +$ sed 's/old/new/g' file.txt +``` + +仅替换第 n 次出现的字符串 + +```shell +$ sed 's/old/new/2' file.txt +``` + +仅在第 5 行替换替换字符串 + +```shell +$ sed '5 s/old/new/' file.txt +``` + +将“world”替换为“universe”,但前提是该行以“hello”开头 + +```shell +$ sed '/hello/s/world/universe/' file.txt +``` + +从每行的末尾删除“\” + +```shell +$ sed 's/\\$//' file.txt +``` + +删除每行开头的所有空格 + +```shell +$ sed 's/^\s*//' file.txt +``` + +删除评论。 即使是那些在行尾的 + +```shell +$ sed 's/#.*$//' file.txt +``` + +### 搜索文本 + +搜索字符串并仅打印匹配的行 + +```shell +$ sed -n '/hello/p' file.txt +``` + +不区分大小写的搜索 + +```shell +$ sed -n '/hello/Ip' file.txt +``` + +搜索字符串,但仅输出不匹配的行 + +```shell +$ sed -n '/hello/!p' file.txt +``` + +### 追加行 + +在第 2 行之后追加一行 + +```shell +$ sed '2a Text after line 2' file.txt +``` + +在文件末尾追加一行 + +```shell +$ sed '$a THE END!' file.txt +``` + +从第 3 行开始,每 3 行后追加一行 + +```shell +$ sed '3~3a Some text' file.txt +``` + +### 编号 +<!--rehype:wrap-class=col-span-2--> + +文件的数字行(简单的左对齐) + +```shell +$ sed = file.txt | sed 'N;s/\n/\t/' +``` + +文件的数字行(数字在左,右对齐) + +```shell +$ sed = file.txt | sed 'N; s/^/ /; s/ *\(.\{6,\}\)\n/\1 /' +``` + +文件的数字行,但如果行不为空,则仅打印数字 + +```shell +$ sed '/./=' file.txt | sed '/./N; s/\n/ /' +``` + +计算行数(模拟“wc -l”) + +```shell +$ sed -n '$=' +``` + +### 前置行 + +在第 5 行之前插入文本 + +```shell +$ sed '5i line number five' file.txt +``` + +在包含“hello”的每一行之前插入“示例:” + +```shell +$ sed '/hello/i Example: ' file.txt +``` + +### 删除行 + +删除文件中的第 5-7 行 + +```shell +$ sed '5,7d' file.txt +``` + +删除从第 3 行开始的每 2 行 + +```shell +$ sed '3~2d' file.txt +``` + +删除文件的最后一行 + +```shell +$ sed '$d' file.txt +``` + +删除以“Hello”开头的行 + +```shell +$ sed '/^Hello/d' file.txt +``` + +删除所有空行 + +```shell +$ sed '/^$/d' file.txt +``` + +删除以“#”开头的行 + +```shell +$ sed '/^#/d' file.txt +``` + +### 文件间距 + +双倍行距 + +```shell +$ sed G +``` + +删除所有空行和双空格 + +```shell +$ sed '/^$/d;G' +``` + +三倍空间文件 + +```shell +$ sed 'G;G' +``` + +撤消双倍行距 + +```shell +$ sed 'n;d' +``` + +在匹配“正则表达式”的行上方插入一个空行 + +```shell +$ sed '/regex/{x;p;x;}' +``` + +在匹配“正则表达式”的行下方插入一个空行 + +```shell +$ sed '/regex/G' +``` + +在匹配“正则表达式”的行周围插入一个空行 + +```shell +$ sed '/regex/{x;p;x;G;}' +``` + +另见 +---------- + +- [sed 备忘单](https://gist.github.com/ssstonebraker/6140154) _(gist.github.com)_ diff --git a/scripts/create.mjs b/scripts/create.mjs index 507de8b..9e12037 100644 --- a/scripts/create.mjs +++ b/scripts/create.mjs @@ -82,8 +82,8 @@ export function getTocsTree(arr = [], result = []) { } export function create(str = '', options = {}) { - let title = str.match(/[^===]+(?=[===])/g); - let description = str.match(/\n==={1,}\n+([\s\S]*?)\n/g); + let title = str.match(/[^===]+(?=[===])/g) || []; + let description = str.match(/\n==={1,}\n+([\s\S]*?)\n/g) || []; title = title[0] || ''; description = (description[0] || '').replace(/^\n[=\n]+/, '').replace(/\[([\s\S]*?)?\]\(([\s\S]*?)?\)/g, '$1').replace(/\n/, ''); const subTitle = options.filename && !options.isHome ? `${options.filename} cheatsheet & `: ''