LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-03-2011, 03:10 AM   #1
grob115
Member
 
Registered: Oct 2005
Posts: 542

Rep: Reputation: 32
Find largest file within a directory


Am interested in writing a script that can return to me say the top 5 files in terms of size under a specific directory. The difficulty is this directory may have any number of sub-directories under it, and each of these directories may or may not have files in them. Any idea how this can be done?
 
Click here to see the post LQ members have rated as the most helpful post in this thread.
Old 09-03-2011, 03:29 AM   #2
yooy
Senior Member
 
Registered: Dec 2009
Posts: 1,387

Rep: Reputation: 174Reputation: 174
you will need to go throuht all files with "find" and save biggest files as list that gets updated when bigger file is found
 
Old 09-03-2011, 03:41 AM   #3
Nominal Animal
Senior Member
 
Registered: Dec 2010
Location: Finland
Distribution: Xubuntu, CentOS, LFS
Posts: 1,723
Blog Entries: 3

Rep: Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948Reputation: 948
Use find to list all files with file size in bytes first. Then sort the output by the file size. Finally, use sed to output the N largest files (5, below), removing the file size in bytes.
Code:
find dir(s)... -type f -printf '%s %p\n' | sort -rg | sed -ne '1,5 s/^[^ ]*//p'
 
2 members found this post helpful.
Old 09-03-2011, 11:21 PM   #4
grob115
Member
 
Registered: Oct 2005
Posts: 542

Original Poster
Rep: Reputation: 32
Nominal Animal, thanks.
 
Old 09-04-2011, 12:42 AM   #5
ArthurSittler
Member
 
Registered: Jul 2008
Distribution: Slackware
Posts: 124

Rep: Reputation: 31
I find ls with appropriate flags works well. Pipe the output from ls through sort, as above, and head with the number of largest files you want. Then I used awk to invoke find with system() to display the complete relative path.
I suppose starting out with find is the more direct approach.
 
Old 09-04-2011, 02:35 AM   #6
kurumi
Member
 
Registered: Apr 2010
Posts: 228

Rep: Reputation: 53
Quote:
Originally Posted by ArthurSittler View Post
I find ls with appropriate flags works well. Pipe the output from ls through sort, as above, and head with the number of largest files you want. Then I used awk to invoke find with system() to display the complete relative path.
I suppose starting out with find is the more direct approach.
This is not the correct approach. Using ls to list files (with ls options) spells trouble if files contains white spaces. And why would you invoke find within awk? You are making alot of overheads doing things like this.
 
1 members found this post helpful.
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Command to find largest file. paragkalra Linux - Software 2 11-17-2009 04:36 AM
a2ps: cannot find file `c0419bt_.afm': No such file or directory rg3 Slackware 5 08-02-2009 03:11 PM
C++ find array index for largest number. joshp Programming 7 03-15-2009 10:56 AM
how do i find the largest file on machine amit_joshi Linux - Newbie 4 10-08-2007 07:09 PM
Find 100 largest files in folder tree tyreth Linux - General 2 02-07-2006 08:18 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 02:29 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration