Gradient
Gradient

Reputation: 2343

Difference between 'sed -i' and 'sed ... > file'

I would like to know the difference between these two lines :

sudo sed 's/GRUB_TIMEOUT=10/GRUB_TIMEOUT=3/' /etc/default/grub >/etc/default/grub

and

sudo sed -i 's/GRUB_TIMEOUT=10/GRUB_TIMEOUT=3/' /etc/default/grub

There seems to be a difference because the first returns a Permission denied error while the other doesn't.

Upvotes: 0

Views: 2155

Answers (2)

Jack Kelly
Jack Kelly

Reputation: 18667

As @sarathi said, the -i flag modifies the file in-place. The reason you're getting a permission denied error is because /etc/default/grub is probably only modifiable by root.

Your first command:

sudo sed 's/GRUB_TIMEOUT=10/GRUB_TIMEOUT=3/' /etc/default/grub >/etc/default/grub

Runs sed as a superuser, which doesn't do anything useful as sed writes to its stdout. Then it tries to overwrite /etc/default/grub as the current user, which is disallowed.

In the second command:

sudo sed -i 's/GRUB_TIMEOUT=10/GRUB_TIMEOUT=3/' /etc/default/grub

The file is modified by sed itself, which is running as root.

Upvotes: 4

Vijay
Vijay

Reputation: 67291

-i flag of sed says inplace replacement.

Upvotes: 1

Related Questions