ibm.com — Adopt 10 good habits that improve your UNIX? command line efficiency -- and break away from bad usage patterns in the process. This article takes you step-by-step through several good, but too often neglected, techniques for command-line operations.
Aug 25, 2008 View in Crawl 4
onetruedabeAug 26, 2008
There's certainly more than one way to do it: prompt$ grep ERROR !$Have you ever been on a system with no "/bin" ? I don't remember why, but one night I had to diagnose a machine and all I had were shell builtins(!). No "cat", no "ls".So, in case you ever need it, you can simulate "ls" using: prompt$ echo * # rather intuitiveand simulate "cat" with: prompt$ while read i; do echo "$i"; done < filename.txt
killercoderAug 26, 2008
Use Ctrl+R to search backwards for a command instead of the up and down keys. This is the command I find myself using the most!Also use "bang" shortcuts whenever you can e.g !!, !$, !^, !$:h, !$:t etc
arkayceeAug 26, 2008
Oh and I almost forgot to explain why "cat" -- sometimes I'm refining the things to remove with the grep -v and it's easier to, as I'm creating a script, modify or remove any grep -v part of the pipe (including the very first one). My scripts tend to look like:cat ${FILENAME} | grep -v ^SUCCESS | grep -v ^INFORMATION | ...Now it's easier for me to be consistent if I decide to, say, throw something before removing the "SUCCESS" lines (say I find something that would remove 95% of the lines that should go before it).Of course, when I feel like the script is finalized, I may go back and remove the first cat and replace it with a grep -v .
arkayceeAug 26, 2008
Sometimes better yet, bash lets you maintain a stack of directories you can push to and pop from (i keep forgetting about this myself):cd /startdirpushd /newdir... (do some stuff)pushd /newerdir... (do some more stuff stuff)popd(now you're in /newdir again)popd(now you're back to /startdir)dirs(would show you the stack of directories at any point).For more:man pushdman popdman dirs
mrbitchAug 27, 2008
RE: " Here's a simple one: "cd -" to goto the previous directory you were in, and "cd -" again to jump back where you were... "Ok, that is a very useful tip, thankyou.
jstandenAug 29, 2008
I think for #2 and #3 on your list (which is great, btw) you could elaborate with CTRL+R history partial searching. If you know a few unique characters from a command you type all the time, it's a huge time-saver. UP/DOWN history is dead to me. ;)
jstandenAug 29, 2008
@pandaroIf you get too paranoid you could always wrap a long directory in double quotes too:# rm -Rvf "/usr /local/etc/postfix"That will prevent the errant space from ruining your day. ;)
jstandenAug 29, 2008
It's probably also worth mentioning pushd/popd, which fly under the radar. If you need to jump around to a specific set of directories to perform a common task you can 'pushd' them all into the stack and 'popd' to move through them. To clear the stack you can use 'dirs -c'.