Truncate a file: Difference between revisions

Content added Content deleted
(Add C for Windows.)
(→‎{{header|UNIX Shell}}: Restore my dd(1) example; my OpenBSD system has no truncate(1) command. Fix the dd(1) example to not create the file.)
Line 151: Line 151:


=={{header|UNIX Shell}}==
=={{header|UNIX Shell}}==
The dd(1) command can truncate a file. Because dd(1) would create the file, this example runs ls(1). If the file does not exist, then ls(1) prints an error. If the file exists, then dd(1) truncates the file or prints an error. Unix can extend a file, so there is no error

<lang bash># Truncate a file named "myfile" to 1440 kilobytes.
ls myfile >/dev/null &&
dd if=/dev/null of=myfile bs=1 seek=1440k</lang>

----
Some systems have a truncate(1) command ([http://www.freebsd.org/cgi/man.cgi?query=truncate&apropos=0&sektion=0&manpath=FreeBSD+8.2-RELEASE&format=html FreeBSD truncate(1)], [http://www.gnu.org/software/coreutils/manual/html_node/truncate-invocation.html#truncate-invocation GNU truncate(1)]).

<lang bash># Truncate a file named "myfile" to 1440 kilobytes.
<lang bash># Truncate a file named "myfile" to 1440 kilobytes.
truncate -s 1440k myfile</lang>
truncate -s 1440k myfile</lang>