Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


  Search this Thread
Old 11-23-2011, 02:59 PM   #1
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
Senior Member
Registered: Feb 2001
Location: Atlanta, GA
Distribution: Slackware
Posts: 1,823

Rep: Reputation: 120Reputation: 120
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
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
LQ 5k Club
Registered: Sep 2009
Posts: 6,443
Blog Entries: 3

Rep: Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721Reputation: 721
Or you can use:

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 + kde 4 / 5
Posts: 6,837

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

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

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

	if (( size > lsize )); then

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


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:39 AM.

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