Linux Find Large Files Example

Having our disk tidy and free of garbage occupies space is something we all want to do, but many times we don’t pay attention at large files that are taking up a considerable amount of space that could be useful for something else.

In this example, we will see how to look for large files, just to be aware of their existence, or for deleting or moving them somewhere else.

For this example, Linux Mint 18 has been used.

1. Introduction

There’s more than a way to find by its size. And, of course, a “large file” is only a subjective term to refer to files that each one considers large depending on its size; there’s no a way of directly saying “look for large files”. So, we will look for files specifying a size.

For this, we will use the powerful find command.

2. Understanding the find command

The syntax of find, for looking for files by their size, is the following:

1find <path> [-type <file-type>] -size +<size><unit>

Let’s explain it briefly:

  • find looks for files recursively, so, when we specify a path, it will also look in the children directories.
  • Optionally, we can specify the file type, if we only want to find certain ones. The file types are the followings:
    • d: directory
    • f: regular file
    • l: symbolic link
    • b: buffered block
    • c: unbuffered character
    • p: named pipe
    • s: socket
  • Finally, the size, composed by the size itself, and the unit we are using, that can be one of these:
    • b: 512-byte blocks (the one used by default, if no other unit is specified)
    • c: bytes
    • k: kilobytes
    • M: megabytes
    • G: gigabytes

2.1. Examples

Let’s see some examples, if there’s any doubt left:

Find files in home directory bigger than 2 GB:

1find /home -size +2G

Find directories in the whole system bigger than 10 GB:

1sudo find / -type d -size +10G

Note that, in this case, we have used root privileges with sudo, to look also inside directories the current user does not have access to.

2.2. Finding by file extension

If we want to be more precise, we can also look for files by the extension. For that, we can use the -name option:

1find [...] -name *.<extension>

Here are some examples:

Find pdf files in home directory bigger than 20 MB:

1sudo find /home -size +20M -name *.pdf

Find mkv files in the whole system bigger than 5 GB:

1sudo find /home -size +5G -name *.mkv

3. Executing actions

Until now, we have seen how to just find large files. Usually, if we want to find large files, is with a certain purpose (usually, delete them, or move them somewhere else).

So, finding the files, just give as the path to them. This is not very useful if we want to perform some action, like previously mentioned.

find command allows to execute commands for every found file. The syntax is the following:

1find [...] -exec <command> {} \;

Where the command is the command itself, and the curly braces {} are where the found file will be placed, for the command execution. Finally, \; indicates the command end.

Let’s see some examples:

Remove all log files bigger than 20 MB:

1sudo find / -name *.log -size +20M -exec rm {} \;

Move all files bigger than 30 GB to /tmp:

1sudo find / -size +30G -exec mv {} /tmp \;

Be careful, if you execute those commands, the files will be removed/moved!

Of course, we can execute any other action, such as showing file information:

1sudo find / -size +10G -exec ls -l {} \;

4. Finding the top larges files

The remaining interesting action we can do with find is to find the largest files in the system.

In the previous examples, we have been looking for files specifying a size. But we may have no idea about the sizes we want to look for, and we just want to find the largest ones.

For that, we can use find executing the following command:

1find [...] -exec ls -s {} \; | sort -n -r | head -n

Where n is the number of files to look for.

So, for example, if we execute:

1sudo find / -type f -exec ls -s {} \; | sort -n -r | head -10

Will look for the biggest 10 files in the disk. This is a sample output of the command:

0126460 /tmp/lu24928inllnr.tmp/lu24928inllvi.tmp
025984 /tmp/lu24928inllnr.tmp/lu24928inllos.tmp
035984 /tmp/lu24928inllnr.tmp/lu24928inllom.tmp
045984 /tmp/lu24928inllnr.tmp/lu24928inllo8.tmp
055984 /tmp/lu24928inllnr.tmp/lu24928inllo4.tmp
065864 /tmp/lu24928inllnr.tmp/lu24928inllox.tmp
075864 /tmp/lu24928inllnr.tmp/lu24928inllor.tmp
085864 /tmp/lu24928inllnr.tmp/lu24928inllod.tmp
095864 /tmp/lu24928inllnr.tmp/lu24928inllo7.tmp
105784 /tmp/lu24928inllnr.tmp/lu24928inllp2.tmp

5. Conclusion

In this example we have seen how to look for files in Linux by their size, with the aim of finding the largest files. We have seen that we can specify any type of file and size, being also able to execute a command for each coincidence, which can help us cleaning up our disks.

Finally, we have shown how to look for the top largest files in the disk, without the need of specifying a size.

转载于:https://my.oschina.net/topeagle/blog/849534

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值