Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to empty ("truncate") a file on linux that already exists and is protected in someway?

People also ask

How do I truncate a file in Linux?

To empty the file completely, use -s 0 in your command. Add a plus or minus sign in front of the number to increase or decrease the file by the given amount. If you don't have proper permissions on the file you're trying to truncate, you can usually just preface the command with sudo .

How can I edit a write protected file in Linux?

Under Linux and UNIX user cannot remove or modify file if they don't have a write permission. You can use normal chmod command for this purpose. Method #2 : You need to use chattr command which changes the file attributes on a Linux second extended (ext2 / ext3) file system.

How do you clear a file in Linux terminal?

To remove (or delete) a file in Linux from the command line, use either the rm (remove) or unlink command.


You have the noclobber option set. The error looks like it's from csh, so you would do:

cat /dev/null >! file

If I'm wrong and you are using bash, you should do:

cat /dev/null >| file

in bash, you can also shorten that to:

>| file

You can also use function truncate

$truncate -s0 yourfile

if permission denied, use sudo

$sudo truncate -s0 yourfile

Help/Manual: man truncate

tested on ubuntu Linux


This will be enough to set the file size to 0:

> error.log

the credit goes for my senior colleague for this:

:> filename

This will not break log files, so you can even use it on syslog, for example.


false|tee fileToTruncate

may work as well