Intro To 'truncate' Command In Linux
2023-06-18 - By Robert Elder
I use the 'truncate' command to truncate or extend a file to a given size:
truncate -s 0 foo.txt
Truncate A File
Let's start by creating a file called 'README.txt' that contains a simple 'hello world' satement:
echo "Hello World!" > README.txt
I can use the 'truncate' command to truncate this readme file by providing a value of 0 to the '-s' flag:
truncate -s 0 README.txt
Using the 'ls' command, you can see that the file is now empty:
ls -l
-rw-rw-r-- 1 robert robert 0 Jun 11 09:48 README.txt
And using the 'cat' command, you can see that there is indeed nothing in the file anymore:
cat README.txt
(no output)
Specify A Target File Size
If I specify a different value to the '-s' flag, like 12, the size of the file will be explicitly changed to 12 bytes:
touch README.txt
truncate -s 12 README.txt
ls -l
-rw-rw-r-- 1 robert robert 12 Jun 11 09:54 README.txt
xxd README.txt
00000000: 0000 0000 0000 0000 0000 0000 ............
If the file was previously larger than 12 bytes, the extra data will be discarded, but the first part of the file will remain unchanged:
echo "The quick brown fox jumps over the lazy dog" > README-full.txt
truncate -s 12 README-full.txt
xxd README-full.txt
00000000: 5468 6520 7175 6963 6b20 6272 The quick br
Size Units
You can also provide size units like kilobytes or megabytes to the '-s' flag.
truncate -s 12K README.txt
ls -l README.txt
-rw-rw-r-- 1 robert robert 12288 Jun 11 10:07 README.txt
truncate -s 12M README.txt
ls -l README.txt
-rw-rw-r-- 1 robert robert 12582912 Jun 11 10:08 README.txt
Relative Size Adjustments
The 'truncate' command also supports several modifiers to the size argument:
info truncate
...
SIZE may also be prefixed by one of the following to adjust the
size of each FILE based on its current size:
‘+’ => extend by
‘-’ => reduce by
‘<’ => at most
‘>’ => at least
‘/’ => round down to multiple of
‘%’ => round up to multiple of
...
Here's an example of using plus to add to the current file size:
rm README.txt
touch README.txt
xxd README.txt
(no output)
truncate -s +10 README.txt
xxd README.txt
00000000: 0000 0000 0000 0000 0000 ..........
truncate -s +10 README.txt
xxd README.txt
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000010: 0000 0000
Here's an example of using minus to reduce the size of the same file:
truncate -s -3 README.txt
xxd README.txt
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000010: 00
truncate -s -3 README.txt
xxd README.txt
00000000: 0000 0000 0000 0000 0000 0000 0000 ..............
The percentage sign modifier will round the file size up to the nearest multiple:
truncate -s %13 README.txt
xxd README.txt
00000000: 0000 0000 0000 0000 0000 0000 0000 0000 ................
00000010: 0000 0000 0000 0000 0000 ..........
ls -l README.txt
-rw-rw-r-- 1 robert robert 26 Jun 11 10:10 README.txt
And that's why the 'truncate' command is my favourite Linux command.
Intro To 'stty' Command In Linux
Published 2023-10-04 |
$1.00 CAD |
Intro To 'nproc' Command In Linux
Published 2023-07-15 |
Intro To 'comm' Command In Linux
Published 2023-09-06 |
How To Force The 'true' Command To Return 'false'
Published 2023-07-09 |
A Surprisingly Common Mistake Involving Wildcards & The Find Command
Published 2020-01-21 |
A Guide to Recording 660FPS Video On A $6 Raspberry Pi Camera
Published 2019-08-01 |
Intro To 'chroot' Command In Linux
Published 2023-06-23 |
Join My Mailing List Privacy Policy |
Why Bother Subscribing?
|