Finding largest file recursively

user2419571

I am trying to find the largest file in a directory recursively. If there is a subdirectory inside of that directory the function needs to go inside that directory and check to see if the largest file is there. Once the largest file is found the output is displayed with the relative path name and the name and size of the largest file.

EX:

dude@shell2 (~...assignment/solutions) % bash maxfile.sh ~/test
class/asn
dude.h.gch: 9481628

This is what I have:

#!/bin/sh
clear

recursiveS() {
    for d in *; do
        if [ -d $d ]; then
            (cd $d; echo $(pwd)/$line; du -a; recursiveS;)
        fi
    done
}
recursiveS

I have been stuck for a while now. I cannot implement this by pipelining a number of existing Unix tools. Any ideas would be nice!

glenn jackman

use find (here assuming GNU find) to output file names with the file size. sort. print out the largest one.

find . -type f -printf "%s\t%p\n" | sort -n | tail -1

That assumes file paths don't contain newline characters.


Using a loop in bash with the GNU implementation of stat:

shopt -s globstar
max_s=0
for f in **; do
  if [[ -f "$f" && ! -L "$f" ]]; then
    size=$( stat -c %s -- "$f" )
    if (( size > max_s )); then
      max_s=$size
      max_f=$f
    fi
  fi
done
echo "$max_s $max_f"

This will be significantly slower than the find solution. That also assumes that file names don't end in newline characters and will skip hidden files and not descend into hidden directories.

If there's a file called - in the current directory, the size of the file open on stdin will be considered.

Beware that versions of bash prior to 4.3 followed symbolic links when descending the directory tree.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Finding largest file size recursively from a directory

From Java

finding the largest version number file of of many files?

From Dev

Git: finding largest files of a large pack file

From Dev

Finding file by name recursively, deleting it and creating a symlink

From Dev

I would like to find the largest file in each directory recursively

From Dev

Finding the Largest File in a Folder With Anonymous Type and the IComparable Interface

From Dev

Finding the largest and second largest number

From Dev

Finding the largest and second largest number

From Dev

Find largest files recursively

From Dev

Finding Largest Twin Prime

From Dev

Finding Largest String in ArrayList

From Dev

Finding the largest time in an array

From Dev

finding largest number Qt

From Dev

Finding Largest Twin Prime

From Dev

Recursively finding indices of a string

From Dev

Recursively finding with AWK

From Dev

Finding a substring recursively

From Dev

Finding empty directories recursively

From Dev

Regex for finding a string recursively?

From Dev

Recursively finding indices of a string

From Dev

The largest file

From Dev

The largest file

From Dev

Finding k-largest elements of a very large file (while k is very LARGE)

From Dev

Finding the nth largest in an int array

From Dev

Finding Largest Element of a Matrix in R

From Dev

finding largest consecutive region in table

From Dev

Finding the largest vector inside a matrix

From Dev

Finding Largest Values in Array of Arrays

From Dev

Dynamic programming: finding largest triangle