Your Favorite Command Line Tricks

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Inspired by Megan's thread, let's hear what your favorite shell command trick is.

My favorite is creating a file with output redirection.

> my-new-file
'
I cringe when I see someone typing out touch (what a waste of a keystroke!)

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

Curse you! I didn't know about that, I've been using touch. Gosh, that's seconds of my life down the drain! Laughing out loud

find . -maxdepth 1 -name '*.tar.gz' -type f -print0 | xargs -0 --max-lines=1 tar -xzf

Useful if you have a directory with some tar.gz files in it.

a Padded Cell our articles site!

greg's picture

He has: 1,581 posts

Joined: Nov 2005

cd \\
rm *.* Bomb

Evil

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

cd \\'

What's this, a backslash? Directory separators aren't backslashes! Laughing out loud

pr0gr4mm3r's picture

He has: 1,502 posts

Joined: Sep 2006

...and rm *.* doesn't remove all files on the filesystem. There are a lot of files with no extension, and hence no '.'. Smiling

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

Input history. C-r to search backwards incrementally and C-s to search forwards. In most long sessions in the command line, I'm working on an iterative task, say repeatedly starting and stopping a server, or building a program, etc. The commands are too transient to warrant aliasing or scripting because I probably won't want in future sessions. But I don't want to keep tapping the up-key for each command -- or worse, re-type it.

Of course, bash does remember inputs even in a future session, which can be handy. My rule is that if I find myself resorting to the input history for a command in more than two successive sessions, I want it in a script or alias.

The other big feature along the same lines is history expansions. !! refers to the previous command, !-n refers to the nth command back, !$ refers to the last argument of the previous command, and so on. Makes it easy to string together a command like so:

$ ./configure --some-long-options=inconvenient --we-would-hate-retyping=this
  [much output]
$ make some-target
$ make another-target
  [error due to incompatible library]
$ aptitude install new-library
$ !./conf? && !?some? && !?another?
  [expands to ./configure... && make some-target && make another-target]

More on these in the Definitive Guide to Bash Command Line History.

Megan's picture

She has: 11,421 posts

Joined: Jun 1999

Shocked I just like being able to tab to complete the directory path and arrow up to access recent commands!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

That's pretty much all I need for the majority of my time in the command line, too. Say, 60%.

In the other 40% of the time I carry out highly repetitious, cyclical tasks. Like incrementally building, testing, and rebuilding a program. Or repeatedly shutting down, reconfiguring, and starting up a server. That's when the fancy shortcuts really become useful.

JeevesBond's picture

He has: 3,956 posts

Joined: Jun 2002

In the other 40% of the time I carry out highly repetitious, cyclical tasks.

Hehe, I read this as: 'I carry out highly repetitious, cynical tasks.'

Like incrementally posting more and more depressed comments on the end of the world is nigh forums. Or repeatedly bashing ones head against the keyboard. That's when hating the world really becomes useful. Laughing out loud

But seriously, I had totally forgotten about those search commands, I'd actually started to think I was just dreaming of their existence! More on topic (about aliases), I discovered these the other day myself. They allow really long commands to be shortened to single words, for example:

alias checkouthead='cvs -z6 -d:pserver:anonymous:[email protected]:/cvs/drupal checkout drupal'

Gets rid of all the tedious typing to checkout Drupal HEAD, reducing it to: checkouthead. These can be switched on/added in ~/.bashrc

a Padded Cell our articles site!

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

JeevesBond wrote: Hehe, I read this as: 'I carry out highly repetitious, cynical tasks.'

Like incrementally posting more and more depressed comments on the end of the world is nigh forums.

I do that too. Sticking out tongue

JeevesBond wrote: Or repeatedly bashing ones head against the keyboard.

I see what you did there.

JeevesBond wrote: More on topic (about aliases), I discovered these the other day myself. They allow really long commands to be shortened to single words

Definitely one of the most useful features. My .bashrc is filled with aliases to the point that I'm sometimes uncomfortable in foreign shells without my customisations. Thankfully dotfiles are easily portable!

teammatt3's picture

He has: 2,102 posts

Joined: Sep 2003

Just found out this one, "cd -". If you start in /root and change dirs over to /home/matt, cd - will take you to /root. Typing it again will take you back to /home/matt. It's like a back/forward button. I haven't really used it, but it might be useful.

I don't use aliases very much, I write shell scripts (I always forget the syntax for aliases). But you bring up the portability of dot files. Every shell script I have could be put into .bashrc as an alias, right? That seems like a better idea because I could just bring over my bashrc file and not my whole bin directory.

Abhishek Reddy's picture

He has: 3,348 posts

Joined: Jul 2001

teammatt3 wrote: But you bring up the portability of dot files. Every shell script I have could be put into .bashrc as an alias, right? That seems like a better idea because I could just bring over my bashrc file and not my whole bin directory.

Exactly. If it's something you're interesting in porting to multiple systems, certainly try setting it up as an alias.

Want to join the discussion? Create an account or log in if you already have one. Joining is fast, free and painless! We’ll even whisk you back here when you’ve finished.