sparsefile 658 B

123456789101112131415161718192021222324252627282930313233343536
  1. #! /bin/sh
  2. # sparsefile - create sparse files conveniently
  3. #
  4. # depends on dd and dc commands.
  5. usage() {
  6. echo "usage: `basename $0` {10M|10G|10T} {filename}" 1>&2
  7. }
  8. size=$1
  9. if test "$size" = "-h"; then
  10. usage
  11. exit
  12. fi
  13. fnam=$2
  14. die() {
  15. usage
  16. exit 1
  17. }
  18. set -e
  19. units=`echo "$size" | sed 's!.*\(.\)$!\1!'`
  20. n=`echo "$size" | sed 's!\(.*\).$!\1!'`
  21. test "$units" && test "$n" && test "$units" != "$n" || die
  22. case "$units" in
  23. M)
  24. seek=`echo "$n 1024 * 1 - p" | dc` ;;
  25. G)
  26. seek=`echo "$n 1024 1024 * * 1 - p" | dc` ;;
  27. T)
  28. seek=`echo "$n 1024 1024 1024 * * * 1 - p" | dc` ;;
  29. *)
  30. die
  31. ;;
  32. esac
  33. sh -xc "dd bs=1k count=1 if=/dev/zero of=$fnam seek=$seek"
  34. ls -lh "$fnam"