LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
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

Reply
 
Search this Thread
Old 12-21-2007, 09:45 AM   #1
anandv_1234
LQ Newbie
 
Registered: Dec 2007
Posts: 8

Rep: Reputation: 0
Question 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.
 
Old 12-21-2007, 09:48 AM   #2
Nylex
LQ Addict
 
Registered: Jul 2003
Location: London, UK
Distribution: Slackware
Posts: 7,464

Rep: Reputation: Disabled
Read the man page for ls and find the options you need to use.
 
Old 12-21-2007, 09:59 AM   #3
jay73
Guru
 
Registered: Nov 2006
Location: Belgium
Distribution: Ubuntu 11.04, Debian testing
Posts: 5,019

Rep: Reputation: 130Reputation: 130
And the man pages for sort and head, too, while you're at it.
 
Old 12-21-2007, 10:12 AM   #4
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I'd opt for man pages of find, xargs, ls, head.
 
Old 12-21-2007, 10:34 AM   #5
sadiqdm
Member
 
Registered: Nov 2003
Location: London, UK
Distribution: openSUSE, Ubuntu
Posts: 357

Rep: Reputation: 35
Quote:
Originally Posted by anandv_1234 View Post
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/
 
Old 12-21-2007, 10:41 AM   #6
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 63
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:
Quote:
-S sort by file size
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:
Code:
ls -S |head -1
Or if you want to see the details, add an l (lower case L):
Code:
ls -lS |head -1
 
Old 12-21-2007, 11:37 AM   #7
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
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 12:43 PM.
 
Old 12-21-2007, 11:58 AM   #8
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Code:
du -ab /path/*|sort -rn|head -1

Last edited by ghostdog74; 12-21-2007 at 12:01 PM.
 
Old 12-21-2007, 12:12 PM   #9
ghostdog74
Senior Member
 
Registered: Aug 2006
Posts: 2,697
Blog Entries: 5

Rep: Reputation: 241Reputation: 241Reputation: 241
Quote:
Originally Posted by matthewg42 View Post
This will just print the name of the largest file:
Code:
ls -S |head -1
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):
Code:
ls -lS |head -1
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'
 
Old 12-21-2007, 12:46 PM   #10
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655Reputation: 655
Quote:
Originally Posted by ghostdog74 View Post
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 12:53 PM.
 
Old 12-21-2007, 01:02 PM   #11
pwc101
Senior Member
 
Registered: Oct 2005
Location: UK
Distribution: Slackware
Posts: 1,847

Rep: Reputation: 128Reputation: 128
Quote:
Originally Posted by jschiwal View Post
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
 
Old 12-21-2007, 01:58 PM   #12
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,509

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
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
 
  


Reply

Tags
ls, size, sort


Thread Tools Search this Thread
Search this Thread:

Advanced Search

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
find: /proc/xxxx/task: No such file or directory King4lex Linux - Software 5 04-14-2007 08:42 AM
find pattern in any file in directory sancho1980 Linux - Newbie 4 12-19-2006 09:14 AM
Biggest File Within Directory matsko Linux - Software 2 04-11-2006 06:30 AM
Command to find similar file names in a directory Ottoguy Linux - Newbie 4 02-02-2006 06:42 AM
Looking for biggest files into a directory jobano Linux - Software 3 10-04-2004 02:48 PM


All times are GMT -5. The time now is 05:55 AM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration