{"id":9121,"date":"2021-08-09T17:06:12","date_gmt":"2021-08-09T17:06:12","guid":{"rendered":"https:\/\/linuxways.net\/?p=9121"},"modified":"2021-08-09T17:06:12","modified_gmt":"2021-08-09T17:06:12","slug":"truncate-command-in-linux","status":"publish","type":"post","link":"https:\/\/linuxways.net\/de\/centos\/truncate-command-in-linux\/","title":{"rendered":"Truncate Command in Linux"},"content":{"rendered":"<h2>Introduction<\/h2>\n<p>truncate is a command used to resize the file to the size you require. It helps you to delete the content inside a file without removing that file.<\/p>\n<p>This command helps to truncate a file quickly and easily. The truncated file depends on the current file size; if the file size is larger than the specified size, the extra data will be lost.<\/p>\n<p>This article will show you how to use the truncate command in Linux as we go through below.<\/p>\n<h2>Install Coreutils Packages<\/h2>\n<p>Run the apt command:<\/p>\n<pre>$ sudo apt install coreutils<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1021\" height=\"694\" class=\"wp-image-9122\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-161.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-161.png 1021w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-161-300x204.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-161-768x522.png 768w\" sizes=\"auto, (max-width: 1021px) 100vw, 1021px\" \/><\/p>\n<p>Use the grep command to see the detail of packages:<\/p>\n<pre>$ dpkg -l | grep coreutils<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1021\" height=\"82\" class=\"wp-image-9123\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-162.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-162.png 1021w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-162-300x24.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-162-768x62.png 768w\" sizes=\"auto, (max-width: 1021px) 100vw, 1021px\" \/><\/p>\n<h2>The syntax of truncate command<\/h2>\n<pre>$ truncate -s SIZE filename<\/pre>\n<h2>The truncate command with examples<\/h2>\n<p><strong>1. Clear the content of the file<\/strong><\/p>\n<p>Syntax:<\/p>\n<pre>$ truncate -s 0 filename<\/pre>\n<p>We will use du command to check the disk space used:<\/p>\n<pre>$ du -sh filename<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1026\" height=\"63\" class=\"wp-image-9124\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-163.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-163.png 1026w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-163-300x18.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-163-1024x63.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-163-768x47.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-163-1020x63.png 1020w\" sizes=\"auto, (max-width: 1026px) 100vw, 1026px\" \/><\/p>\n<p>Here the original size of skip.txt file is 4.0KB.<\/p>\n<p>Now we will clear the content of skip.txt file following the syntax:<\/p>\n<pre>$ truncate -s 0 skip.txt<\/pre>\n<p>Then will use du command to check again:<\/p>\n<pre>$ du -sh skip.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"85\" class=\"wp-image-9125\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-164.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-164.png 1025w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-164-300x25.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-164-768x64.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-164-1020x85.png 1020w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><\/p>\n<p>Use the ls -lh command to check file size<\/p>\n<pre>$ ls -lh skip.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1026\" height=\"65\" class=\"wp-image-9126\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-165.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-165.png 1026w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-165-300x19.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-165-1024x65.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-165-768x49.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-165-1020x65.png 1020w\" sizes=\"auto, (max-width: 1026px) 100vw, 1026px\" \/><\/p>\n<p><strong>2. Truncate a file to the desired size<\/strong><\/p>\n<p>We will use du command to check the disk space used:<\/p>\n<pre>$ du -sh filename<\/pre>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"46\" class=\"wp-image-9127\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-166.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-166.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-166-300x13.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-166-768x35.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-166-1020x46.png 1020w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Here the original size of hello.txt file is 4.0KB.<\/p>\n<p>Now we will truncate the file to 400 bytes size:<\/p>\n<pre>$ truncate -s 400 hello.txt<\/pre>\n<p>Use the ls -lh command to check file size:<\/p>\n<pre>$ ls -lh hello.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"84\" class=\"wp-image-9128\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-167.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-167.png 1025w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-167-300x25.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-167-768x63.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-167-1020x84.png 1020w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><\/p>\n<p><strong>3. Increase or decrease file size<\/strong><\/p>\n<p>You can use the prefix &#8220;+&#8221; or &#8220;-&#8221; with<strong> -s <\/strong>option to do this.<\/p>\n<p>The dup.txt file is 400k:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"86\" class=\"wp-image-9129\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-168.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-168.png 1025w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-168-300x25.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-168-768x64.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-168-1020x86.png 1020w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><\/p>\n<p>Now we will try increasing 600k bytes:<\/p>\n<pre>$ truncate -s +600k dup.txt<\/pre>\n<p>Then check the file:<\/p>\n<pre>$ ls -lh dup.txt<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"86\" class=\"wp-image-9130\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-169.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-169.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-169-300x25.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-169-768x65.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-169-1020x86.png 1020w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p>Same as above, you can use the prefix &#8220;-&#8221; with<strong> -s <\/strong>option to decrease file size.<\/p>\n<p><strong>4. Get help<\/strong><\/p>\n<pre>$ truncate --help<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1024\" height=\"592\" class=\"wp-image-9131\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-170.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-170.png 1024w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-170-300x173.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-170-768x444.png 768w\" sizes=\"auto, (max-width: 1024px) 100vw, 1024px\" \/><\/p>\n<p><strong>5. Check your version<\/strong><\/p>\n<pre>$ truncate --version<\/pre>\n<p>Output:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" width=\"1025\" height=\"191\" class=\"wp-image-9132\" src=\"http:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-171.png\" srcset=\"https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-171.png 1025w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-171-300x56.png 300w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-171-768x143.png 768w, https:\/\/linuxways.net\/wp-content\/uploads\/2021\/08\/word-image-171-1020x191.png 1020w\" sizes=\"auto, (max-width: 1025px) 100vw, 1025px\" \/><\/p>\n<h2>Conclusion<\/h2>\n<p>You\u2019ve already gone through the details of how to use the truncate command in Linux.<\/p>\n<p>Thanks for reading.<\/p>","protected":false},"excerpt":{"rendered":"<p>Introduction truncate is a command used to resize the file to the size you require. It helps you to delete the content inside a file without removing that&hellip;<\/p>","protected":false},"author":1,"featured_media":9156,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1,4,5,83,165,2],"tags":[473],"class_list":["post-9121","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-centos","category-debian","category-mint","category-opensuse","category-red-hat","category-ubuntu","tag-truncate-command"],"_links":{"self":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9121","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/comments?post=9121"}],"version-history":[{"count":0,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/posts\/9121\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media\/9156"}],"wp:attachment":[{"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/media?parent=9121"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/categories?post=9121"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/linuxways.net\/de\/wp-json\/wp\/v2\/tags?post=9121"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}