28

I use below command to empty a file

> file.txt

It works perfectly fine! But there are some files with root user permissions. So I tried

sudo > different-file.txt

and I got below verbose

usage: sudo [-D level] -h | -K | -k | -V
usage: sudo -v [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid]
usage: sudo -l[l] [-AknS] [-D level] [-g groupname|#gid] [-p prompt] [-U user name] [-u user name|#uid] [-g groupname|#gid] [command]
usage: sudo [-AbEHknPS] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] [-g groupname|#gid] [VAR=value] [-i|-s]
        [<command>]
usage: sudo -e [-AknS] [-C fd] [-D level] [-g groupname|#gid] [-p prompt] [-u user name|#uid] file ...

This suggests that there is some syntax error in my command.

What is wrong?

Ramesh
  • 39,297

7 Answers7

47

To empty a file as root, you can also use the truncate command:

$ sudo truncate -s0 file.txt

The -s0 sets the file's size to 0, effectively emptying it.

terdon
  • 242,166
10

Use sudo -i to become root (with a root-shell) first instead.

sudo is used to run commands as root, but > isn't a command, but a redirection inside the shell - neither of which is run by root.

Say you run sudo cat foo > bar...

cat is run as root, and can access and do anything - including opening "foo" even if it was owned by another user than you and had all read-permissions turned off.

But > is part of the shell sudo cat is run in, and runs as you (non-root) and your normal permissions. And the file "bar" is also accessed (by the shell) as your normal (non-root) user, so your normal user must 1) have write permisson to the directory or 2) own the file. If the directory or file is owned by another user (including root), you won't be able to access it.

6

I created a new file named new which is now owned by the root user as seen below.

ls -lat new
-rw-r--r-- 1 root root 42 Sep  9 13:37 new

Now, I logged in as a normal user and from there I tried to erase the contents of this file using the below command.

sudo sh -c " > /root/new"

Now, I can verify that the contents are erased by logging back as root again.

ls -lat new
-rw-r--r-- 1 root root 0 Sep  9 13:42 new

References

This answer has the following to say.

Alternatively, you can run sudo sh -c "echo 'text' >> /file.txt", which also works, but is a bit of a hassle with all the interpolation/escaping that can interfere if you have complicated expressions.

However, since you are just emptying the file I believe there shouldn't be any complications in your case.

Ramesh
  • 39,297
5

It's difficult and confusing to get sudo and redirection together to work, but there is a more clear alternative:

printf '' | sudo tee file.txt

There is nothing special, actually:

  • tee truncates the output files by default without -a option.
  • It writes the input from stdin to the file.
  • The input is the empty string, so the file is left empty.
  • tee writes a copy of the input to the output, but the input is empty.
Volker Siegel
  • 17,283
3

sudo > different-file.txt launches sudo without arguments, and redirects standard output into different-file.txt. (Sudo probably outputs nothing to stdout when no arguments are given, so if you had permissions to overwrite you would probably truncate the file if it existed).

Try cat /dev/null > different-file.txt instead (from a root prompt).

Become root by either sudo -i or sudo /bin/bash

MattBianco
  • 3,704
3

Here's yet another alternative - just in case you were looking for one...

sudo cp /dev/null /path/to/soon/to/be/truncated/file
mikeserv
  • 58,310
  • Thanks so much! I had one single mistake in my line, and I think I should quote it here so that people are alerted: sudo cp /dev/null > /to/be/truncated/file. I dunno why exactly it spits me out a bash: /to/be/truncated/file: Permission denied but it does. Omit that damn > and it works. D'oh. (Well, in my case, the origins may still date back to my MS-DOS times, where I would frequently use C> type (...) plus the redirection operator >.) – syntaxerror Jan 29 '16 at 17:27
2

sudo wants to run a program for you, but > file.txt does not contain a command. It is simply an implicit output redirect (incidentally, this doesn't work as given under tcsh, zsh and probably others).

Even if you pass sudo a valid command, such as echo or : (the built in null command), what you are trying to do won't work as you expect. When you hit ENTER, your shell notices the output redirection and tries to open the target file for writing, as your currently logged in user. If you don't have write permission on the file, this will fail, because the redirect is set up outside the scope of sudo.

In order to truncate the file, you need to do the redirect in a root shell.

D_Bye
  • 13,977
  • 3
  • 44
  • 31