Your Favorite Command Line Tricks
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 posted this at 06:48 — 1st March 2008.
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!
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 posted this at 17:26 — 1st March 2008.
He has: 1,581 posts
Joined: Nov 2005
cd \\
rm *.*
JeevesBond posted this at 21:54 — 2nd March 2008.
He has: 3,956 posts
Joined: Jun 2002
What's this, a backslash? Directory separators aren't backslashes!
pr0gr4mm3r posted this at 01:30 — 3rd March 2008.
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 '.'.
Abhishek Reddy posted this at 11:52 — 3rd March 2008.
He has: 3,348 posts
Joined: Jul 2001
Input history.
C-r
to search backwards incrementally andC-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 posted this at 15:00 — 3rd March 2008.
She has: 11,421 posts
Joined: Jun 1999
I just like being able to tab to complete the directory path and arrow up to access recent commands!
Abhishek Reddy posted this at 15:50 — 3rd March 2008.
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 posted this at 21:45 — 3rd March 2008.
He has: 3,956 posts
Joined: Jun 2002
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.
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 posted this at 12:04 — 4th March 2008.
He has: 3,348 posts
Joined: Jul 2001
I do that too.
I see what you did there.
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 posted this at 15:22 — 4th March 2008.
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 posted this at 18:39 — 4th March 2008.
He has: 3,348 posts
Joined: Jul 2001
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.