Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place! |
Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
|
 |
12-21-2007, 08:45 AM
|
#1
|
LQ Newbie
Registered: Dec 2007
Posts: 8
Rep:
|
How do i find the biggest file in my directory?
Dear Folks,
My requirement is like this, in my folder i have lot of files
i have to find the biggest file. my folder contains lots of sub folders also......
i think i gave a clear picture about my requirment...
if any one knows, do send me a reply...
Thanks and Regards,
V.Anand,
Bangalore.
|
|
|
12-21-2007, 08:48 AM
|
#2
|
LQ Addict
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464
Rep: 
|
Read the man page for ls and find the options you need to use.
|
|
|
12-21-2007, 08:59 AM
|
#3
|
LQ Guru
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019
Rep: 
|
And the man pages for sort and head, too, while you're at it.
|
|
|
12-21-2007, 09:12 AM
|
#4
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
I'd opt for man pages of find, xargs, ls, head.
|
|
|
12-21-2007, 09:34 AM
|
#5
|
Member
Registered: Nov 2003
Location: London, UK
Distribution: openSUSE, Ubuntu
Posts: 358
Rep:
|
Quote:
Originally Posted by anandv_1234
Dear Folks,
My requirement is like this, in my folder i have lot of files
i have to find the biggest file. my folder contains lots of sub folders also......
i think i gave a clear picture about my requirment...
|
You could have given us some info about which Linux distro you are using, and also how big is your hard drive, etc.
If you are using the KDE desktop, then open the Start menu and click on Home to open the File Manager. Then click on the Size tab at the top of the second column twice, and a little black arrow should appear pointing up. This will sort directories and files with the biggest at the top. Now you just have to go through all the directories and sub-directories by hand.
A faster way would be to use Filelight - http://www.methylblue.com/filelight/
|
|
|
12-21-2007, 09:41 AM
|
#6
|
Senior Member
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530
Rep:
|
You could view the directory using a GUI file manager like konqueror under KDE, nautilus under Gnome, Thunar under XFCE, select detailed listing and then sort on the size and look at the top/bottom of the list.
The same approach works for the ls program in the terminal - sort and take the top or bottom of the list.
From the ls manual page:
What it doesn't say explicitly is that the list will be in descending order, so you can take the top of the list to see the largest (I just found it by trying it and seeing what order it was using).
This will just print the name of the largest file:
Or if you want to see the details, add an l (lower case L):
|
|
|
12-21-2007, 10:37 AM
|
#7
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Code:
find ./ -type f -printf "%s %p\n" | awk 'BEGIN {max=0} {
if ($1>max) {max=$1; maxfile=$2 }
}
END {print max maxfile}'
The find program will print the size of the largest file in the current directory and its subdirectories. The awk program will print out just the largest file.
Code:
ls -lR * | tr -s ' ' | sed -n '/^-/p' | cut -d' ' -f5,9 | sort -nr | head
This oneliner A) lists all of the files B) truncates the extra spaces B) filters out directories C) performs a reverse numeric sort D) prints out the first few lines.
If you have a large amount of files, "ls -lR" might fail. If that is the case, use the find command.
Last edited by jschiwal; 12-21-2007 at 11:43 AM.
|
|
|
12-21-2007, 10:58 AM
|
#8
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Code:
du -ab /path/*|sort -rn|head -1
Last edited by ghostdog74; 12-21-2007 at 11:01 AM.
|
|
|
12-21-2007, 11:12 AM
|
#9
|
Senior Member
Registered: Aug 2006
Posts: 2,697
|
Quote:
Originally Posted by matthewg42
This will just print the name of the largest file:
|
if the requirement is to find the largest file including subfolders, then have to include -R and do some extra processing to get rid of extraneous lines.
Quote:
Or if you want to see the details, add an l (lower case L):
|
if the -l is added, then head -1 will give the "total <size>" string..got to get the second line instead.
Code:
# ls -lS| sed -n '2p'
|
|
|
12-21-2007, 11:46 AM
|
#10
|
LQ Guru
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733
|
Quote:
Originally Posted by ghostdog74
if the requirement is to find the largest file including subfolders, then have to include -R and do some extra processing to get rid of extraneous lines.
if the -l is added, then head -1 will give the "total <size>" string..got to get the second line instead.
Code:
# ls -lS| sed -n '2p'
|
Including the -R argument will cause each folder to list the largest files first, but won't list the largest file of all directories on the top.
Code:
du -ab /path/*|sort -rn|head -1
This has promise but will list a directories as well.
Last edited by jschiwal; 12-21-2007 at 11:53 AM.
|
|
|
12-21-2007, 12:02 PM
|
#11
|
Senior Member
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847
Rep: 
|
Quote:
Originally Posted by jschiwal
Code:
du -ab /path/*|sort -rn|head -1
This has promise but will list a directories as well.
|
Code:
find ./ -type f -exec du -ab {} \; | sort -rn | head -1
|
|
|
12-21-2007, 12:58 PM
|
#12
|
LQ Guru
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509
|
This should be a little faster
Code:
ls -asR | sort -n | tail -1
and to retrieve the full path
Code:
find . -name name_of_the_biggest_file
|
|
|
All times are GMT -5. The time now is 01:31 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|