#!/bin/sh # Increase or decrease the apparent size of a file # License: LGPLv2 # Author: # http://www.pixelbeat.org/ # Notes: # There is a truncate command packaged with coreutils since v7.0, # and available on FreeBSD for ages. These are more functional as # they allow specifying relative sizes and mandating the file exists. # This is a thin wrapper around the truncate functionality of dd. # Changes: # V1.0, 16 Dec 2005 Initial release if test "$Argc" != "2" { shell { echo " Usage: $[basename $0] Truncate the to exactly bytes. If doesn't exist it is created. is a number which may be optionally followed by the following multiplicative suffixes: b 512 KB 1000 K 1024 MB 1000*1000 M 1024*1024 and so on for G, T, P, E, Z, Y If the file previously was larger than this size, the extra data is lost. If the file previously was shorter, it is extended and the extended part reads as zero bytes. Note in both cases no data is written (if the filesystem supports holes in files). E.G.: truncate 2TB ext3.test " } >&2 exit 1 } global size := $1 global file := $2 global error := $[dd bs=1 seek=$size if=/dev/null of="$file" !2 > !1] global ret := $Status echo $error | grep -v "^0" > !2 exit $ret (CommandList children: [ (If arms: [ (if_arm cond: [ (Sentence child: (C {(Lit_Other "[")} {(DQ ($ VSub_Pound "$#"))} {(KW_Bang "!") (Lit_Other "=")} {(DQ (2))} {(Lit_Other "]")} ) terminator: ) ] action: [ (Subshell child: (C {(echo)} { (DQ ("\n") ("Usage: ") (CommandSubPart command_list: (CommandList children: [(C {(basename)} {($ VSub_Number "$0")})] ) left_token: spids: [68 72] ) (" \n") ("\n") ("Truncate the to exactly bytes.\n") ("\n") ("If doesn't exist it is created.\n") ("\n") (" is a number which may be optionally followed\n") ("by the following multiplicative suffixes:\n") (" b 512\n") (" KB 1000\n") (" K 1024\n") (" MB 1000*1000\n") (" M 1024*1024\n") ("and so on for G, T, P, E, Z, Y\n") ("\n") ("If the file previously was larger than this size, the extra data is\n") ("lost. If the file previously was shorter, it is extended\n") ("and the extended part reads as zero bytes. Note in both cases\n") ("no data is written (if the filesystem supports holes in files).\n") ("\n") ("E.G.: truncate 2TB ext3.test\n") ) } ) redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[99])] spids: [60 97] ) (C {(exit)} {(1)}) ] spids: [-1 57] ) ] spids: [-1 107] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:size) op:Equal rhs:{($ VSub_Number "$1")} spids:[110])] spids: [110] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:file) op: Equal rhs: {(DQ ($ VSub_Number "$2"))} spids: [113] ) ] spids: [113] ) (Assignment keyword: Assign_None pairs: [ (assign_pair lhs: (LhsName name:error) op: Equal rhs: { (CommandSubPart command_list: (CommandList children: [ (SimpleCommand words: [ {(dd)} {(Lit_VarLike "bs=") (1)} {(Lit_VarLike "seek=") ($ VSub_Name "$size")} {(Lit_VarLike "if=") (/dev/null)} {(Lit_VarLike "of=") (DQ ($ VSub_Name "$file"))} ] redirects: [(Redir op_id:Redir_GreatAnd fd:2 arg_word:{(1)} spids:[137])] ) ] ) left_token: spids: [120 139] ) } spids: [119] ) ] spids: [119] ) (Assignment keyword: Assign_None pairs: [(assign_pair lhs:(LhsName name:ret) op:Equal rhs:{($ VSub_QMark "$?")} spids:[141])] spids: [141] ) (Pipeline children: [ (C {(echo)} {(DQ ($ VSub_Name "$error"))}) (SimpleCommand words: [{(grep)} {(-v)} {(DQ ("^0"))}] redirects: [(Redir op_id:Redir_GreatAnd fd:-1 arg_word:{(2)} spids:[160])] ) ] negated: False ) (C {(exit)} {($ VSub_Name "$ret")}) ] )