How to Find Files Based on File Size under Linux System Detailed Command Introduction

Detailed introduction of how to use the find command to find files based on file size under Linux system

In our daily work, we may encounter the situation where the size of the search file is abnormal. Here we need to skillfully use the command to find the file according to the file size.

du command - file size query:

1. View the size of the current directory

du -sh

2. Check the size of all files in the current directory

du -sh *

3. All files in the current directory are sorted by size

du -sh * | sort -n #升序
du -sh * | sort -r #降序

4. View the largest/smallest files in the current directory

du -sh * | sort -nr | head -10 #查看最大的10个文件
du -sh * | sort -nr | tail -10 #查看最小的10个文件

find command - file size query

-size parameter introduction:

	  b    for 512-byte blocks (this is the default if no suffix is used)

	  c    for bytes

	  w    for two-byte words

	  k    for Kilobytes (units of 1024 bytes)

	  M    for Megabytes (units of 1048576 bytes)

	  G    for Gigabytes (units of 1073741824 bytes)

Note: The default unit is b, and it represents 512 bytes, so 2 means 1K, and 1M means 2048. If you don’t want to convert by yourself, you can use other units, such as c, K, M, etc.

1. Find files with a file size of 4096 (4k) in the current directory

find ./ -size 8
find ./ -size 4096c
find ./ -size 4K

2. Find files greater than or less than a certain value

查找大于2M的文件,+ 表示大于
find ./ -size +2M

查找小于2m的文件,- 表示小于
find ./ -size -2M

3. Search by file size range

find . -type f -size +100k -size-400k
查找大于 100k 且小于 400k 的文件

-type f means only find files, filter out folders, block files, etc.

Display the detected files in a detailed list

find . -type f -size +100k -size-400k | xargs ls –l

4. Some other uses

1、以查找 /home下最近两天修改过的文件
find /home -type f -mtime -2

2、近3天创建的文件log
find / -mtime -3 -name '*.log'

3、查找3天之前的文件;
find / -mtime +3 -name '*.log'

find other usages

find / -name httpd.conf   (在根目录查找) 
find / -amin -10   查找在系统中最后10分钟访问的文件
find / -atime -2    查找在系统中最后48小时访问的文件
find / -empty      查找在系统中为空的文件或者文件夹
find / -group cat    查找在系统中属于 groupcat的文件
find / -mmin -5     查找在系统中最后5分钟里修改过的文件
find / -mtime -1    查找在系统中最后24小时里修改过的文件
find / -nouser     查找在系统中属于作废用户的文件
find / -user fred   查找在系统中属于FRED这个用户的文件

Guess you like

Origin blog.csdn.net/qq_42716761/article/details/128573559