如何在Linux中查找和列出大文件和目录?

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
Find and Sort Large Files and Directories with du Command
使用du命令查找和排序大文件和目录

仅打印目录摘要(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
Print Only Directory Summaries
仅打印目录摘要

跳过不同文件系统中的文件和目录(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
Find and Sort Large Files with find Command
使用find命令查找和排序大文件

创建别名和命令以查找大文件和目录(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
Create Alias and Command For Finding Large Files and Directories
创建别名和命令以查找大文件和目录
LEARN MORE  How To Open Windows File Explorer In Different Ways like Shortcut, Command-line, GUI?
了解更多信息如何以快捷方式,命令行,GUI等不同方式打开Windows File Explorer?

翻译自: https://www.poftut.com/find-and-list-large-files-and-directories-in-linux/

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值