mac电脑磁盘清理

背景

mac电脑磁盘空间比较下,只有128G

经常报磁盘空间不足,今天闲来无事,清理下无用的文件,释放下磁盘空间

清理

第一步当然是 df du命令来一波,看看情况再说拉

➜  ~ df -h
Filesystem      Size   Used  Avail Capacity iused      ifree %iused  Mounted on
/dev/disk1s5   113Gi   11Gi   24Gi    31%  488924 1182353556    0%   /
devfs          190Ki  190Ki    0Bi   100%     658          0  100%   /dev
/dev/disk1s1   113Gi   68Gi   24Gi    74% 1440272 1181402208    0%   /System/Volumes/Data
/dev/disk1s4   113Gi  9.0Gi   24Gi    27%      10 1182842470    0%   /private/var/vm
map auto_home    0Bi    0Bi    0Bi   100%       0          0  100%   /System/Volumes/Data/home

对于 du命令 需要注意的是,mac 下没有  --max-depth=x(文件深度,比如1)  选项;取而代之的是  -dx(x表示文件深度) 选项

为了方便 我限制了文件深度为1,这样方便快速定位哪个文件比较大

你可以使用如下命令

➜  ~ du -d1 | sort -rn
73160216    .
38524312    ./Library
14329760    ./workspace
8279904    ./.m2
5429344    ./Documents
3787192    ./.gradle

我更喜欢使用 -h 选项,可以直观明了的看出文件的大小

➜  ~ du -hd1 | grep 'G'
6.8G    ./workspace
 18G    ./Library
1.8G    ./.gradle
2.6G    ./Documents
3.9G    ./.m2
 35G    .

直接找 最大的文件夹 Library; cd进去 再次使用  du -hd1 | grep 'G' 命令

➜  ~ cd Library
➜  Library
➜  Library du -hd1 | grep 'G'
8.9G    ./Application Support
  0B    ./GameKit
 10M    ./Google
 11M    ./Group Containers
9.8G    ./Containers
4.6G    ./Caches
 18G    .

可以看到 Containers比较大  (@see  https://www.xiaohongshu.com/discovery/item/61d289350000000021038982)

根据苹果官网上的指南,可以有选择地进行删除:

“"~/Library/Containers"和"~/Library/Group Containers" 主要是沙盒程序在里面生成的用户本地存储,用于用户配置、数据保存等。

其实删除前,最好是先将其改名,然后运行程序,如果有“不适反应”,就将程序新建的删除,把改名的改回原名。注意,在运行时,不要做任何新数据的处理。”

删除以后真的节约出很大的空间,尤其是微信这种不知道存着什么东西的巨大乱麻一团。

进去文件夹后,你会看到你常用的各种app 尤其是微信 占了这个文件夹大小的 50%;我直接删除了

➜  Containers rm -fr com.tencent.xinWeChat

下面我们在看 Application Support 文件夹

➜  Containers cd ../ApplicationSupport
➜  ApplicationSupport du -hd1 | grep 'G'
896M    ./Google
6.6G    ./xxx公司内部的聊天工具
➜  ApplicationSupport cd xxx
➜  xxx du -hd1 | grep 'G'
292K    ./GPUCache
2.5G    ./databases
2.7G    ./downloads
6.6G    .

可以看到 downloads 文件夹比较大,看了下都是聊天记录下载的 中各种 图片、文件等 

我想把一年前的文件都删除,近一年的文件保留下来

➜  downloads find . -mtime +365 -exec rm -fr {} \;

完美,清出来了20+G的空间!

也可以使用 xagrs 来删除

find . -mtime +365 -print0 | xargs -0 rm -fr

 

扩展

关于find命令

-mtime n[smhdw]
If no units are specified, this primary evaluates to true if the difference between the
file last modification time and the time find was started, rounded up to the next full
24-hour period, is n 24-hour periods.

If units are specified, this primary evaluates to true if the difference between the file
last modification time and the time find was started is exactly n units. Please refer to
the -atime primary description for information on supported time units.

Possible time units are as follows:

s second
m minute (60 seconds)
h hour (60 minutes)
d day (24 hours)
w week (7 days)

大概意思是:默认的单位是天(d),是指: 最后一次修改发生在距离当前时间n*24小时至(n+1)*24 小时

PRIMARIES
All primaries which take a numeric argument allow the number to be preceded by a plus sign
(``+'') or a minus sign (``-''). A preceding plus sign means ``more than n'', a preceding minus
sign means ``less than n'' and neither means ``exactly n''.

大概意思是指:指定的数字参数前可以加 + 或者 - 来表示:n+1 天之前 或者  n天之内

例子

几个简单的例子说明下

find . –mtime n
文件/目录最后一次修改发生时间(即文件/目录的mtime)在距离find命令执行时间的 n天至(n+1)天之间 会被查找到

find . –mtime +n
文件/目录最后一次修改发生时间(即文件/目录的mtime)在距离find命令执行时间的n+1天以前 会被查找到

find . –mtime –n 文件/目录最后一次修改发生时间(即文件/目录的mtime)在距离find命令执行时间的n天以内 会被查找到

atime、ctime 选项也是一样的

这里有个图可以更方便的看出区别  @see https://blog.csdn.net/db_murphy/article/details/107053545

其他使用栗子

find . -path './Library' -prune -o -type f -name '*.txt' -print0 | xargs -0 grep '密码'

查询除了 ./Library 目录下的  .txt文件  中  带有密码的  文件

posted @ 2022-12-09 21:56  halu126  阅读(498)  评论(0编辑  收藏  举报