Need help with script that finds the 10 largest files
How can I write a script that finds 10 largest files in a filesystem? The script has to display the filenames and sizes in reverse order, largest first. I would appreciate if someone can please help.
Thanks |
Quote:
du -ha | sort -hr | head -n 10 |
You will need following commands: find, du, sort, head
Well, it's not a good idea to offer you a ready-made script. Instead you can create it yourself with help of following cmds: Code:
find / -exec du -sk {} \; 2>/dev/null| gawk '{gsub(/K/,"",$1); print $0}' > /tmp/files.txt |
Quote:
I only can use grep as grep -v ".$" to avoid the . into the files.txt |
@nicksu:
Searching for files in / directory will print absolute path i.e. complete path of files in /tmp/files.txt, which is the only way to search files in whole file system. Please explain your problem little more, what exactly you want to say? Else, following script is running fine: Code:
#!/bin/bash Run the script as root user or use sudo, because you will search for files in / directory. |
Quote:
|
Quote:
#! /bin/bash echo "pleae type in the path in which you want to check the most largest file" read path a=`find $path -type f -exec du -h {} \;|sort -hr|head -n 10|awk '{print $2}'` for i in $a do echo $(ls -hld $i) 2>/dev/null done |
@nicksu:
Once go through this guide. And note that, 1. There's no need to use a for loop, -exec option will do that. 2. Do not use "head -10" with find command, because it will then print first 10 files, not all files. Code:
#! /bin/bash |
Quote:
and for your point 2,I am not so clear.I use the "head -10" to print out the first 10 lines,because I have issued the "sort -hr" which sort the find result and then fetch the first 10 line by "head -10".Why wrong ? |
Please use a descriptive title for your thread excluding words like 'urgent' or 'help'. Using a proper title makes it easier for members to help you. This thread has been reported for title modification. Please do not add replies that address the thread title.
|
Quote:
Second thing, making your script unnecessarily lengthy is not a good idea when your work can be done in a few commands, so for loop is also not needed. However, please refer the guide I mentioned, so your doubts can be cleared. |
I do not like those chain of commands like find, du, grep, sed, awk, head, cut ... (not to speak about additional loops), keep it simple:
Code:
find . -type f -exec du -sb {} \; | perl -e ' my %h; while (<>) { @b = split; $h{$b[1]}=$b[0] } $i=2; for my $k (sort { $h{$b} <=> $h{$a} } keys %h) { print "$k $h{$k}\n"; last if $i++>3; } ' |
amber1,
The commands du and find both go through all files and directories beneath the starting point you give them, and so will cross file system boundaries, unless you tell them not to do so. That is, they will not necessarily be limited to a single file system. If the file system on which you start, has other file systems mounted within it, the result would be the largest file on any of those file systems, not the largest file on the one file system. Also, du tends to attribute all space used by all files and directories under a directory, to the directory itself. So it would show the root of any directory tree as owning all the space used below it, falsely showing the root directory as the largest file. Although the -exec option of the find command is a powerful capability to extend the usefulness of the find command, repeatedly having find execute another command, such as du, when it isn't necessary, can be rather slow. A command sequence such as the following, limits find to a single file system, making use of find's own capabilities to produce its output, only then sorting the result, and grabbing the first ten lines. Code:
find / -xdev -printf "%s %p\n" | sort -rn | head -10 Code:
find / -xdev -type f -printf "%s %p\n" | sort -rn | head -10 |
All times are GMT -5. The time now is 07:59 PM. |