Tiny Core Linux
Off-Topic => Off-Topic - Tiny Tux's Corner => Topic started by: remus on September 15, 2011, 07:06:27 AM
-
Hi all,
Just found out how to erase the contents of a file without deleting the file. Here are two ways I learn't to do it.
echo -n > YOURFILE
cat /dev/null > YOURFILE
I'm actually trying to clear the default contents of the /usr/local/etc/samba/smb.conf file with sudo but I get an error message.
sudo cat /dev/null > smb.conf
gets me
-sh: can't create smb.conf: Permission denied
But if I become root with su
And then run the command
cat /dev/null > smb.confIt works.
Can someone help me with the logic here ?
I thought I could use sudo for everything.
-
As I recall, the script /usr/local/tce.installed/samba3 checks for the presence of an existing /usr/local/etc/samba/smb.conf and will not overwrite if one is already present...
-
To redirect to a file using sudo, try something like this:
sudo sh -c "cat /dev/null > file"
With your original command the shell was trying to do the redirect with the non-root users privileges.
-
The shortest is:
> file
Use sudo as needed.
-
Can someone help me with the logic here ?
I thought I could use sudo for everything.
the logic is simple, you gave root permissions to the echo not to the redirect, the redirect (>) is session specific, the root privileges are not passed through it
to achieve what you want you have to pipeline the command
first echo something then use a sudo command to overwrite the file
[ali@linux chuck]$ cat test
hello dear johnny
[ali@linux chuck]$ sudo echo "" > test
bash: test: Permission denied
[ali@linux chuck]$ echo -n | sudo tee test
[ali@linux chuck]$ cat test
[ali@linux chuck]$
here's more proof that it's session specific
the > doesn't care what is before it and it's good because it doesn't
you don't want your log files to be owned by root
it's like this
(sudo echo hi) > test
> is owned by user that's why the output file is owned by user, you were just doing it wrong
you should use > to redirect what the user sees, here's a good example
[ali@linux chuck]$ cat test
[ali@linux chuck]$ echo "why can i still see the output?" | sudo tee test
why can i still see the output?
[ali@linux chuck]$ cat test
why can i still see the output?
[ali@linux chuck]$ echo "i'd rather not see it, thank you" | sudo tee test > /dev/null
[ali@linux chuck]$ cat test
i'd rather not see it, thank you
[ali@linux chuck]$