在文件尾追加内容的命令。
- CuiCan
- 帖子: 27
- 注册时间: 2011-06-14 19:53
在文件尾追加内容的命令。
一般情况下用这个命令就行了:echo "xxxxxx" >>file。
如果要向需要特权的文件(例如,系统配置文件)追加内容怎么办,上面的命令不行啊,sudo echo "xxxxxx" >>file 也不行,可有人知道怎么办?
如果要向需要特权的文件(例如,系统配置文件)追加内容怎么办,上面的命令不行啊,sudo echo "xxxxxx" >>file 也不行,可有人知道怎么办?
Microsoft gives you windows,but Linux gives you whole house.
- ttand
- 帖子: 1745
- 注册时间: 2005-08-22 14:05
- 来自: 离开北京
Re: 在文件尾追加内容的命令。
sudo sh -c "echo xxxxxx >>file"
唉 sudo有时候挺麻烦的
唉 sudo有时候挺麻烦的
上次由 ttand 在 2011-12-14 14:01,总共编辑 1 次。
错过好多好贴,没占到广告位后悔啊
- mjp123
- 帖子: 703
- 注册时间: 2009-04-09 15:06
Re: 在文件尾追加内容的命令。
echo password | sudo echo "Xxxxxxxxxxx" >>file
- CuiCan
- 帖子: 27
- 注册时间: 2011-06-14 19:53
Re: 在文件尾追加内容的命令。
2楼正解。sudo是有些麻烦,不过安全些。3楼的方法不行,结果和 sudo echo "xxxxxx" >>file 一样,都是Permission denied。谢谢了!
Microsoft gives you windows,but Linux gives you whole house.
- 枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
- 联系:
- CuiCan
- 帖子: 27
- 注册时间: 2011-06-14 19:53
Re: 在文件尾追加内容的命令。
少了什么参数,求解啊枫叶饭团 写了:3楼sudo 少了个参数

Microsoft gives you windows,but Linux gives you whole house.
- eexpress
- 帖子: 58428
- 注册时间: 2005-08-14 21:55
- 来自: 长沙
- CuiCan
- 帖子: 27
- 注册时间: 2011-06-14 19:53
- 枫叶饭团
- 帖子: 14683
- 注册时间: 2010-06-16 1:05
- 系统: Mac OS X
- 来自: Tencent
- 联系:
Re: 在文件尾追加内容的命令。
也对sudo只对echo提高了权限,重定向却没有。echo和>>用单引号括在一起再看看
- mjp123
- 帖子: 703
- 注册时间: 2009-04-09 15:06
Re: 在文件尾追加内容的命令。

是错了,谢谢指正。mjp123 写了:echo password | sudo echo "Xxxxxxxxxxx" >>file

- josephyoung
- 帖子: 158
- 注册时间: 2011-11-05 18:53
- 来自: 南极圈
Re: 在文件尾追加内容的命令。
用sed
sudo sed -i '$a xxxxxxxx' urfile
sudo sed -i '$a xxxxxxxx' urfile
- josephyoung
- 帖子: 158
- 注册时间: 2011-11-05 18:53
- 来自: 南极圈
Re: 在文件尾追加内容的命令。
又研究出一个新方法,呵呵
echo -e "a\nxxxxxxxxxxxxxx\n.\nwq" | sudo ed -s urfile
echo -e "a\nxxxxxxxxxxxxxx\n.\nwq" | sudo ed -s urfile
- astolia
- 论坛版主
- 帖子: 6703
- 注册时间: 2008-09-18 13:11
Re: 在文件尾追加内容的命令。
echo "text" | sudo tee -a filename
- HuntXu
- 帖子: 5776
- 注册时间: 2007-09-29 3:09