栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 系统运维 > 运维 > Linux

Linux磁盘内存满了 清理内存 删除文件 清除垃圾 df Linux垃圾文件 英文版 Linux系统垃圾 删除特定文件

Linux 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Linux磁盘内存满了 清理内存 删除文件 清除垃圾 df Linux垃圾文件 英文版 Linux系统垃圾 删除特定文件

https://www.techrepublic.com/article/how-to-free-disk-space-on-linux-systems/

Checking free space
1.df

 df -Th /

This is the most basic command of all; df can display free disk space

This shows you the raw statistics regarding disk usage in bytes, but
to make it more “human readable” run df with the -h switch:

3.df -Th

When figuring out disk space issues it can be important to know what
type of file system you’re dealing with to plan accordingly. For
instance, the ext4 file system in Linux has a maximum file system size
of 16 Tb and can support files up to 2 Tb.

The “df -Th” command will display the same output as in the previous
command but also includes file system types

Now that I’ve established how we can determine free space, how can we actually figure out what’s using space?

4.du -sh *

The du command is short for “disk usage.” In its most basic usage, if
you simply run “du” at the root volume, it will show you the size of
all the directories on your system. However, this may produce a glut
of information difficult to parse.

Adding the switches -sh (“s” means “summarize” and again, “h” means ”
human readable format”) to run du – sh * at the root volume will
produce a cleaner output.

You can easily see which directories contain the largest amount of data; the usr directory holds 15 Gb, tools has 7.1 Gb and var has 3.9 Gb. Let’s focus on the /var directory.

I COULD go into /var and rerun du – sh * to take a look at the size of
the directories underneath, but since there are lots of subdirectories
of varying sizes this may not be helpful since I’d have to rerun the
same command numerous times. Instead, I can run this:

5.du -a /var | sort -nr | head -n 10

Let’s parse this command string.

du is the disk usage command, of course.

-a is the “all” switch to show all items.

/var tells du to scan the /var directory.

The pipe (“|”) character pipes the results to the sort command.

sort -nr will display the largest directories at the top of the list.

The pipe (“|”) character pipes the results to the head command.

head -n 10 will limit the number of results to the top ten largest
directories.

When I run this on my system I get the following output:

This shows me that the largest items are the /var directory itself
(obviously) followed by /var/cache and /var/cache/yum.

Those four directories at the bottom of the list represent patch sets
rolled out every couple of weeks. It’s safe for me to delete the prior
directories, so the command rm -rf /var/cache/yum/rhel6-auto-2016*
could free up over 1 Gb in space from those old patch set directories.

Note: to run this against the entire file system, just change:

du -a /var | sort -nr | head -n 10
to
du -a / | sort -nr | head -n 10

It’s possible to get even more technical using what’s known as a regex to filter the results more explicitly. Let’s take a closer look at the entire file system.

6.du -xh / |grep '^S*[0-9.]+G'|sort -rn

Looks very complicated but it’s easy to break down:

du is the disk usage command

-x tells du to only check this file system (makes the command run faster) and the h after it is to present results in human readable
format.

The pipe (“|”) character pipes the results to the grep command.

grep searches the results using the ‘^s*[0-9.]+G’ regex – [0-9.]
will list all directories and subdirectories beginning with (^S)
non-whitespace characters (such as a tab, carriage return, etc.) – it
must show alpha characters only (no numeric characters) and +G will
display directories 1 Gb in size or larger.

The pipe (“|”) character pipes the results to the sort command.

sort -rn will display the largest directories at the top of the list.

When I run this on my system I get the following output:

This lets me know I might free up some space under my home directory, since I’m using 2.1 Gb.

Cleaning up directories is one way to reclaim space, but it can be tedious navigating about looking for any big files you might come across. The “find” command can work wonders in pinpointing exactly which files are taking up space.

  1. find / -printf ‘%s %pn’| sort -nr | head -10
find / -printf '%s %pn'| sort -nr | head -10
find . -type f -printf '%TY-%Tm-%Td %TT %pn' | sort -nr | head -10

The above is one of my favorite commands since it will show me the ten
biggest files on my system. Like before, let’s break it down.

find is the find command to search for files, obviously.

/ is the root volume

-printf ‘%s %pn’ will nicely format the results by showing file size in bytes (%s) and the file name (%p).

The pipe (“|”) character pipes the results to the sort command.

sort -nr will display the largest directories at the top of the list

The pipe (“|”) character pipes the results to the head command.

head -n 10 will limit the number of results to the top ten largest
directories.

Running this on my system will yield the following:

I don’t need anything in the old “DWI_Bundles” directory, so these are juicy targets for deletion.

My last example gets even more granular:

8.find / -xdev -type f -size +100M -exec ls -la {} ; | sort -nk 5

find is the find command to search for files, obviously.

/ is the root volume.

-xdev – only search this file system.

-type f – look for files.

-size +100M – only show files over 100 Mb in size.

-exec ls -la {} ; – runs the ls command to show all files with the long listing format.

The pipe (“|”) character pipes the results to the sort command.

sort -nk 5 will display the largest files at the bottom of the list.

When I run this on my system it produces the following results:

Hey, what’s that setupSkybotSchedulerAgent.exe Windows executable doing on my Linux file system? That’s 1.3 Gb I can remove right there!

Deciding what to clean up

Finding what’s taking up space on your file system is the easy part.
Figuring out what you can get rid of is trickier. I found some simple
examples above such as deleting old patch set directories and a
Windows executable. However, blindly deleting files is obviously an
extremely risky and dangerous procedure. If you found large database
files and removed them you could produce catastrophic consequences.

All file systems and their contents will vary, so you may find .log
(log files which are relatively harmless to remove), .rpm files (Red
Hat package files), .tar or .tar.gz files (file archives), .iso files
(disc images) documents, multimedia files, and more. Check file
ownerships and consult with the individuals responsible for them
before deleting anything – and ensure you have a valid backup in place
in case you need to undo your efforts!

Conclusion

These are only a few of the many powerful commands (and accompanying command switches) you can run in Linux. I advise you to experiment, read the man pages for df, du and find, and explore similar or even more robust options.

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/946014.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号