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-23-2011, 02:59 PM   #1
am28
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Rep: Reputation: Disabled
How do you find and remove largest file without having to manually type the file name


Hi I'm only starting to learn Linux and i am stuck on this problem
I have found the largest file by using
ls -S|head -1
But i don't know how to remove this file without manually typing the file name.
 
Old 11-23-2011, 03:06 PM   #2
crabboy
Senior Member
 
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
Code:
ls -S | head -1 | xargs rm
xargs will pass any and all text from stdin to the given commands parameter list.
 
1 members found this post helpful.
Old 11-23-2011, 03:18 PM   #3
am28
LQ Newbie
 
Registered: Nov 2011
Posts: 3

Original Poster
Rep: Reputation: Disabled
Thanks a million crabboy it worked a treat
 
Old 11-23-2011, 03:52 PM   #4
MTK358
LQ 5k Club
 
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720Reputation: 720
Or you can use:

Code:
rm "$(ls -S | head -1)"
. $(command) is substituted with the command's output, and the double quotes prevent filenames with whitespace from being treated as separate arguments.
 
Old 11-23-2011, 04:45 PM   #5
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957Reputation: 1957
I made a post on a similar topic just a few hours ago. ls is not usually the recommended tool for such things.

http://www.linuxquestions.org/questi...1/#post4531807

Using the same looping technique as I posted before, this would be safer, if a bit longer.

Code:
lsize=0
for file in *; do
	[[ ! -f $file ]] && continue
	size=$( stat -c "%s" $file )

	if (( size > lsize )); then
		lsize=$size
		biggest=$file
	fi
done

echo "Largest file: $biggest [$lsize bytes]"
Replace the echo at the end with rm and you're set.
 
  


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
Find largest file within a directory grob115 Programming 5 09-04-2011 03:35 AM
Command to find largest file. paragkalra Linux - Software 2 11-17-2009 05:36 AM
how to find out file system type wabaha Linux - Newbie 3 10-25-2007 12:50 PM
how do i find the largest file on machine amit_joshi Linux - Newbie 4 10-08-2007 08:09 PM
How do I remove this type of file?.. kato678 Linux - Newbie 5 06-05-2002 08:02 AM


All times are GMT -5. The time now is 08:25 AM.

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