Truncate File Linux - 5 Easy Methods
3 min. read
Published on
Read our disclosure page to find out how can you help MSPoweruser sustain the editorial team Read more
Want to truncate a file on Linux? This operation refers to removing a file’s content or reducing its size. More specifically, truncating is much easier and faster than deleting a file, creating it again, and specifying the correct file ownership and permissions.
In this guide, I’ll show you how to achieve this. So, let’s go!
How to Truncate Files in Linux
To truncate files in Linux, you can use:
- Colon “:” Symbol
- cat command
- Redirect operator with echo command
- Simple Redirection
- truncate command
1. Using the Colon “:” Symbol
In shell scripting, the colon “:” symbol is primarily used as a null command or placeholder. This symbol results in no output. Therefore, you can utilize it to truncate an existing file.
To do so, I’ll press CTRL+ALT+T to open my terminal, type the “: > filename” command, and hit Enter.
In my case, this command truncates the content of test.txt to zero while keeping the file intact.
2. Using the cat Command
The cat command is usually used to concatenate and show the contents of files on a Linux terminal. However, you can also utilize it to access the null device and truncate the given file.
For this, I’ll run the “cat /dev/null > filename” command.
Here, the cat command truncates the given file by redirecting the null device’s output to it.
3. Using Redirect Operator with echo
Linux echo command prints variable or text value on the terminal. However, you can add the -n option in the echo command to force it to NOT append a newline character.
This consequently redirects an empty string to the given file using the Redirect operator “>“.
For instance, I’ll type the “echo -n > filename” command.
4. Using Simple Redirection
To omit the command before the redirection operator and truncate the specified file, you can simply run the “> filename” command.
The above command truncates the specified file if it exists. Otherwise, it creates a new empty file.
5. Using the truncate Command
On Linux-based systems, the truncate command enables you to remove the content of a file or resize it to the given size. However, to utilize it on your system, you must install core-utils with the “sudo apt-get install coreutils” command.
After successful installation, I’ll use the “truncate -s number_of_bytes filename” command to reduce my test.txt file to 10 bytes.
In the above command, you can also specify other file size units such as:
- K for kilobytes
- M for megabytes
- G for gigabytes
To completely remove file contents, I’ll run the “truncate -s 0 filename” command.
Moreover, using this effective approach, you can empty all log files at once.
Bonus Tips:
To truncate a file, you must have write permission for it. For instance, I’ve changed the file permissions of my test.txt file to read-only.
Now, if I try to truncate it, the output will show a Permission denied error.
To resolve this issue, the first option is to open a new shell with sudo and run the same command with the -c option.
Additionally, you can pipe “|” the output of the tee command with sudo and write the empty output to the specified file.
You may also be interested in:
- Linux Remove User From Group
- FL Studio Linux
- How to Delete a User in Linux
- Rename Directory Linux
- Copy Directory Linux
So, now you know how to truncate a file on Linux! If you have any other tips, share them in the comments below.
User forum
0 messages