punkwalrus (punkwalrus) wrote,

Some bash command line tricks I wish I would remember

I have these tacked up in my pod. But I am leaving my company after 7+ years, and packing up. For some reason, I can never remember the following, so I have them tacked up to remind me and save some significant time. I will most likely lose this slip of paper. Doh.

I started a vi session, forgot to do it as root, and now I can't save it
:w !sudo tee %

Dammit, this became a real issue when I started implementing sudo in my daily life. You need sudo (with correct permissions to the current account) and tee installed, which these days, is real common.

I want to run a long, complex-assed command again, but with one minor change

I found myself doing this with some complicated for-while loops at the command line during DNS dig queries. !! (pronounced, "bang bang" ...really) is also useful for "run last command" when you get into sudo problems.
[you@localhost]$ make sandwich
You do not have permissions to do this.
[you@localhost]$ sudo !!
Enter sudo password:

All set! Note: ^foo^bar also works on MOST systems, but it only works on the FIRST match, and the rest are ignored.

I did an ls to a directory, now I want to cd to it
cd [esc] .

That's the command, the escape key, and a period. It will fill in the last parameter. !$ (bang dollar-sign) also works.

How to I write to dev null again? Fucking hate crontab reports.
 [command] > /dev/null 2>&1

I forget the order of the 2 and 1 and & symbol all the time

Maybe this will help others.
Tags: bash, cli, computer, tech

  • 158 Days with no sugar

    Well, day 158. Thought I'd tell you how it was going. My first big hurdle was Balticon at the end of May, which came and went with little fuss. I…

  • More on sugar-free endurance test

    Like I said in my last post, I am not sure where I am going with this. I kind of expected this to be harder. That being said, I kind of don't want to…

  • Day 35 with no sugar

    I have had odd cravings. Like pineapple juice. I used to hate pineapple juice as a kid, and only barely tolerated it as an adult. Sour and bitter, I…

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded