![]() To truncate a file to 100 KB: # truncate -s 100K file.txt rw-r-r- 1 root root 0 Mar 17 18:39 file.txt The example below will truncate a file to 100 bytes in size. You can confirm by using ls -lh commands: # ls -lh /var/log/syslog Note that truncate command will retain file permissions and ownership. If we do a check again for the file size, it should be 0 bytes. It does not remove the file itself, but it leaves it on the disk as a zero byte file. As an example, let's clear our /var/log/syslog to 0 bytes using truncate. The truncation process basically removes all the contents of the file. ![]() This is better than manually deleting a file and maybe doing a touch for the new one. This is useful e.g for log clearing files. Clear contents of a file with truncate # truncate -s 0 file ![]() Examples of truncate usageīelow is the most common usage examples of the truncate command. The SIZE argument may also be prefixed by one of the following modifying characters: ' +' extend by, ' -' reduce by, ' ' at least, ' /' round down to multiple of, ' %' round up to multiple of.
0 Comments
Leave a Reply. |