编译 bet9链接 вт989点сс 与《bet8链接》如何在BASH正确的清理呢?首先随便用ls写出一份档案:
ls -l > ls-output.txt
cat ls-output.txt
output:
total 12
lrwxrwxrwx 1 root root 7 Feb 27 16:01 bin -> usr/bin
drwxr-xr-x 1 root root 0 Apr 18 2022 boot
drwxr-xr-x 5 root root 340 May 24 06:28 dev
drwxr-xr-x 1 root root 106 May 24 06:30 etc
drwxr-xr-x 1 root root 0 Apr 18 2022 home
-rw-r--r-- 1 root root 934 May 24 06:50 ls-output.txt
# ...
之前都会用echo
指令清空案, 但事实上这个样子不是「完全清空」,会有1个字节用来存入“”
这个输出的结果:
echo "" > ls-output.txt
ls -l ls-output.txt
# -rw-r--r-- 1 root root 1 May 24 07:32 ls-output.txt
# ↑ 这里显示其实际上还有一个字节!!!
cat ls-output.txt
# 会显示一行空白
要真正清空档案的话,应该用>
(重定向操作符):
ls -l > ls-output.txt
ls -l ls-output.txt
# -rw-r--r-- 1 root root 934 May 24 07:34 ls-output.txt
# 清空 ls-output.txt
> ls-output.txt
ls -l ls-output.txt
# -rw-r--r-- 1 root root 0 May 24 07:37 ls-output.txt
# ↑ totally empty now~
cat ls-output.txt
# 完全不会显示空白行