Click here to register.
      

Useful Shell Stuff

Design Squid
Useful Shell Stuff
preaction · 7/14/2009 12:19 am

The Linux command line is a source of constant amazement to me. Not a week goes by where I don't find some new thing to make my job (and/or life) easier. Today I'd like to share a few with you.

BASH-isms

  • !?<pattern>
    Search the command history for <pattern> and run that entry again. So if you find yourself running "wreservice.pl --restart modperl" often, you can instead run it once and then use "!?restart" to run it again. 
  • Ctrl+A
    Go to the beginning of the command-line. Useful to fix a previous command that failed. I've used bash for almost 6 years without knowing this little gem. How many hours of my life have I wasted holding down the left-arrow key?
  • Ctrl+Z and fg
    Ctrl+Z halts a running command and lets you run something else. After you're done, you can use "fg" to bring your halted command back to the foreground. Need to temporarily stop a long-running compile to fix something? Have a process taking too much load while the server is being taxed? Halt it for a little while.
  • &, jobs, and fg
    Putting & at the end of a command forces the command to run in the background. Later, you can use jobs to see if it's waiting for input or wants to show you something, and fg %<#> to bring the command back into the foreground to see output or provide input.

Programs

  • nohup
    Ignore SIGHUP, which is a fancy way of saying "Keep my program running even if this terminal gets disconnected (hang up)." By combining this with &, you can immediately run a long-running process that will stay running after you exit. 
  • screen
    Tthe most useful program ever. Screen is a terminal multiplexer, a fancy way to say "Let me run multiple terminals in one." When you run screen, it doesn't appear to change anything, but once inside you can hit Ctrl+A C to create a new screen. Then, you can switch to your previous screen with Ctrl+A p. You can see a list of all screens you created with Ctrl+A " (that's a double-quote). Most importantly, you can detach from screen and leave everything running with Ctrl+A D. Later, you can reattach to screen by using screen -R. Read a tutorial on screen at kuro5hin.
  • kibitz
    Potentially the second most-useful program ever. kibitz lets you easily share your terminal with multiple people at the same time. The most important use of kibitz is to get help when playing nethack.
  • tee
    Copy standard output to a file, while also showing it on standard output. This lets you keep a log of a program's output and watch it at the same time.
  • basename
    Output the basename of a file. Given /data/WebGUI/etc/www.example.com.conf, basename would output "www.example.com.conf". This makes it easy to write shell scripts that run a WebGUI utility script against all WebGUI configuration files, say to import a file into all the sites on your server. 
  • pbcopy and pbpaste
    Mac OS X only.
     pbcopy puts standard input into the pasteboard (also called the clipboard). pbpaste puts the pasteboard into standard output. Using this, you can copy text from safari and process it using sed or cut or anything else on the command-line.
  • cowsay
    Make a cow say something! 
  • figlet
    Make big block letters! Combine with cowsay to annoy people on IRC! 
This is far from the end of the usefulness one can get out of a good command-line. Expect more from me on this topic in the future!

Re: Useful Shell Stuff·
patspam · 7/14/2009 3:47 am
Re: Useful Shell Stuff·
martink · 7/15/2009 2:42 am

If you like !?, you'll probably gonna love this. In your /etc/inputrc or ./.inputrc ad the following lines:

"^[[5~": history-search-backward    # Previous
"^[[6~": history-search-forward     # Next

The ^[ are control cahracters.

What this does is bind searching through your bash_histrory to pg-up an pg-dn.

Now suppose you did a

  1. vim WebGUI/VersionTag.pm
  2. vim WebGUI/Asset/Wobject/Collaboration.pm
  3. sudo /data/wre/sbin/wreservice --restart modperl
  4. vim .inputrc
  5. ls -l

Want to do the sudo command again? Type s<pg-up><enter>

Want to edit VersionTag again? Type v<pg-up><pg-up><pg-up><enter>

<pg-dn> crawls through the list in the other direction.

--
Martin Kamerbeek / Oqapi Software

·
Stick
Lock
Subscribe