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

  • Post a new comment


    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded