

:max_bytes(150000):strip_icc()/how-to-change-your-signature-in-outlook-4685088-2-748cd0bba4a04569a47db438759cca26.png)
In ksh and its derivates, the >| is used. In csh and its derivative shell, you could use the >! instead of > to override the noclobber configuration. You will need to modify the command to over ride the noclobber option.ĭepending on your command shell, you can try the following. If noclobber is set or enabled on your system, the above I/O redirection methods will throw an error. Noclobber is a feature in Linux which is used to prevent accidental overwriting of files. You can choose to use cp command to truncate the file or use redirection as mentioned above.Īs mentioned in the previous section, you can use redirection as well…īash$ cat /dev/null > myfile.txt noclobber If you have older version, it is possible that it may not be implemented. Most modern versions of Linux has an implementation of the /dev/null device. All of the commands below will truncate files to zero bytes. Other no-op commands such as “:” or true can be used as well. For example, the echo command can be used as shown below.

You can also use the output of various different commands, that produce an empty output with redirection. If you have a really large file, then the I/O redirection could be substantially faster. Also this is the fastest of all the method mentioned here. If the file already exist, it will delete all its content and save it as a zero byte file.Īlthough it is not precisely the same as the truncate system call mentioned above, it will work for almost all practical use cases. This will create a new empty file, if one does not exist. One of the easiest and quickest way to empty a file is to use I/O redirection. On Unix-like systems, the shell command touch filename results in a zero-byte file filename. s or –size: specifies the size to which the file needs to be truncated, in bytes. There are many ways that could manually create a zero-byte file, for example, saving empty content in a text editor, using utilities provided by operating systems, or programming to create it. We will use the size 0 (zero) to empty the file. It is used to shrink the size of a file to a desired size.

Truncate is a command line utility that can be found in most Linux distros. We will only deal with the case where you want to completely empty the file or zero it out and not the case where you might want to remove parts of the file or truncate files by certain number of lines or byte size. The truncation process however reduces the need for the two different steps and also will preserve the inode of the file. You can always delete the file and recreate the file, which might work in most cases. This process is also referred to as “ zero out a file” or to “ empty a file“. This allows the file to re-used or be continually used by other programs while keeping the overall size in check. It does not however remove the file itself, but it leaves it on the disk as a zero byte file. The truncation process basically removes all the contents of the file. This is not just relegated to log files, but could also happen to output files or any other file as well. This is especially true about log files, which often contains a large amount of outdated data. Sometimes, you want to start over by emptying the file especially if you are trying to minimize disk space. That is the least efficient, but if you need your drives to spin when accessing that file, that's the one you'll want to go for.Quite often it is possible to end up with large files, especially large log files on your system. That allocates the space without actually writing data to the disk (the space is reserved but marked as uninitialised). If you'd rather the disk space be allocated, on Linux and some filesystems, you could do: fallocate -l 1M nullbytes (with some dd implementations, you can replace 1048576 with 1M) Without truncate, you can use dd instead: dd bs=1048576 seek=1 of=nullbytes count=0 That is a file that appears filled with zeros but that doesn't take any space on disk. (assuming nullbytes didn't exist beforehand) would create a 1 mebibyte sparse file. With GNU truncate: truncate -s 1M nullbytes
