search by tags

for the user

adventures into the land of the command line

groovy file manipulation tricks

so a couple of times, when using ssh for loops and such, i’ve wanted to substitute a line in a file or append a line to a file.. a file that is owned by the root user

so just say you try something like this

$ sudo echo 'someuser ALL=(ALL) NOPASSWD: ALL' >> /etc/sudoers

you’ll find you get this response

-bash: /etc/sudoers: Permission denied

damn it! what the hell? how come i can’t? all of these you might be thinking, and rightfully so! nevertheless, you can get it to work like this

$ sudo echo 'someuser ALL=(ALL) NOPASSWD: ALL' | sudo tee -a /etc/sudoers

remember to include to -a switch to append, unless you want to wipe the entire file muahahaha

another groovy command i like is sed, which allows you to substitute strings in files. i think you can get really complicated with sed, but for me, this is the most useful thing i use it for in general most of the time

$ sudo sed -ibak 's/some string i want to replace/the replacement string/' /path/to/some/file.bam

the -i switch will edit the actual file in place, and if you add characters to the -i switch, like ’-ibak’, it’ll create a backup first, with the characters appended to the end of the filename

so a file like

file.conf

will have a backup created with the name

file.confbak

these two commands coupled with ssh for loops can potentially save any budding sysadmin, minutes if not HOURS of manual labour

tee and biscuits for everyone!