linux清空文件的几种常见方法

linux清空文件的几种常见方法,第1张

1、使用重定向的方法

[root@centos7 ~]# du -h test.txt

4.0Ktest.txt

[root@centos7 ~]# >test.txt

2、使用true命令重定向清空文件

[root@centos7 ~]# du -h test.txt

4.0Ktest.txt

[root@centos7 ~]# true >test.txt

[root@centos7 ~]# du -h test.txt

0test.txt

3、使用cat/cp/dd命令及/dev/null设备来清空文件

[root@centos7 ~]# du -h test.txt

4.0Ktest.txt

[root@centos7 ~]# cat /dev/null > test.txt

[root@centos7 ~]# echo "Hello World" >test.txt

[root@centos7 ~]# du -h test.txt

4.0Ktest.txt

[root@centos7 ~]# cp /dev/null test.txt

cp:是否覆盖"test.txt"? y

[root@centos7 ~]# echo "Hello World" >test.txt

[root@centos7 ~]# dd if=/dev/null of=test.txt

4、使用echo命令清空文件

[root@centos7 ~]# echo "Hello World" >test.

[root@centos7 ~]# echo -n "" >test.txt==>要加上"-n"参数,默认情况下会"\n",也就是回车符

5、使用truncate命令清空文件

[root@centos7 ~]# du -h test.txt

4.0Ktest.txt

[root@centos7 ~]# truncate -s 0 test.txt -s参数用来设定文件的大小,清空文件,就设定为0;

Ubuntu Linux与Windows系统不同,Ubuntu Linux不会产生无用垃圾文件,但是在升级缓存中,Ubuntu Linux不会自动删除这些文件,今天就来说说这些垃圾文件清理方法。

1,非常有用的清理命令:

sudo apt-get autocleansudo apt-get cleansudo apt-get autoremove

这三个命令主要清理升级缓存以及无用包的。

2,清理opera/ firefox的缓存文件:

ls ~/.opera/cache4

ls ~/.mozilla/firefox/*.default/Cache

3,清理Linux下孤立的包:

图形界面下我们可以用:gtkorphan

sudo apt-get install gtkorphan -y

终端命令下我们可以用:deborphan

sudo apt-get install deborphan -y

4,卸载:tracker

这个东西一般我只要安装Ubuntu就会第一删掉tracker 他不仅会产生大量的cache文件而且还会影响开机速度。所以在新得利里面删掉就行。

5,删除多余的内核:一定不要删错哦,切记!!

打开终端敲命令:dpkg --get-selections|grep linux

有image的就是内核文件

删除老的内核文件:

sudo apt-get remove 内核文件名 (例如:linux-image-2.6.27-2-generic)

内核删除,释放空间了,应该能释放130-140M空间。

最后不要忘了看看当前内核:uname -a

附录:

包管理的临时文件目录:

包在

/var/cache/apt/archives

没有下载完的在

/var/cache/apt/archives/partial

  Linux每次重启都会清理 /tmp 下的临时文件,但既是服务器,又怎么会经常重启呢?当时有人也使用 rm -rf /tmp/* ,但是 rm -rf /tmp/* 可能误删,也可能造成进程僵死。所以楼主推荐你使用 tmpwatch 命令。如果系统是最小安装的情况下,可能没有 tmpwatch 命令,如: -bash: tmpwatch: command not found ,这个时候首先要安装 tmpwatch 命令,执行命令: yum install tmpwatch -y 即可。

tmpwatch   删除临时暂存文件

参数:

-a或--all 删除所有类型的文件。 -f或--force 强制删除文件或目录,类似rm指令的 -f 参数。 -q或--quiet 不显示指令执行过程。 -v或--verbose 详细显示指令执行过程。 --test 仅作测试,并不真的删除文件或目录。

使用实例:

注: 执行结果如下:


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/6211155.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-03-18
下一篇 2023-03-18

发表评论

登录后才能评论

评论列表(0条)

保存