LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How do you find and remove largest file without having to manually type the file name (https://www.linuxquestions.org/questions/linux-newbie-8/how-do-you-find-and-remove-largest-file-without-having-to-manually-type-the-file-name-915164/)

am28 11-23-2011 01:59 PM

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.

crabboy 11-23-2011 02:06 PM

Code:

ls -S | head -1 | xargs rm
xargs will pass any and all text from stdin to the given commands parameter list.

am28 11-23-2011 02:18 PM

Thanks a million crabboy it worked a treat

MTK358 11-23-2011 02:52 PM

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.

David the H. 11-23-2011 03:45 PM

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.


All times are GMT -5. The time now is 04:09 AM.