列出当前目录和所有子目录中特定大小的文件

时间:2021-09-18 07:08:27

How can I display all files greater than 10k bytes in my current directory and it's subdirectories.

如何在当前目录及其子目录中显示大于10k字节的所有文件。

Tried ls -size +10k but that didn't work.

尝试过ls -size +10k,但没有成功。

5 个解决方案

#1


63  

find . -size +10k -exec ls -lh {} \+

找到。-size +10k -exec ls -lh {} \+

the first part of this is identical to @sputnicks answer, and sucesffully finds all files in the directory over 10k (don't confuse k with K), my addition, the second part then executes ls -lh or ls that lists(-l) the files by human readable size(-h). negate the h if you prefer. of course the {} is the file itself, and the \+ is simply an alternative to \;

第一部分与@sputnicks的答案是相同的,sucesffully发现目录中的所有文件都超过10k(不要混淆k和k),我的加法,第二部分执行ls -lh或ls,这些文件由人类可读大小(-h)列出。如果你愿意,可以否定h。当然,{}是文件本身,而\+只是\的替代;

which in practice \; would repeat or:

这在实践中\;会重复或:

ls -l found.file; ls -l found.file.2; ls -l found.file.3

ls - l found.file;ls - l found.file.2;ls - l found.file.3

where \+ display it as one statement or:

将其显示为一个语句或:

ls -l found.file found.file.2 found.file.3

ls - l。文件found.file。2 found.file.3

more on \; vs + with find

更多关于\;vs +与发现

Additionaly, you may want the listing ordered by size. Which is relatively easy to accomplish. I would at the -s option to ls, so ls -ls and then pipe it to sort -n to sort numerically

此外,您可能需要按大小排序清单。这相对容易实现。我在ls的-s选项,所以ls -ls然后通过管道对-n进行数值排序

which would become:

这将成为:

find . -size +10k -exec ls -ls {} \+ | sort -n

找到。-size +10k -exec ls -ls {} + | sort -n

or in reverse order add an -r :

或以相反的顺序添加-r:

find . -size +10k -exec ls -ls {} \+ | sort -nr

找到。-size +10k -exec ls -ls {} + | sort -nr

finally, your title says find biggest file in directory. You can do that by then piping the code to tail

最后,你的标题说在目录中找到最大的文件。您可以通过管道将代码连接到尾部来实现这一点

find . -size +10k -exec ls -ls {} \+ | sort -n | tail -1 would find you the largest file in the directory and its sub directories.

找到。-size +10k -exec ls -ls {} + | sort -n | tail -1将找到目录及其子目录中最大的文件。

note you could also sort files by size by using -S, and negate the need for sort. but to find the largest file you would need to use head so

注意,您还可以使用-S按大小对文件进行排序,并消除排序的需要。但是要找到最大的文件,你需要使用head so

find . -size +10k -exec ls -lS {} \+ | head -1

找到。-size +10k -exec ls -lS {} + | head -1

the benefit of doing it with -S and not sort is one, you don't have to type sort -n and two you can also use -h the human readable size option. which is one of my favorite to use, but is not available with older versisions of ls, for example we have an old centOs 4 server at work that doesn't have -h

使用-S而不是排序的好处是,你不需要输入-n和2你也可以使用-h人类可读大小选项。这是我最喜欢使用的一个,但是在较老版本的ls中不能使用,例如,我们有一个旧的centOs 4服务器,它没有-h

#2


10  

Try doing this:

试着这样做:

find . -size +10k -ls

And if you want to use the binary ls :

如果你想用二进制的ls

find . -size +10k -exec ls -l {} \;

#3


6  

I realize the assignment is likely long over. For anyone else:

我意识到这个任务很可能已经结束很久了。对于其他人:

You are overcomplicating.

你是过于复杂。

find . -size +10k

#4


3  

I'll add to @matchew answer (not enough karma points to comment):

我将添加@matchew的答案(没有足够的业力点来评论):

find . -size +10k -type f -maxdepth 1 -exec ls -lh {} \; > myLogFile.txt

-type f :specify regular file type

-类型f:指定常规文件类型

-maxdepth 1 :make sure it only find files in the current directory

-maxdepth 1:确保只找到当前目录中的文件

#5


1  

You may use ls like that:

你可以这样使用ls:

ls -lR | egrep -v '^d' | awk '$5>10240{print}'

Explanation:

解释:

ls -lR         # list recursivly
egrep -v '^d'  # only print lines which do not start with a 'd'. (files)

only print lines where the fifth column (size) is greater that 10240 bytes:

只打印第5列(大小)大于10240字节的行:

awk '$5>10240{print}'

#1


63  

find . -size +10k -exec ls -lh {} \+

找到。-size +10k -exec ls -lh {} \+

the first part of this is identical to @sputnicks answer, and sucesffully finds all files in the directory over 10k (don't confuse k with K), my addition, the second part then executes ls -lh or ls that lists(-l) the files by human readable size(-h). negate the h if you prefer. of course the {} is the file itself, and the \+ is simply an alternative to \;

第一部分与@sputnicks的答案是相同的,sucesffully发现目录中的所有文件都超过10k(不要混淆k和k),我的加法,第二部分执行ls -lh或ls,这些文件由人类可读大小(-h)列出。如果你愿意,可以否定h。当然,{}是文件本身,而\+只是\的替代;

which in practice \; would repeat or:

这在实践中\;会重复或:

ls -l found.file; ls -l found.file.2; ls -l found.file.3

ls - l found.file;ls - l found.file.2;ls - l found.file.3

where \+ display it as one statement or:

将其显示为一个语句或:

ls -l found.file found.file.2 found.file.3

ls - l。文件found.file。2 found.file.3

more on \; vs + with find

更多关于\;vs +与发现

Additionaly, you may want the listing ordered by size. Which is relatively easy to accomplish. I would at the -s option to ls, so ls -ls and then pipe it to sort -n to sort numerically

此外,您可能需要按大小排序清单。这相对容易实现。我在ls的-s选项,所以ls -ls然后通过管道对-n进行数值排序

which would become:

这将成为:

find . -size +10k -exec ls -ls {} \+ | sort -n

找到。-size +10k -exec ls -ls {} + | sort -n

or in reverse order add an -r :

或以相反的顺序添加-r:

find . -size +10k -exec ls -ls {} \+ | sort -nr

找到。-size +10k -exec ls -ls {} + | sort -nr

finally, your title says find biggest file in directory. You can do that by then piping the code to tail

最后,你的标题说在目录中找到最大的文件。您可以通过管道将代码连接到尾部来实现这一点

find . -size +10k -exec ls -ls {} \+ | sort -n | tail -1 would find you the largest file in the directory and its sub directories.

找到。-size +10k -exec ls -ls {} + | sort -n | tail -1将找到目录及其子目录中最大的文件。

note you could also sort files by size by using -S, and negate the need for sort. but to find the largest file you would need to use head so

注意,您还可以使用-S按大小对文件进行排序,并消除排序的需要。但是要找到最大的文件,你需要使用head so

find . -size +10k -exec ls -lS {} \+ | head -1

找到。-size +10k -exec ls -lS {} + | head -1

the benefit of doing it with -S and not sort is one, you don't have to type sort -n and two you can also use -h the human readable size option. which is one of my favorite to use, but is not available with older versisions of ls, for example we have an old centOs 4 server at work that doesn't have -h

使用-S而不是排序的好处是,你不需要输入-n和2你也可以使用-h人类可读大小选项。这是我最喜欢使用的一个,但是在较老版本的ls中不能使用,例如,我们有一个旧的centOs 4服务器,它没有-h

#2


10  

Try doing this:

试着这样做:

find . -size +10k -ls

And if you want to use the binary ls :

如果你想用二进制的ls

find . -size +10k -exec ls -l {} \;

#3


6  

I realize the assignment is likely long over. For anyone else:

我意识到这个任务很可能已经结束很久了。对于其他人:

You are overcomplicating.

你是过于复杂。

find . -size +10k

#4


3  

I'll add to @matchew answer (not enough karma points to comment):

我将添加@matchew的答案(没有足够的业力点来评论):

find . -size +10k -type f -maxdepth 1 -exec ls -lh {} \; > myLogFile.txt

-type f :specify regular file type

-类型f:指定常规文件类型

-maxdepth 1 :make sure it only find files in the current directory

-maxdepth 1:确保只找到当前目录中的文件

#5


1  

You may use ls like that:

你可以这样使用ls:

ls -lR | egrep -v '^d' | awk '$5>10240{print}'

Explanation:

解释:

ls -lR         # list recursivly
egrep -v '^d'  # only print lines which do not start with a 'd'. (files)

only print lines where the fifth column (size) is greater that 10240 bytes:

只打印第5列(大小)大于10240字节的行:

awk '$5>10240{print}'