File: sparsefile

package info (click to toggle)
vblade 24-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 364 kB
  • sloc: ansic: 1,299; sh: 633; makefile: 58
file content (36 lines) | stat: -rwxr-xr-x 658 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#! /bin/sh
# sparsefile - create sparse files conveniently
#
# depends on dd and dc commands.

usage() {
	echo "usage: `basename $0` {10M|10G|10T} {filename}" 1>&2
}
size=$1
if test "$size" = "-h"; then
	usage
	exit
fi
fnam=$2

die() {
	usage
	exit 1
}
set -e
units=`echo "$size" | sed 's!.*\(.\)$!\1!'`
n=`echo "$size" | sed 's!\(.*\).$!\1!'`
test "$units" && test "$n" && test "$units" != "$n" || die
case "$units" in
M)
	seek=`echo "$n 1024 * 1 - p" | dc` ;;
G)
	seek=`echo "$n 1024 1024 * * 1 - p" | dc` ;;
T)
	seek=`echo "$n 1024 1024 1024 * * * 1 - p" | dc` ;;
*)
	die
	;;
esac
sh -xc "dd bs=1k count=1 if=/dev/zero of=$fnam seek=$seek"
ls -lh "$fnam"