## # bash脚本 ```{lang} x=100 x=x/10 #在输出的过程中直接运算 echo $((x/10)) ``` Bash 这个名字是 Bourne-Again SHell 的首字母缩写,是对 Stephen Bourne 这个名字的双关语,Stephen Bourne 是 Bash 的前任之一的创造者。 1. 对变量进行赋值的时候等于号的两边不能有空格。 错误示例 ```{lang} users = "a b c" ``` 正确写法 ```{lang} user="a b c" ``` 1. function 如果可能的话,函数应该有提到描述、全局变量、参数、输出和返回值的注释。 ```{lang} ####################################### # Description: Hello function # Globals: # None # Arguments: # Single input argument # Outputs: # Value of input argument # Returns: # 0 if successful, non-zero on error. ####################################### function hello(){ echo "Hello $1!" } ``` 1. 为了能够调试bash脚本,可以在执行脚本的时候使用参数-x 2. jq JSON处理器 3. 对于if表达式,[]代表的是单个表达式,例如if [ $name = "李明" ],[[ ]]代表支持复合表达式,例如if [[ 8 -eq 11 && "hello" == "hello" || 1 -eq 3 || 1 -eq 1 ]]或if [[ ${name} = *"Li Ming"* ]] 格式化输出日期 ```{lang} Usage: date +FORMAT 常用的FORMAT如下所示:(假设此时为Tue Jan 19 11:14:07 AM CST 2038) `%d` :本月第几号 `%H` : 获取当前小时 `%m` : 获取当前月份 `%Y` : 获取当前年份 `%s` : 自1970-01-01 00:00:00 UTC至今的秒数 `%N` : 获取当前纳秒 example: $ echo $(date +%Y-%m-%d%t%H:%M:%S) 2022-08-31 17:48:45 ``` ## # 进程管理 管道输入变参数 例如,删除当前文件夹下的所有文件 ls | xargs rm ps aux to repetitively update processes info, we can use top command. ```{lang} PID|%CPU |%MEM | VSZ | RSS |TTY | STAT| START | TIME |COMMAND |--|--|--|--|--|--|--|--|--|--| process ID||||||||cumulated time that cpu running on this process || ``` crontab is the program used to install, deinstall or list the tables used to drive the cron(8) daemon in Vixie Cron. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. crontab -e open the config file kill progress by name: kill -9 $(ps aux | grep 'top' | awk '{print $2}' | head -n 1) 忽略挂断信号,当ssh连接断开时,所有的子进程都会中断。 Run COMMAND, ignoring hangup signals. nohup COMMAND & ## # 系统管理 切换到root用户 sudo -i useradd sudo useradd /home/test test -p password > **注意** > 无论是否设置了密码都要在添加用户之后,再次设置一次密码,否则远程登陆时无法正常登录。sudo passwd test userdel usermod 查看进程套接字信息。端口信息 ss 这个程序主要是为了打印网络接口信息,但是这个程序在2022年来看已经是过时的了。 目前可以使用ss来替换netstat, ip route 来替换netstat -r, netstat -i 可以使用ip -s link. 使用ip maddr来替换netstat -g ```{lang} Usage: ss [ OPTIONS ] ss [ OPTIONS ] [ FILTER ] -h, --help this message -V, --version output version information -n, --numeric don't resolve service names -r, --resolve resolve host names -a, --all display all sockets -l, --listening display listening sockets -o, --options show timer information -e, --extended show detailed socket information -m, --memory show socket memory usage -p, --processes show process using socket -i, --info show internal TCP information --tipcinfo show internal tipc socket information -s, --summary show socket usage summary --tos show tos and priority information --cgroup show cgroup information -b, --bpf show bpf filter socket information -E, --events continually display sockets as they are destroyed -Z, --context display process SELinux security contexts -z, --contexts display process and socket SELinux security contexts -N, --net switch to the specified network namespace name -4, --ipv4 display only IP version 4 sockets -6, --ipv6 display only IP version 6 sockets -0, --packet display PACKET sockets -t, --tcp display only TCP sockets -M, --mptcp display only MPTCP sockets -S, --sctp display only SCTP sockets -u, --udp display only UDP sockets -d, --dccp display only DCCP sockets -w, --raw display only RAW sockets -x, --unix display only Unix domain sockets --tipc display only TIPC sockets --vsock display only vsock sockets -f, --family=FAMILY display sockets of type FAMILY FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help} -K, --kill forcibly close sockets, display what was closed -H, --no-header Suppress header line -O, --oneline socket's data printed on a single line --inet-sockopt show various inet socket options -A, --query=QUERY, --socket=QUERY QUERY := {all|inet|tcp|mptcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram|tipc}[,QUERY] -D, --diag=FILE Dump raw information about TCP sockets to FILE -F, --filter=FILE read filter information from FILE FILTER := [ state STATE-FILTER ] [ EXPRESSION ] STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES} TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing} connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} bucket := {syn-recv|time-wait} big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing} ``` 查看服务运行情况,并且可以开启或关闭指定服务 sudo update-rc.d ServiceName remove watch -n 2 sensors 每两秒刷新一下信息 ```{lang} Every 2.0s: sensors penetration-fqq: Mon Jun 6 15:56:22 2022 coretemp-isa-0000 //cpu 两个物理核心 Adapter: ISA adapter Package id 0: +37.0°C (high = +100.0°C, crit = +100.0°C) Core 0: +38.0°C (high = +100.0°C, crit = +100.0°C) Core 1: +35.0°C (high = +100.0°C, crit = +100.0°C) amdgpu-pci-0100 //gpu Adapter: PCI adapter vddgfx: 806.00 mV edge: +39.0°C (crit = +104000.0°C, hyst = -273.1°C) slowPPT: 572.15 W (cap = 15.00 W) pch_skylake-virtual-0 Adapter: Virtual device temp1: +39.5°C BAT0-acpi-0 Adapter: ACPI interface in0: 8.44 V ``` ## # GIT 修改分支名称: branch -m [old_branch] 显示全部分支 git branch -a 显示当前分支名称 git branch --show-current git config --global --edit your name and email address were configured in this file ```{lang} ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git log commit de14fcc22521e0c64bfdb4dccbda9e281efe2acc (HEAD -> master) Author: root Date: Sat May 7 15:02:56 2022 +0800 edit flagj commit 1dbfdfb1d913a4c517f550fc65b9d24ee907c0ea Author: root Date: Sat May 7 14:17:31 2022 +0800 flag ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git log --all commit de14fcc22521e0c64bfdb4dccbda9e281efe2acc (HEAD -> master) Author: root Date: Sat May 7 15:02:56 2022 +0800 edit flagj commit 1dbfdfb1d913a4c517f550fc65b9d24ee907c0ea Author: root Date: Sat May 7 14:17:31 2022 +0800 flag ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ ``` ```{lang} ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git diff HEAD HEAD^ diff --git a/flag b/flag index 3d18cbe..4b30686 100644 --- a/flag +++ b/flag @@ -1,3 +1 @@ flag{} -nihao -laskfjaslfj ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ ``` reset current HEAD to specific state ```{lang} ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ sudo git reset --hard HEAD^ 128 ⨯ HEAD is now at 1dbfdfb flag ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git log commit 1dbfdfb1d913a4c517f550fc65b9d24ee907c0ea (HEAD -> master) Author: root Date: Sat May 7 14:17:31 2022 +0800 flag ``` ## # 文件管理 压缩工具 压缩指定文件夹及其子文件(默认就会压缩全部子文件和目录,避免出现错误,不需要使用-r参数) 7za a -t7z 目标文件名称.7z /path/to/derectory 解压 7za x 压缩文件.7z tar -xf archive.tar ```{lang} Examples: tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. tar -tvf archive.tar # List all files in archive.tar verbosely. tar -xf archive.tar # Extract all files from archive.tar. tar -rvf archive.tar newfile # append newfile to the end of an archive Main operation mode: -A, --catenate, --concatenate append tar files to an archive -c, --create create a new archive --delete delete from the archive (not on mag tapes!) -d, --diff, --compare find differences between archive and file system -r, --append append files to the end of an archive --test-label test the archive volume label and exit -t, --list list the contents of an archive -u, --update only append files newer than copy in archive -x, --extract, --get extract files from an archive ``` just work on single file gzip -d archive.gz ```{lang} Usage: gzip [OPTION]... [FILE]... Compress or uncompress FILEs (by default, compress FILES in-place). Mandatory arguments to long options are mandatory for short options too. -c, --stdout write on standard output, keep original files unchanged -d, --decompress decompress -f, --force force overwrite of output file and compress links -h, --help give this help -k, --keep keep (don't delete) input files -l, --list list compressed file contents -L, --license display software license -n, --no-name do not save or restore the original name and timestamp -N, --name save or restore the original name and timestamp -q, --quiet suppress all warnings -r, --recursive operate recursively on directories --rsyncable make rsync-friendly archive -S, --suffix=SUF use suffix SUF on compressed files --synchronous synchronous output (safer if system crashes, but slower) -t, --test test compressed file integrity -v, --verbose verbose mode -V, --version display version number -1, --fast compress faster -9, --best compress better ``` OpenSSH secure file copy copy single file from remote. scp @: copy entire directories. scp -r @: copy local file to remote. scp -P @:/remote/folder/fileName cat -n 显示行号 cat -b 显示行号(除了空白行) cat -b >> 追加前两个文本内容到3 cat /dev/null > 清空文件 cat /dev/fd0 > 制作镜像文件 cat > /dev/fd0 将文件写入到软盘 chmod u=rwx,g=r,o=r chmod u+r chmod -R 777 递归变更权限 find ./ -name 'log.txt' find ./ -perm -u=x -type f 查找类型为一般文件且可运行的文件 ## # 内容编辑 wc -w 显示字数 wc -l 显示行数 grep 'log' 排除指定内容的行 grep -v 'name' sort -n [file] 按照数值大小排序 sort -r [file] 逆排序 vim -r show temp file in /tmp, /var/tmp/, ~/tmp/, './' vim -r flag 显示当前目录下的flag的缓存文件,当对flag文件进行编辑的过程中,意外强制关闭了vim编辑器之后,可以通过此方法来进行文件的恢复, 打开.flag.swq文件,并运行:wq命令即可将缓存中的修改应用到原文件中了,然后删除掉该缓存文件即可。若在编辑缓存文件的过程中,意外推出编辑器,则会生成新的缓存文件.flag.swo, 以此类推,若在编辑缓存文件的过程中仍然遇到了vim意外推出的状况,则会在当前目录下生成文件.flag.swn, .flag.swm, .flag.swl ...... 必须将换问文件全部删除之后才能正常打开原始文件。 dw 删除当前单词直到下一个单词开头(不包含首字母) de 删除单词,直到该单词末尾(包含最后一个单词) d$ 删除单词一整行 d2w 删除两个单词 dd 删除一整行,并保存行到vim注册器中 p 将注册器中保存的行插入到当前行的下一行 3dd 删除三行 2w 光标移动两个单词至单词开头 3e 光标移动两个单词至单词末尾 0 移动光标到行首 u 撤销动作 U 撤销当前行的所有动作 CTRL-R 重做 rx 替换当前光标下的字为x R 输入并替换文本 ce 删除单词至词尾并插入 cc 清空本行并插入 G 移动光标到文件的最后一行 g 移动光标到文本的第一行 505G 移动到第505行 CTRL-O 撤销行跳转操作 CTRL-I 重做行跳转操纵 % 匹配光标所在的括号对应的括号 :s/thee/the/g 替换行中所有的thee为the :1,5s/old/new/g 替换1到5行的old为new :%s/old/new/g 全文替换 :%s/old/new/gc 替换之前会提示 CTRL-G 文档的状态 ?hello 查找当前光标之前的所有的hello /hello 查找当前光标之后的hello n 移动到下一个查找结果 N 移动到上一个查找结果 :!ls 执行外部命令 :w filename 保存当前文件为指定名称 o 在下方插入一行 O 在上方插入一行 a 在当前光标指向的字的后面插入 y 复制 p 粘贴 j$``j0 控制每次移动到下一行时,光标所在位置时行首还是行尾 :set hls 搜索结果高亮显示 :set ic 忽略大小写 :set noic 不忽略大小写 /hello\c 本次查找忽略大小写 CTRL-D 显示所有匹配的命令 自动补全 :set number 显示行号 ## # 磁盘管理 du -sh * 使用合适的单位显示当前文件夹下的全部文件(不递归) df -h 显示磁盘容量使用合适的单位 Loading... ## # bash脚本 ```{lang} x=100 x=x/10 #在输出的过程中直接运算 echo $((x/10)) ``` Bash 这个名字是 Bourne-Again SHell 的首字母缩写,是对 Stephen Bourne 这个名字的双关语,Stephen Bourne 是 Bash 的前任之一的创造者。 1. 对变量进行赋值的时候等于号的两边不能有空格。 错误示例 ```{lang} users = "a b c" ``` 正确写法 ```{lang} user="a b c" ``` 1. function 如果可能的话,函数应该有提到描述、全局变量、参数、输出和返回值的注释。 ```{lang} ####################################### # Description: Hello function # Globals: # None # Arguments: # Single input argument # Outputs: # Value of input argument # Returns: # 0 if successful, non-zero on error. ####################################### function hello(){ echo "Hello $1!" } ``` 1. 为了能够调试bash脚本,可以在执行脚本的时候使用参数-x 2. jq JSON处理器 3. 对于if表达式,[]代表的是单个表达式,例如if [ $name = "李明" ],[[ ]]代表支持复合表达式,例如if [[ 8 -eq 11 && "hello" == "hello" || 1 -eq 3 || 1 -eq 1 ]]或if [[ ${name} = *"Li Ming"* ]] 格式化输出日期 ```{lang} Usage: date +FORMAT 常用的FORMAT如下所示:(假设此时为Tue Jan 19 11:14:07 AM CST 2038) `%d` :本月第几号 `%H` : 获取当前小时 `%m` : 获取当前月份 `%Y` : 获取当前年份 `%s` : 自1970-01-01 00:00:00 UTC至今的秒数 `%N` : 获取当前纳秒 example: $ echo $(date +%Y-%m-%d%t%H:%M:%S) 2022-08-31 17:48:45 ``` ## # 进程管理 管道输入变参数 例如,删除当前文件夹下的所有文件 ls | xargs rm ps aux to repetitively update processes info, we can use top command. ```{lang} PID|%CPU |%MEM | VSZ | RSS |TTY | STAT| START | TIME |COMMAND |--|--|--|--|--|--|--|--|--|--| process ID||||||||cumulated time that cpu running on this process || ``` crontab is the program used to install, deinstall or list the tables used to drive the cron(8) daemon in Vixie Cron. Each user can have their own crontab, and though these are files in /var/spool/cron/crontabs, they are not intended to be edited directly. crontab -e open the config file kill progress by name: kill -9 $(ps aux | grep 'top' | awk '{print $2}' | head -n 1) 忽略挂断信号,当ssh连接断开时,所有的子进程都会中断。 Run COMMAND, ignoring hangup signals. nohup COMMAND & ## # 系统管理 切换到root用户 sudo -i useradd sudo useradd /home/test test -p password > **注意** > 无论是否设置了密码都要在添加用户之后,再次设置一次密码,否则远程登陆时无法正常登录。<code>sudo passwd test</code> userdel usermod 查看进程套接字信息。端口信息 ss 这个程序主要是为了打印网络接口信息,但是这个程序在2022年来看已经是过时的了。 目前可以使用ss来替换netstat, ip route 来替换netstat -r, netstat -i 可以使用ip -s link. 使用ip maddr来替换netstat -g ```{lang} Usage: ss [ OPTIONS ] ss [ OPTIONS ] [ FILTER ] -h, --help this message -V, --version output version information -n, --numeric don't resolve service names -r, --resolve resolve host names -a, --all display all sockets -l, --listening display listening sockets -o, --options show timer information -e, --extended show detailed socket information -m, --memory show socket memory usage -p, --processes show process using socket -i, --info show internal TCP information --tipcinfo show internal tipc socket information -s, --summary show socket usage summary --tos show tos and priority information --cgroup show cgroup information -b, --bpf show bpf filter socket information -E, --events continually display sockets as they are destroyed -Z, --context display process SELinux security contexts -z, --contexts display process and socket SELinux security contexts -N, --net switch to the specified network namespace name -4, --ipv4 display only IP version 4 sockets -6, --ipv6 display only IP version 6 sockets -0, --packet display PACKET sockets -t, --tcp display only TCP sockets -M, --mptcp display only MPTCP sockets -S, --sctp display only SCTP sockets -u, --udp display only UDP sockets -d, --dccp display only DCCP sockets -w, --raw display only RAW sockets -x, --unix display only Unix domain sockets --tipc display only TIPC sockets --vsock display only vsock sockets -f, --family=FAMILY display sockets of type FAMILY FAMILY := {inet|inet6|link|unix|netlink|vsock|tipc|xdp|help} -K, --kill forcibly close sockets, display what was closed -H, --no-header Suppress header line -O, --oneline socket's data printed on a single line --inet-sockopt show various inet socket options -A, --query=QUERY, --socket=QUERY QUERY := {all|inet|tcp|mptcp|udp|raw|unix|unix_dgram|unix_stream|unix_seqpacket|packet|netlink|vsock_stream|vsock_dgram|tipc}[,QUERY] -D, --diag=FILE Dump raw information about TCP sockets to FILE -F, --filter=FILE read filter information from FILE FILTER := [ state STATE-FILTER ] [ EXPRESSION ] STATE-FILTER := {all|connected|synchronized|bucket|big|TCP-STATES} TCP-STATES := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|closed|close-wait|last-ack|listening|closing} connected := {established|syn-sent|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} synchronized := {established|syn-recv|fin-wait-{1,2}|time-wait|close-wait|last-ack|closing} bucket := {syn-recv|time-wait} big := {established|syn-sent|fin-wait-{1,2}|closed|close-wait|last-ack|listening|closing} ``` 查看服务运行情况,并且可以开启或关闭指定服务 sudo update-rc.d ServiceName remove watch -n 2 sensors 每两秒刷新一下信息 ```{lang} Every 2.0s: sensors penetration-fqq: Mon Jun 6 15:56:22 2022 coretemp-isa-0000 //cpu 两个物理核心 Adapter: ISA adapter Package id 0: +37.0°C (high = +100.0°C, crit = +100.0°C) Core 0: +38.0°C (high = +100.0°C, crit = +100.0°C) Core 1: +35.0°C (high = +100.0°C, crit = +100.0°C) amdgpu-pci-0100 //gpu Adapter: PCI adapter vddgfx: 806.00 mV edge: +39.0°C (crit = +104000.0°C, hyst = -273.1°C) slowPPT: 572.15 W (cap = 15.00 W) pch_skylake-virtual-0 Adapter: Virtual device temp1: +39.5°C BAT0-acpi-0 Adapter: ACPI interface in0: 8.44 V ``` ## # GIT 修改分支名称: branch -m [old_branch] <new_branch> 显示全部分支 git branch -a 显示当前分支名称 git branch --show-current git config --global --edit your name and email address were configured in this file ```{lang} ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git log commit de14fcc22521e0c64bfdb4dccbda9e281efe2acc (HEAD -> master) Author: root <root@kali.frunoob> Date: Sat May 7 15:02:56 2022 +0800 edit flagj commit 1dbfdfb1d913a4c517f550fc65b9d24ee907c0ea Author: root <root@kali.frunoob> Date: Sat May 7 14:17:31 2022 +0800 flag ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git log --all commit de14fcc22521e0c64bfdb4dccbda9e281efe2acc (HEAD -> master) Author: root <root@kali.frunoob> Date: Sat May 7 15:02:56 2022 +0800 edit flagj commit 1dbfdfb1d913a4c517f550fc65b9d24ee907c0ea Author: root <root@kali.frunoob> Date: Sat May 7 14:17:31 2022 +0800 flag ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ ``` ```{lang} ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git diff HEAD HEAD^ diff --git a/flag b/flag index 3d18cbe..4b30686 100644 --- a/flag +++ b/flag @@ -1,3 +1 @@ flag{} -nihao -laskfjaslfj ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ ``` reset current HEAD to specific state ```{lang} ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ sudo git reset --hard HEAD^ 128 ⨯ HEAD is now at 1dbfdfb flag ┌──(frunoob㉿kali)-[/var/www/html/git_test] └─$ git log commit 1dbfdfb1d913a4c517f550fc65b9d24ee907c0ea (HEAD -> master) Author: root <root@kali.frunoob> Date: Sat May 7 14:17:31 2022 +0800 flag ``` ## # 文件管理 压缩工具 压缩指定文件夹及其子文件(默认就会压缩全部子文件和目录,避免出现错误,不需要使用-r参数) 7za a -t7z 目标文件名称.7z /path/to/derectory 解压 7za x 压缩文件.7z tar -xf archive.tar ```{lang} Examples: tar -cf archive.tar foo bar # Create archive.tar from files foo and bar. tar -tvf archive.tar # List all files in archive.tar verbosely. tar -xf archive.tar # Extract all files from archive.tar. tar -rvf archive.tar newfile # append newfile to the end of an archive Main operation mode: -A, --catenate, --concatenate append tar files to an archive -c, --create create a new archive --delete delete from the archive (not on mag tapes!) -d, --diff, --compare find differences between archive and file system -r, --append append files to the end of an archive --test-label test the archive volume label and exit -t, --list list the contents of an archive -u, --update only append files newer than copy in archive -x, --extract, --get extract files from an archive ``` just work on single file gzip -d archive.gz ```{lang} Usage: gzip [OPTION]... [FILE]... Compress or uncompress FILEs (by default, compress FILES in-place). Mandatory arguments to long options are mandatory for short options too. -c, --stdout write on standard output, keep original files unchanged -d, --decompress decompress -f, --force force overwrite of output file and compress links -h, --help give this help -k, --keep keep (don't delete) input files -l, --list list compressed file contents -L, --license display software license -n, --no-name do not save or restore the original name and timestamp -N, --name save or restore the original name and timestamp -q, --quiet suppress all warnings -r, --recursive operate recursively on directories --rsyncable make rsync-friendly archive -S, --suffix=SUF use suffix SUF on compressed files --synchronous synchronous output (safer if system crashes, but slower) -t, --test test compressed file integrity -v, --verbose verbose mode -V, --version display version number -1, --fast compress faster -9, --best compress better ``` OpenSSH secure file copy copy single file from remote. scp <user-name>@<remote-ip>:<source-file-name> <target-file-path-name> copy entire directories. scp -r <user-name>@<remote-ip>:<source-directory-name> <target-directory-name> copy local file to remote. scp -P <port> <local/file/path> <user name>@<ip>:/remote/folder/fileName cat -n <file> 显示行号 cat -b <file> 显示行号(除了空白行) cat -b <file1> <file2> >> <file3> 追加前两个文本内容到3 cat /dev/null > <file> 清空文件 cat /dev/fd0 > <file> 制作镜像文件 cat <IMG_file> > /dev/fd0 将文件写入到软盘 chmod u=rwx,g=r,o=r <file> chmod u+r <file> chmod -R 777 <file> 递归变更权限 find ./ -name 'log.txt' find ./ -perm -u=x -type f 查找类型为一般文件且可运行的文件 ## # 内容编辑 wc -w 显示字数 wc -l 显示行数 grep 'log' 排除指定内容的行 grep -v 'name' sort -n [file] 按照数值大小排序 sort -r [file] 逆排序 vim -r show temp file in /tmp, /var/tmp/, ~/tmp/, './' vim -r flag 显示当前目录下的flag的缓存文件,当对flag文件进行编辑的过程中,意外强制关闭了vim编辑器之后,可以通过此方法来进行文件的恢复, 打开.flag.swq文件,并运行:wq命令即可将缓存中的修改应用到原文件中了,然后删除掉该缓存文件即可。若在编辑缓存文件的过程中,意外推出编辑器,则会生成新的缓存文件.flag.swo, 以此类推,若在编辑缓存文件的过程中仍然遇到了vim意外推出的状况,则会在当前目录下生成文件.flag.swn, .flag.swm, .flag.swl ...... 必须将换问文件全部删除之后才能正常打开原始文件。 dw 删除当前单词直到下一个单词开头(不包含首字母) de 删除单词,直到该单词末尾(包含最后一个单词) d$ 删除单词一整行 d2w 删除两个单词 dd 删除一整行,并保存行到vim注册器中 p 将注册器中保存的行插入到当前行的下一行 3dd 删除三行 2w 光标移动两个单词至单词开头 3e 光标移动两个单词至单词末尾 0 移动光标到行首 u 撤销动作 U 撤销当前行的所有动作 CTRL-R 重做 rx 替换当前光标下的字为x R 输入并替换文本 ce 删除单词至词尾并插入 cc 清空本行并插入 G 移动光标到文件的最后一行 g 移动光标到文本的第一行 505G 移动到第505行 CTRL-O 撤销行跳转操作 CTRL-I 重做行跳转操纵 % 匹配光标所在的括号对应的括号 :s/thee/the/g 替换行中所有的thee为the :1,5s/old/new/g 替换1到5行的old为new :%s/old/new/g 全文替换 :%s/old/new/gc 替换之前会提示 CTRL-G 文档的状态 ?hello 查找当前光标之前的所有的hello /hello 查找当前光标之后的hello n 移动到下一个查找结果 N 移动到上一个查找结果 :!ls 执行外部命令 :w filename 保存当前文件为指定名称 o 在下方插入一行 O 在上方插入一行 a 在当前光标指向的字的后面插入 y 复制 p 粘贴 j$``j0 控制每次移动到下一行时,光标所在位置时行首还是行尾 :set hls 搜索结果高亮显示 :set ic 忽略大小写 :set noic 不忽略大小写 /hello\c 本次查找忽略大小写 CTRL-D 显示所有匹配的命令 <TAB> 自动补全 :set number 显示行号 ## # 磁盘管理 du -sh * 使用合适的单位显示当前文件夹下的全部文件(不递归) df -h 显示磁盘容量使用合适的单位 最后修改:2025 年 07 月 14 日 © 允许规范转载 赞 别打赏,我怕忍不住购买辣条与续命水