Linux 如何获取所有超过一定大小的文件并删除它们

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5057041/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 02:56:02  来源:igfitidea点击:

How to get all the files exceeding certain size and deleting them

linuxdelete-file

提问by Ran

  1. I am looking for a linux command to get all the files exceeding a certain size from the current directory and its sub-directories.

  2. Whats the easiest way to delete all these files?

  1. 我正在寻找一个 linux 命令来从当前目录及其子目录中获取超过一定大小的所有文件。

  2. 删除所有这些文件的最简单方法是什么?

采纳答案by Erik

Similar to the exec rm answer, but doesn't need a process for each found file:

类似于 exec rm 答案,但不需要每个找到的文件的进程:

find . -size +100k -delete

回答by Tomasz Nurkiewicz

One-liner:

单线:

find . -size +100k -exec rm {} \;

The first part (find . -size +100k) looks for all the files starting from current directory (.) exceeding (+) 100 kBytes (100k).

第一部分 ( find . -size +100k) 查找从当前目录 ( .) 开始超过 ( +) 100 KB ( 100k) 的所有文件。

The second part (-exec rm {} \;) invoked given command on every found file. {}is a placeholder for current file name, including path. \;just marks end of the command.

第二部分 ( -exec rm {} \;) 在每个找到的文件上调用给定的命令。{}是当前文件名的占位符,包括路径。\;只是标记命令的结束。

Remember to always check whether your filtering criteria are proper by running raw find:

请记住始终通过运行 raw 来检查您的过滤条件是否正确find

find . -size +100k

Or, you might even make a backup copy before deleting:

或者,您甚至可以在删除之前制作一个备份副本:

find . -size +100k -exec cp --parents {} ~/backup \;

回答by Brian Agnew

In zsh:

zsh 中

ls -l *(Lk+100)   # list file size greater than 100kb

so:

所以:

rm *(Lk+100)

More zsh goodness here.

更多 zsh 优点在这里

回答by Uri Goren

python is installed on all unix based OS, so why not use it instead of bash ?

python 安装在所有基于 unix 的操作系统上,那么为什么不使用它而不是 bash 呢?

I always find python more readable than awkand sedmagic.

我总是发现蟒蛇比更具可读性awksed魔法。

This is the python code I would have written:

这是我会写的python代码:

import os
Kb = 1024 # Kilo byte is 1024 bytes
Mb = kb*kb
Gb = kb*kb*kb
for f in os.listdir("."):
    if os.stat(f).st_size>100*Kb:
        os.remove(f)

And this is the one-liner version with python -c

这是单线版本 python -c

python -c "import os; [os.remove(f) for f  in os.listdir('.') if os.stat(f).st_size>100*1024]"

And if you want to apply the search recursively, see this

如果你想递归地应用搜索,请看这个