truncate Command in Linux

Learn via video courses
Topics Covered

Transform Your Career

Choose from our industry-leading programs designed for career success

NSDC Certified

Modern Software and AI Engineering Program

Master full-stack development with AI integration

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Modern Data Science and ML with specialisation in AI

Advanced data science techniques with AI specialization

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

Advanced AIML with Specialisation in Agentic AI

Deep dive into AIML with focus on Agentic systems

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

DevOps, Cloud & AI Platform Engineering

Build and manage AI-powered cloud infrastructure

12 MonthsDuration
AI-LedCurriculum
Career SupportSupport
GoogleAmazonPaytm+1000 more
Go to Program
NSDC Certified

AI Engineering Advanced Certification by IIT-Roorkee

Premier AI engineering certification from IIT-Roorkee

3 MonthsDuration
AI-LedCurriculum
Career SupportSupport
Program highlights
Go to Program

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.
Free Courses by top Scaler instructors
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course
Python Course for Beginners With Certification: Mastering the Essentials
Java Course - Mastering the Fundamentals
DBMS Course - Master the Fundamentals and Advanced Concepts
JavaScript Course With Certification: Unlocking the Power of JavaScript
C++ Course: Learn the Essentials
Python and SQL for Data Science Course

Options in truncate Command in Linux

  1. -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.

  2. -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.

  3. -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

₹23L
AVG CTC
SCALER PLACEMENT PROOF

Scaler learners achieved 2.5x salary growth with average post-Scaler CTC reaching ₹23L.

11,000+placements
650+companies
Verified data
Hiring Partners:
GoogleGoogleAmazonAmazonMicrosoftMicrosoftFlipkartFlipkartAdobeAdobe1200+ more

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

1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary
1200+Hiring Partners
89%Placement Rate
11,000+Placements
147%Avg Salary Increment
2.5XCareer Growth
₹23 LPAAvg Post-Scaler Salary

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.