nnn

mirror of https://github.com/jarun/nnn
Log | Files | Refs | README | LICENSE

verify-du.sh (2628B)


      1 #!/bin/sh
      2 # Verify disk usage: run du from a path and print in GB for comparison with nnn.
      3 # Usage: ./verify-du.sh [path]
      4 # Default path: current directory (.)
      5 # Compare the output with nnn's du display when run from the same path.
      6 
      7 path="${1:-.}"
      8 echo "Path: $path"
      9 echo ""
     10 
     11 # Count files (like nnn does)
     12 # nnn counts all non-directory entries: regular files, symlinks, devices, pipes, sockets, etc.
     13 # nnn also counts files it cannot stat (permission denied) as non-directories
     14 # -P: do not follow symbolic links
     15 # -xdev: do not cross filesystem boundaries
     16 # ! -type d: count everything except directories
     17 # Do NOT suppress errors - count entries we can't access too
     18 if command -v find >/dev/null 2>&1; then
     19 	echo "File count (all non-directory entries):"
     20 	# First, try to count including permission errors
     21 	# find will still list the path even if it can't stat it
     22 	file_count=$(find -P "$path" -xdev ! -type d 2>&1 | grep -v "Permission denied" | wc -l)
     23 
     24 	# Alternative: count accessible files + count permission denied entries
     25 	accessible=$(find -P "$path" -xdev ! -type d 2>/dev/null | wc -l)
     26 	denied=$(find -P "$path" -xdev 2>&1 >/dev/null | grep -c "Permission denied" || echo 0)
     27 
     28 	echo "  Accessible: $accessible files"
     29 	echo "  Permission denied: $denied entries"
     30 	echo "  Estimated total: $((accessible + denied)) files"
     31 
     32 	# Debug: show breakdown by type
     33 	echo ""
     34 	echo "  Breakdown of accessible files:"
     35 	echo "    Regular files: $(find -P "$path" -xdev -type f 2>/dev/null | wc -l)"
     36 	echo "    Symlinks: $(find -P "$path" -xdev -type l 2>/dev/null | wc -l)"
     37 	echo "    Other (devices, pipes, sockets): $(find -P "$path" -xdev \( -type c -o -type b -o -type p -o -type s \) 2>/dev/null | wc -l)"
     38 	echo ""
     39 
     40 	# Count directories
     41 	echo "Directory count:"
     42 	dir_count=$(find -P "$path" -xdev -type d 2>/dev/null | wc -l)
     43 	echo "  $dir_count directories"
     44 	echo ""
     45 fi
     46 
     47 # GNU du: -s = summary, default block size 1024 bytes
     48 # So "du -s" output * 1024 = bytes
     49 if command -v du >/dev/null 2>&1; then
     50 	echo "du -s (1024-byte blocks, same as 'du -sh'):"
     51 	blocks=$(du -s "$path" 2>/dev/null | cut -f1)
     52 	if [ -n "$blocks" ]; then
     53 		# bytes = blocks * 1024; GB = bytes / 1024^3
     54 		bytes=$((blocks * 1024))
     55 		gb=$(echo "scale=3; $bytes / 1073741824" | bc 2>/dev/null)
     56 		echo "  $blocks 1K-blocks"
     57 		echo "  ${gb} GB (bytes: $bytes)"
     58 	else
     59 		echo "  (failed or permission denied)"
     60 	fi
     61 	echo ""
     62 
     63 	# Also show in 512-byte blocks (st_blocks units, what nnn uses for disk usage)
     64 	echo "Equivalent in 512-byte blocks (nnn st_blocks units):"
     65 	if [ -n "$blocks" ]; then
     66 		blocks512=$((blocks * 2))
     67 		echo "  $blocks512 512-byte blocks"
     68 	fi
     69 fi