truncate Command in Linux
Transform Your Career
Choose from our industry-leading programs designed for career success
Modern Software and AI Engineering Program
Master full-stack development with AI integration
+1000 moreModern Data Science and ML with specialisation in AI
Advanced data science techniques with AI specialization
+1000 moreAdvanced AIML with Specialisation in Agentic AI
Deep dive into AIML with focus on Agentic systems
+1000 moreDevOps, Cloud & AI Platform Engineering
Build and manage AI-powered cloud infrastructure
+1000 moreAI Engineering Advanced Certification by IIT-Roorkee
Premier AI engineering certification from IIT-Roorkee
Overview
In the vast world of Linux, various commands make our lives easier. One such utility is the 'truncate command in Linux'. The truncate command is a straightforward but potent utility to resize or truncate a file to a specified size. The primary use of the truncate command in Linux is to save disk space, especially in scenarios where log files grow uncontrollably, and you need to get them down to a manageable size without deleting them entirely.
Syntax of truncate Command in Linux
The basic syntax of the truncate command in Linux is as follows:
Where:
- OPTION: OPTION are flags or settings that change the behavior of the command. The truncate command has a limited but useful set of options.
- SIZE: SIZE is the desired file size after truncation. It can be in bytes (default), kilobytes (K), megabytes (M), gigabytes (G), terabytes (T), and so on.
- FILE: FILE is the name of the file that you want to truncate.
Options in truncate Command in Linux
-
-s, --size=SIZE: Set or adjust the file size by SIZE bytes. SIZE can also have a leading '+' or '-' sign, indicating the file should be increased or decreased by that amount.
For example -
Output:
The -s option increases the size of myfile.txt by 2MB. If the file was less than 2MB before the command, it will be exactly 2MB after.
-
-c, --no-create: Do not create any files that do not exist.
For example -
Output:
The -c option ensures that if 'myfile.txt' does not exist, it will not be created. Without this option, the truncate command in Linux creates a new file if the file in the command does not exist.
-
-o, --io-blocks: Treat SIZE as the number of IO blocks of the file rather than bytes.
For example -
Output:
The -o option allows the SIZE to be treated as IO blocks rather than bytes, setting the size of 'myfile.txt' to one IO block.
Scaler Placement Report and Statistics
Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.
Example Usages
-
Truncating a file to a specific size:
Output:
Explanation: This command truncates 'myfile.txt' to a size of 10KB. If the file was larger than 10KB, the extra data is lost. If it was smaller, it's increased to 10KB.
-
Increasing file size:
Output:
Explanation: This command increases the size of 'myfile.txt' by 5MB. The '+' sign before the size indicates that the size should be increased.
Tips
-
It's important to note that the truncate command in Linux does not overwrite or alter existing data when enlarging a file. Instead, it simply extends the file size and fills the new space with null bytes.
-
Keep in mind that if you truncate a file to a smaller size, data loss will occur.
Turn Learning into Career Growth
Advanced Use Cases of truncate Command in Linux
-
Combining truncate with find to truncate multiple files:
Output:
Explanation: This command uses the find command to locate all .log files in the specified directory and its subdirectories, then uses the truncate command to empty each file.
-
Use truncate with shell redirection:
Output:
Explanation: This is a shell redirection trick that can serve as a substitute for the truncate command in Linux. The ':' is a shell builtin that does nothing, and the '>' redirects the output (which is nothing) to 'myfile.txt', effectively truncating it.
Conclusion
-
The truncate command in Linux is a simple and effective way to manage file sizes.
-
It's primarily used to increase or decrease the size of a file, with the ability to specify the size in various units such as bytes, kilobytes, megabytes, and more.
-
Care should be taken when truncating a file to a smaller size as it leads to data loss.
-
The truncate command does not alter or overwrite existing data when enlarging a file, but instead fills the new space with null bytes.




