文章目录
修改命令行提示符
显示出当前命令行提示符的设定
echo $PS1
比如输出为:
\[\e]0;\u@\h: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$
- \u : 代表当前用户名
- \h : 代表当前主机名
- \w : 代表当前工作路径
如果需要取消主机名的显示,则删除 \h 即可,可以使用如下命令:
export PS1="\[\e]0;\u: \w\a\]${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$"
如果需要永久修改,则可以直接修改 .bashrc
调整 shell 提示符长度
有时候进入路径太长,在Ubuntu下面,shell 的提示符会特别长,可以通过一下命令设置,只显示最后三层的目录
PROMPT_DIRTRIM=3
shell 注释
注释单行
#this is comment
注释多行
: <<'END'
the lines between END are comments
END
通过shell脚本动态生成文件
将下面的 file_name 替换为自己的文件名。
cat << 'EOF' > file_name
multi-lines need to write file
EOF
复制同一个目录下的多个文件
复制同一个目录下的多个文件,注意文件名之间不能有空格,否则会被分开解析
cp /home/usr/dir/{file1,file2,file3,file4} /home/usr/destination/
grep 解析 -
(连字符, hyphen)
man find | grep -e -o
这里将 -o 作为字符串整体搜索,不转义 -
.
-e PATTERNS, --regexp=PATTERNS
Use PATTERNS as the patterns. If this option is used multiple times or is combined with the -f (--file) option, search for all
patterns given. This option can be used to protect a pattern beginning with “-”.
动态查看一个持续更新的文本
tail -f filename.txt
显示一个文件的绝对路径
$ realpath filename.txt
/the/path/to/filename.txt
查看磁盘空间占用情况
df -hl
df - report file system disk space usage
- -h: 以更友好的形式展示大小
- -l: 只展示本地文件系统
查看各个文件的大小
du -ah --max-depth=1
- -a: 展示所有文件,默认是只显示文件夹
- -h: 以更友好的形式展示大小
- –max-depth: 只遍历一层,默认遍历所有子目录
查询域名的ip地址
dig domainname.com @8.8.8.8
检查字符串是否以指定字符串开头
if [[ "$HOST" =~ ^user.* ]]; then
echo "yes"
fi
或
if [[ "$HOST" =~ ^user.*|^host1 ]]; then
echo "yes"
fi