diff --git a/docs/find.html b/docs/find.html new file mode 100644 index 00000000..e6a501cd --- /dev/null +++ b/docs/find.html @@ -0,0 +1,396 @@ + + + + +Find 备忘清单 + & find cheatsheet & Quick Reference + + + + + +

Find 备忘清单

+

这是 Linux find 命令备忘单的快速参考列表,包含常用选项和示例。

+

入门

+

用法

+
$ find [path...] [options] [expression]
+
+

通配符

+
$ find . -name "*.txt"
+$ find . -name "2020*.csv"
+$ find . -name "json_*"
+
+
+ +

参数示例

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
参数示例描述
-typefind . -type d仅查找目录
-namefind . -type f -name "*.txt"按名称查找文件
-inamefind . -type f -iname "hello"按名称查找文件(不区分大小写)
-sizefind . -size +1G查找大于 1G 的文件
-userfind . -type d -user jack查找杰克的文件
-regexfind /var -regex '.*/tmp/.*[0-9]*.file'将正则表达式与查找一起使用
-maxdepthfind . -maxdepth 1 -name "a.txt"在当前目录和子目录中
-mindepthfind / -mindepth 3 -maxdepth 5 -name pass在子目录级别 2 和 4 之间
+

类型

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
-type d目录
-type f文件
-type l符号链接
-type b缓冲块
-type c无缓冲字符
-type p命名管道
-type s插座
+

大小

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
-size b512 字节块(默认)
-size c字节
-size k千字节
-size M兆字节
-size G千兆字节
-size T太字节_(仅限 BSD)_
-size PPB (仅 BSD)
+

大小 +/-

+

查找所有大于 10MB 的文件

+
$ find / -size +10M
+
+

查找所有小于 10MB 的文件

+
$ find / -size -10M
+
+

查找所有正好为 10M 的文件

+
$ find / -size 10M
+
+

查找 100MB 和 1GB 之间的大小

+
$ find / -size +100M -size -1G
+
+

像往常一样,+- 前缀表示大于和小于。

+

名称

+

在当前目录中使用名称查找文件

+
$ find . -name tecmint.txt
+
+

查找主目录下的文件

+
$ find /home -name tecmint.txt
+
+

使用名称查找文件并忽略大小写

+
$ find /home -iname tecmint.txt
+
+

使用名称查找目录

+
$ find / -type d -name tecmint
+
+

使用名称查找php文件

+
$ find . -type f -name tecmint.php
+
+

查找目录下的所有php文件

+
$ find . -type f -name "*.php"
+
+

权限

+

查找权限为 777 的文件。

+
$ find . -type f -perm 0777 -print
+
+

查找未经许可的文件 777.

+
$ find / -type f ! -perm 777
+
+

查找 SUID 集文件。

+
$ find / -perm /u=s
+
+

查找 SGID 集文件。

+
$ find / -perm /g=s
+
+

查找只读文件。

+
$ find / -perm /u=r
+
+

查找可执行文件。

+
$ find / -perm /a=x
+
+

所有者和组

+

根据用户查找单个文件

+
$ find / -user root -name tecmint.txt
+
+

根据用户查找所有文件

+
$ find /home -user tecmint
+
+

根据组查找所有文件

+
$ find /home -group developer
+
+

查找用户的特定文件

+
$ find /home -user tecmint -iname "*.txt"
+
+

多个文件名

+
$ find . -type f \( -name "*.sh" -o -name "*.txt" \)
+
+

查找带有 .sh.txt 扩展名的文件

+

多个目录

+
$ find /opt /usr /var -name foo.scala -type f
+
+

查找具有多个目录的文件

+

空的

+
$ find . -type d -empty
+
+

删除目录中的所有空文件

+
$ find . -type f -empty -delete
+
+

查找日期和时间

+

方法

+ + + + + + + + + + + + + + + + + + + + + +
OptionDescription
atime访问时间(上次文件 + 打开) +
mtime修改时间(上次文件 + 内容被修改) +
ctime更改时间(上次文件 + inode 已更改) +
+

示例

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
OptionDescription
-mtime +024 小时前修改
-mtime 0从现在到 1 天前修改
-mtime -1不到 1 天前修改(与 -mtime 0 相同)
-mtime 124 至 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 天修改的文件

+
$ find / -mtime 50
+
+

查找最近 50 天访问的文件

+
$ find / -atime 50
+
+

查找最近 50-100 天修改的文件

+
$ find / -mtime +50 –mtime -100
+
+

查找最近 1 小时内更改的文件

+
$ find / -cmin -60
+
+

查找最近 1 小时内修改过的文件

+
$ find / -mmin -60
+
+

查找最近 1 小时内访问过的文件

+
$ find / -amin -60
+
+

查找并

+

查找和删除

+

查找并删除多个文件

+
$ find . -type f -name "*.mp3" -exec rm -f {} \;
+
+

查找和删除单个文件

+
$ find . -type f -name "tecmint.txt" -exec rm -f {} \;
+
+

查找和删除 100mb 文件

+
$ find / -type f -size +100m -exec rm -f {} \;
+
+

查找特定文件并删除

+
$ find / -type f -name *.mp3 -size +10m -exec rm {} \;
+
+

查找和替换

+
$ find ./ -type f -exec sed -i 's/find/replace/g' {} \;
+$ find ./ -type f -readable -writable -exec sed -i "s/old/new/g" {} \;
+
+

参见:sed 命令

+

查找和重命名

+
$ find . -type f -name 'file*' -exec mv {} {}_renamed \;
+$ find . -type f -name 'file*' -exec sh -c 'x="{}"; mv "$x" "${x}.bak"' \;
+
+

查找和移动

+
$ find . -name '*.mp3' -exec mv {} /tmp/music \;
+
+

查找并将其移动到特定目录

+

查找和复制

+
$ find . -name '*2020*.xml' -exec cp -r "{}" /tmp/backup \;
+
+

查找并将其复制到特定目录

+

查找并连接

+
$ find download -type f -iname '*.csv' | xargs cat > merged.csv
+$ find download -type f -name '*.gz' -exec cat {} \; > output
+
+

查找和排序

+
$ find . -printf "%T+\t%p\n" | sort
+$ find . -printf "%T+\t%p\n" | sort -r
+
+

查找和 chmod

+

查找文件并将权限设置为 644。

+
$ find / -type f -perm 0777 -print -exec chmod 644 {} \;
+
+

查找目录并将权限设置为 755。

+
$ find / -type d -perm 777 -print -exec chmod 755 {} \;
+
+

查找并 tar

+
$ 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.html b/docs/sed.html new file mode 100644 index 00000000..041db766 --- /dev/null +++ b/docs/sed.html @@ -0,0 +1,362 @@ + + + + +Sed 备忘清单 + & sed cheatsheet & Quick Reference + + + + + +

Sed 备忘清单

+

入门

+

Sed 用法

+

语法

+
$ sed [options] command [input-file]
+
+

带管道

+
$ cat report.txt | sed 's/Nick/John/g'
+
+
$ echo '123abc' | sed 's/[0-9]+//g'
+
+

选项示例

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
参数示例描述
-ised -ibak 's/On/Off/' php.ini直接备份和修改输入文件
-Esed -E 's/[0-9]+//g' input-file使用扩展正则表达式
-nsed -n '3 p' config.conf禁止默认图案空间打印
-fsed -f script.sed config.conf执行 sed 脚本文件
-esed -e 'command1' -e 'command2' input-file执行多个 sed 命令
+

多个命令

+
$ echo "hello world" | sed -e 's/h/H/g' -e 's/w/W/g'
+Hello World
+
+

使用 -e 执行多个 sed 命令

+

Sed 脚本

+
$ 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

+
$ 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 命令

+

命令

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandExampleDescription
psed -n '1,4 p' input.txtPrint lines 1-4
psed -n -e '1,4 p' -e '6,7 p' input.txtPrint lines 1-4 and 6-7
dsed '1,4 d' input.txtPrint lines except 1-4
wsed -n '1,4 w output.txt' input.txtWrite pattern space to file
ased '2 a new-line' input.txtAppend line after
ised '2 i new-line' input.txtInsert line before
+

空间命令

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
n打印模式空间,空模式空间,读取下一行
x用保持空间交换模式空间
h复制模式空间以保持空间
H追加模式空间以保持空间
g将保持空间复制到模式空间
G将保持空间附加到模式空间
+

Flags

+
$ sed 's/old/new/[flags]' [input-file]
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FlagDescription
g全球替代
1,2...替换第 n 次出现
p仅打印替换的行
w仅将替换的行写入文件
I搜索时忽略大小写
e在命令行中替换并执行
+

循环命令

+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
CommandDescription
b label分支到标签(用于循环)
t label仅在成功替换时分支到标签(用于循环)
:labelb 和 t 命令的标签(用于循环)
N将下一行追加到模式空间
P多行打印第一行
D删除多行中的第一行
+

杂项标志

+ + + + + + + + + + + + + + + + + + + + + +
FlagDescription
/ | ^ @ ! #替换分隔符可以是任何字符
&获取匹配的模式
( ) \1 \2 \3使用 () 进行分组。
使用 \1\2 替换来引用组
+

Sed 示例

+

替换文本

+

替换所有出现的字符串

+
$ sed 's/old/new/g' file.txt
+
+

仅替换第 n 次出现的字符串

+
$ sed 's/old/new/2' file.txt
+
+

仅在第 5 行替换替换字符串

+
$ sed '5 s/old/new/' file.txt
+
+

将“world”替换为“universe”,但前提是该行以“hello”开头

+
$ sed '/hello/s/world/universe/' file.txt
+
+

从每行的末尾删除“\”

+
$ sed 's/\\$//' file.txt
+
+

删除每行开头的所有空格

+
$ sed 's/^\s*//' file.txt
+
+

删除评论。 即使是那些在行尾的

+
$ sed 's/#.*$//' file.txt
+
+

搜索文本

+

搜索字符串并仅打印匹配的行

+
$ sed -n '/hello/p' file.txt
+
+

不区分大小写的搜索

+
$ sed -n '/hello/Ip' file.txt
+
+

搜索字符串,但仅输出不匹配的行

+
$ sed -n '/hello/!p' file.txt
+
+

追加行

+

在第 2 行之后追加一行

+
$ sed '2a Text after line 2' file.txt
+
+

在文件末尾追加一行

+
$ sed '$a THE END!' file.txt
+
+

从第 3 行开始,每 3 行后追加一行

+
$ sed '3~3a Some text' file.txt
+
+

编号

+

文件的数字行(简单的左对齐)

+
$ sed = file.txt | sed 'N;s/\n/\t/'
+
+

文件的数字行(数字在左,右对齐)

+
$ sed = file.txt | sed 'N; s/^/   /; s/ *\(.\{6,\}\)\n/\1  /'
+
+

文件的数字行,但如果行不为空,则仅打印数字

+
$ sed '/./=' file.txt | sed '/./N; s/\n/ /'
+
+

计算行数(模拟“wc -l”)

+
$ sed -n '$='
+
+

前置行

+

在第 5 行之前插入文本

+
$ sed '5i line number five' file.txt
+
+

在包含“hello”的每一行之前插入“示例:”

+
$ sed '/hello/i Example: ' file.txt
+
+

删除行

+

删除文件中的第 5-7 行

+
$ sed '5,7d' file.txt
+
+

删除从第 3 行开始的每 2 行

+
$ sed '3~2d' file.txt
+
+

删除文件的最后一行

+
$ sed '$d' file.txt
+
+

删除以“Hello”开头的行

+
$ sed '/^Hello/d' file.txt
+
+

删除所有空行

+
$ sed '/^$/d' file.txt
+
+

删除以“#”开头的行

+
$ sed '/^#/d' file.txt
+
+

文件间距

+

双倍行距

+
$ sed G
+
+

删除所有空行和双空格

+
$ sed '/^$/d;G'
+
+

三倍空间文件

+
$ sed 'G;G'
+
+

撤消双倍行距

+
$ sed 'n;d'
+
+

在匹配“正则表达式”的行上方插入一个空行

+
$ sed '/regex/{x;p;x;}'
+
+

在匹配“正则表达式”的行下方插入一个空行

+
$ sed '/regex/G'
+
+

在匹配“正则表达式”的行周围插入一个空行

+
$ sed '/regex/{x;p;x;G;}'
+
+

另见

+ + +
+ diff --git a/index.html b/index.html index 1f47aaca..f27d8f95 100644 --- a/index.html +++ b/index.html @@ -34,6 +34,8 @@

Cron Git + find + Sed

其它