One of the important job for System Administrators is finding and deleting large and unneeded files from Linux operating system. In this tutorial we will look how to find, sort and delete large and trash files from Linux distributions like Ubuntu, Debian, Mint, Fedora, CentOS and RHEL etc.
对于系统管理员来说,重要的工作之一是从Linux操作系统中查找并删除不需要的大文件。 在本教程中,我们将研究如何从Linux发行版(如Ubuntu,Debian,Mint,Fedora,CentOS和RHEL等)中查找,排序和删除大文件和垃圾文件。
大文件目录 (Large Files Directories)
In Linux distributions there are some directories and paths where files generally holds a lot of disk space. These space are generally used by temporary, cache, log or personal files. Here list of these directories.
在Linux发行版中,有一些目录和路径,其中文件通常包含大量磁盘空间。 这些空间通常由临时文件,缓存文件,日志文件或个人文件使用。 这些目录在这里。
- /tmp / tmp
- /var / var
- /var/log / var / log
- /home /家
使用du命令查找和排序大文件和目录(Find and Sort Large Files and Directories with du Command)
There are alternative ways to find and list directories and files. We will look du
command which is short for disk usage. We will combine commands sort
to sort lines according to size and head
to cut first specified number of lines. In this example we will list files and directories located at /home/ismail
. File and directory sizes are listed in the first columns and we sort with sort
command. We select first 20
line with head
command.
有其他方法可以查找和列出目录和文件。 我们将查找du
命令,它是磁盘使用情况的缩写。 我们将结合命令sort
来根据大小和行head
对行进行排序,以剪切第一个指定的行数。 在此示例中,我们将列出位于/home/ismail
文件和目录。 文件和目录的大小在第一列中列出,我们使用sort
命令进行sort
。 我们使用head
命令选择前20
行。
$ sudo du -a /home/ismail | sort -n -r | head -n 20
![Find and Sort Large Files and Directories with du Command](https://i-blog.csdnimg.cn/blog_migrate/c361b99d70396ff9fef1112beee83b7f.png)
仅打印目录摘要(Print Only Directory Summaries)
In previous example we will listed a lot of path. This may be consuming. We can list only given path and do not recursively its sub directories with -s
option.
在前面的示例中,我们将列出很多路径。 这可能会很消耗。 我们只能使用-s
选项列出给定路径,而不能递归列出其子目录。
$ sudo du -s /home/ismail/* | sort -n -r | head -n 20
![Print Only Directory Summaries](https://i-blog.csdnimg.cn/blog_migrate/f1fd38369b1e8a7a06bf8f4f2560b33c.png)
跳过不同文件系统中的文件和目录(Skip Files and Directories in Different File Systems)
Another useful option provided by du
command is skipping other files systems. As we know in Linux it can be mounted different files systems in to sub directories. We can prevent checking files sizes in this file systems with -x
like below.
du
命令提供的另一个有用的选项是跳过其他文件系统。 如我们所知,在Linux中,可以将不同的文件系统挂载到子目录中。 我们可以使用-x
阻止如下所示检查此文件系统中的文件大小。
$ sudo du -s -x /mnt
使用find命令查找和排序大文件 (Find and Sort Large Files with find Command)
find
is very useful command used for different purposes. The extensible of find
command provides us to list even delete big files. We print the file and sizes with the -printf
option of the find
command.
find
是用于不同目的的非常有用的命令。 find
命令的可扩展性使我们可以列出甚至删除大文件。 我们使用find
命令的-printf
选项打印文件和大小。
$ find /home/ismail -printf '%s %p\n'| sort -nr | head -5
![Find and Sort Large Files with find Command](https://i-blog.csdnimg.cn/blog_migrate/ef777c80330ab55f79fe9e032b239981.png)
创建别名和命令以查找大文件和目录(Create Alias and Command For Finding Large Files and Directories)
As a daily job writing these commands again and agina will be very trivial task. We can create some alias or script from these commands. In this example we will create an alias named list_big_files
alias and add it to the .bashrc
of the current user.
作为日常工作,再次编写这些命令和agina将是非常琐碎的任务。 我们可以从这些命令创建一些别名或脚本。 在此示例中,我们将创建一个名为list_big_files
alias的别名,并将其添加到当前用户的.bashrc
中。
alias list_big_files="sudo du -s /home/ismail/* | sort -n -r | head -n 20"
and we can call list_big_files
like below but in order to make it effective we should start new shell to load our new alias.
并且我们可以像下面那样调用list_big_files
,但是为了使其生效,我们应该启动新的shell来加载新的别名。
$ list_big_files
![Create Alias and Command For Finding Large Files and Directories](https://i-blog.csdnimg.cn/blog_migrate/bdcd733118fc6ea30fc767f397911f4f.png)
翻译自: https://www.poftut.com/find-and-list-large-files-and-directories-in-linux/