0
0
Bash-scriptingHow-ToBeginner · 2 min read

Bash Script to Find Largest File in Directory

Use ls -S | head -n 1 to list files sorted by size and pick the largest, or use find . -type f -exec ls -s {} + | sort -n -r | head -n 1 for a more robust script.
📋

Examples

InputDirectory with files: file1.txt (100 bytes), file2.txt (200 bytes), file3.txt (50 bytes)
Outputfile2.txt
InputDirectory with files: a.log (1024 bytes), b.log (2048 bytes), c.log (512 bytes)
Outputb.log
InputEmpty directory
Output
🧠

How to Think About It

To find the largest file, first list all files with their sizes, then sort them by size in descending order, and finally select the top file. This approach ensures you compare actual file sizes and pick the biggest one.
📐

Algorithm

1
List all files in the directory with their sizes.
2
Sort the list by file size from largest to smallest.
3
Select the first file from the sorted list as the largest file.
4
Print the name of the largest file.
💻

Code

bash
#!/bin/bash

largest_file=$(find . -maxdepth 1 -type f -exec ls -s {} + | sort -n -r | head -n 1 | awk '{print $2}')
echo "$largest_file"
Output
file2.txt
🔍

Dry Run

Let's trace a directory with files file1.txt (100 bytes), file2.txt (200 bytes), file3.txt (50 bytes) through the code

1

Find files and list sizes

find . -maxdepth 1 -type f -exec ls -s {} + outputs: 200 ./file2.txt 100 ./file1.txt 50 ./file3.txt

2

Sort by size descending

Sorted output: 200 ./file2.txt 100 ./file1.txt 50 ./file3.txt

3

Select largest file

head -n 1 picks: 200 ./file2.txt awk '{print $2}' extracts: ./file2.txt

SizeFile
200./file2.txt
100./file1.txt
50./file3.txt
💡

Why This Works

Step 1: Find files with sizes

The find command locates files and ls -s lists their sizes in blocks.

Step 2: Sort files by size

Sorting numerically in reverse order with sort -n -r puts the largest file first.

Step 3: Extract largest file name

Using head -n 1 picks the top file, and awk '{print $2}' extracts its name.

🔄

Alternative Approaches

Using ls with sort
bash
largest_file=$(ls -S | head -n 1)
echo "$largest_file"
Simple and fast but only works in current directory and may fail with filenames containing spaces.
Using find with stat
bash
largest_file=$(find . -maxdepth 1 -type f -printf '%s %p\n' | sort -nr | head -n 1 | cut -d' ' -f2-)
echo "$largest_file"
More precise size in bytes and handles spaces in filenames better.

Complexity: O(n log n) time, O(n) space

Time Complexity

Listing and sorting all files takes O(n log n) time, where n is the number of files.

Space Complexity

The script stores file info temporarily, using O(n) space for sorting.

Which Approach is Fastest?

Using ls -S is fastest but less reliable; find with stat is more robust but slightly slower.

ApproachTimeSpaceBest For
ls -S | head -n 1O(n log n)O(n)Simple cases, no spaces in filenames
find . -exec ls -s {} + | sort -n -r | head -n 1O(n log n)O(n)General use, handles spaces
find . -printf '%s %p\n' | sort -nr | head -n 1O(n log n)O(n)Precise size, handles spaces well
💡
Use find with sort for reliable results even with complex filenames.
⚠️
Using ls alone without sorting by size or handling spaces in filenames can give wrong results.