tags:

views:

317

answers:

9

Do you know any resources that teach to good habits of working in UNIX command line?

EDIT: I don't mean general books about shell or man pages. I mean the things that you can only see watching professionals working with command line. For example when changing frequently between two directories they use "pushd" command, when repeating a command they use "history". I can read about these commands but I want to make it a habit to use them effectively.

+1  A: 

stackoverflow.com esp. the bash tag ;-) (and of course the bash man page)

flolo
+3  A: 

For the more advanced stuff This seems to be fairly comprehensive

BCS
+7  A: 

I am speaking out of my own experience so it may not apply to you;

The best way to be efficient is actually using it on a daily basis, instead of using graphical tools even if they make look things easy. You will then become aware of most common tasks you care about, and instead of trying to grok it at once, you get a fairly good starting point to start learning. Man pages are the first thing to look at, but there will be non-obvious tricks which you need to search anyway. Knowing what you exactly want, infinitely increases probability of finding it.

For example, you can find how to search all mp3 files easier in man page of "find" than how to deal with files in general (where to start?).

artificialidiot
Indeed. That is the *best* way.
ayaz
+3  A: 

this is a great resource: "Rute User's Tutorial and Exposition" (http://rute.2038bug.com/index.html.gz)

Ray Tayek
+1  A: 

If you want things that you can "only see watching professionals working with command line," then you've answered your own question: Watch professionals working with the command line. I don't personally find that very useful unless the other person is doing the same thing multiple times; it's hard to pick something up after just one session because it's hard to watch the screen and the keyboard at the same time.

I think the key is to not try to become an expert right away. Just use the command line frequently, and be aware that you might not be using it as well as you could, but don't let that discourage you from using it anyway.

Browse through the man page of your shell, and through lists of tips, not with the goal of memorizing everything in them, but just to pick out a couple of things to try out. Skim through until something catches your eye and makes you think, "Gee, that sounds useful." Then try it out. Not everything is going to be useful immediately; you might have to wait a while before you encounter a situation where you can try something out. Maybe you could write down some things on Post-It notes by your desk to remind you that certain feats are possible, so when you encounter a situation where a more obscure feature could be handy, you'll be more likely to remember to try it.

Rob Kennedy
+4  A: 

Some common bash command line actions, not in order:

  • Command line editing: you'll want to be good with emacs or vi and apply that to editing your commands.
  • Completion: use TAB to expand file names and paths.
    • note: There is a huge set of file, command, and history completion functions, and it is configurable. Big topic.
  • "cd -" : go back to the last directory you were in
  • ~ = home directory (or ~user for users home dir)
  • "ESC ." : expands to the final arg from the previous command
  • "!string" : execute the last command starting with string
  • learn find, grep, sed, piping "|" and redirection ">". You'll often combine these to do useful things.
  • Loops from the shell prompt, e.g. "for" loop - to do repetitive actions
  • Learn your regular expressions! Often used for matching files.
    • example: ls x[0-5]*.{zip,tar} = list files starting with x, followed by a number 0 through 5, followed by any string ending in .zip or .tar

If possible ask others for their favorite tricks, read the manual, and practice.

Markc
+1  A: 

Frankly, it's impossible to learn this stuff in a vacuum. You need to have problems to solve.

While it certainly helps to have familiarity with the tools available (of which there are a myriad), "learning" it requires applying it. And applying it requires "real" problems to solve.

For example, the skillset of a System Admin may be different from someone who works with databases because their roles are different.

I use them for data processing, using mostly one off files. /tmp/x.sh and /tmp/x.x are worn bare in the directory folder.

My hammers tend to lean towards: ls, find, sort, sed, vi, awk, grep, and comm. Combined with simple shell scripting like: for i in cat /tmp/list; do .. done

But I do a lot of ETL work, and very few script files, which is why my shell scripting skills are so weak.

I do rely on one script, however:

#!/bin/sh
# latest -- show latest files
ls -lt $@ | head

As 95% of the time the files I'm working on are in the top 10 latest files. And "latest *.txt" works a peach.

So, bottom line, you need problems to solve. You need to learn the 'man' command, man -k is nice to find things. You also need to leverage the "See Also" at the bottom of most man pages. That's a treasure trove of "I didn't know you could do that".

Then, just start solving problems. Start figuring out "what would be nice to have" and then see if it exists (it very well may). If not, awk, perl, or python can make those "nice to haves" out of thin air.

Will Hartung
A: 

Join a LUG. That is where I learned most things early on. Ask the organizers to do a "Bash Tips And Tricks Night".

Deft shell users love to show off.

Nathan Powell
A: 

apropos is a really good tool for this sort of thing. Whenever you find yourself unsure of the best way to do something, or wishing you weren't repeating yourself, just use apropos with a keyword or two to find other commands that can help. In distros like debian, you can also install web-based help tools that search all of the manuals available on the system: texinfo, man pages, html, and pdf etc.

Aside from that, yep, read your shell's manual right through at least once --- preferably, go back to repeatedly it as you learn more, reach limits and want to be more efficient.

The join a LUG idea is also good; you'll definitely learn from others' demos.

Lee B