Page 2 of 2 FirstFirst 12
Results 11 to 12 of 12

Thread: Bash Cheet Sheets Share/Save - My123World.Com!

  1. #11
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1

    Vim "Write with Sudo" Trick

    Hello all,

    I'm pretty sure we all have been there when we opened a root owned file with vim and didn't use sudo. The file is opened in readonly mode and you've already made the changes, but you can't save the changes. Here's a simple trick to keep the changes you've made,

    When in insert mode in vim, press 'Esc' key to enter the command mode and enter the following command
    Code:
    :w !sudo tee %
    Here's the explanation part for the curious ones.
    http://stackoverflow.com/questions/2...078429#7078429

    Hope it helps.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

  2. #12
    Garage Member
    Join Date
    Aug 2012
    Location
    India
    Posts
    97
    Blog Entries
    1

    'echo' to a Privileged File

    Hello,

    There are times when we just want add a single line in some file. So, instead of opening editor, we simply can use "echo 'your text' >> somefile". However, this fails when you try to "sudo echo" into a privileged file. So, here are two ways to do that,

    Code:
    c0dist@ubuntu ~ $ echo ' echo "#damn" >> /etc/resolv.conf ' | sudo bash
    
    OR
    
    c0dist@ubuntu ~ $ echo '# blah' | sudo tee --append /etc/resolv.conf # This also prints the changed content on screen.
    
    OR
    
    c0dist@ubuntu ~ $ echo '# blah' | sudo tee --append /etc/resolv.conf > /dev/null # This won't print anything on console.
    Source of last two tricks - http://stackoverflow.com/questions/8.../550808#550808
    Can't remember where I read the first trick but it's saved in my brain.

    Regards,
    c0dist.
    Anyone who stops learning is old, whether at twenty or eighty. Anyone who
    keeps learning stays young. The greatest thing in life is to keep your mind young.
    - Henry Ford

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •