LinuxQuestions.org
Review your favorite Linux distribution.
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 11-14-2011, 02:47 PM   #1
lainey
LQ Newbie
 
Registered: Nov 2011
Posts: 5

Rep: Reputation: Disabled
Question Store the size of largest file from a file/directory listing into variable SIZE


How do i store the size of the largest file from a file or directory into a variable called SIZE?
 
Old 11-14-2011, 03:07 PM   #2
rtmistler
Moderator
 
Registered: Mar 2011
Location: Sutton, MA. USA
Distribution: MINT Debian, Angstrom, SUSE, Ubuntu
Posts: 4,484
Blog Entries: 10

Rep: Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643Reputation: 1643
Try using "ls -1S" in your script.

That's #1, capital S.

That gives you single column, sorted by size, largest file is first. Pipe that somewhere and then post-process the output to give you that first found file.
 
Old 11-14-2011, 06:08 PM   #3
lainey
LQ Newbie
 
Registered: Nov 2011
Posts: 5

Original Poster
Rep: Reputation: Disabled
Thanks!
 
Old 11-15-2011, 01:29 PM   #4
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958Reputation: 1958
Note though that parsing ls is not generally recommended.

For an in-shell way, you can loop through the directory (or list of files) like this:
Code:
for file in * ; do
	fsize=$( stat -c %s $file )
	(( fsize > size )) && size=$fsize largest=$file
done

echo "The largest file is: $largest [$size]"
You can of course also add tests to ensure you exclude directories or whatever.
 
  


Reply


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
listing file size by percentage? abolishtheun Linux - Software 12 03-26-2008 07:22 AM
How to write crontab to check file size and all file in a directory? modpriest Linux - Newbie 1 02-14-2008 07:48 AM
how to get the size of a file in a shell variable bricedebrignaisplage Programming 2 10-18-2006 03:34 AM
How to issue "ls" command start with the largest file size in a folder? fjkum Linux - Newbie 5 07-27-2006 04:28 AM
Commit file size to a variable mark_wexler Programming 2 05-26-2006 07:48 AM


All times are GMT -5. The time now is 05:22 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
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration