How to learn work effectively with Unix CLI - unix

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.

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?).

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.

For the more advanced stuff This seems to be fairly comprehensive

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

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

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.

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.

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.

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.

Related

Zsh: tab completion for files not in the current directory?

I'm new to zsh and I'm trying to figure out how to get tab completion to work so that when I typed part of the name of a file that's not in the current directory zsh will complete it.
The idea is that I have some scripts in ~ and c:\MyStuff\bin and I'd love for zsh to try and complete those (executable) scripts when I'm in other directories.
Being able to complete files that are anywhere in my path would be nice, but if it's easier to complete files using a list of directories set in my .zshrc, well, that would work fine too.
If anyone has any pointers for resources about how to do this, or even advice like "This will / won't work in zsh" that would be great. zsh seems open-ended enough that it ought to be able to do this AND ALSO I've searched long enough without finding anything that I wouldn't be surprised if there's nothing at the end of this rabbit hole :)
Thanks in advance!
So I decided to invest the time and actually try and read the (very, very thorough) zsh completion docs. There's a lot there, including a section which says that zsh actually does this out of the box:
8 Command Execution
"...the shell searches each element of $path for a directory containing an executable file by that name..."
Turns out this does work for me, it just takes a long time (more than a second or two) and so I thought it wasn't working.
Next up: looking at why it's taking so long - perhaps it's my very long $path variable :)

Compile Finite State Machine to UML(-like) Diagram

Every Python developer knows tools like Sphinx. You write some text in a markup language, write make in the shell and let some compilers do their job. In the end you get beautiful HTML or PDF.
I am looking for something like that, just for Finite State Machines, e.g. I put SCXML into a file (with a GUI or manually with VIM as I desire) start a compiler and out comes a picture file format that i can use however I please and that looks good even if I don't know what I am doing. Example:
$ vim my-fsm.scxml
$ scxml2svg my-fsm.scxml
writing file...
finished writing my-fsm.svg
$
The closest I got so far is using various Eclipse plugins (years ago, dislike huge IDEs), draw.io or what I am using now: Umlet. Even Umlet has problems, though. For example it doesn't support the workflow I am used to (write text files, start compiler, see beautiful result). The results are often also suboptimal, because the engine is actually quite simple. But everywhere I look for a more useful alternative (python wiki, other SO questions, tools) I still don't find a simple compiler.
Now I would be really happy if anybody would know such a compiler. If not possible a FOSS GUI editors with PNG/SVG export would also be okay.
GraphViz has a file format which can be written manually and compiled to different picture formats.
I wrote some tools to do this: http://goo.gl/V97ft

How can I quickly search my code using unix?

I frequently search for a token, perhaps a function name, throughout my codebase. My traditional method would be to grep for the term itself. However, the codebase is so large that I can't do this efficiently (it takes minutes).
Is there a way to do this efficiently?
ack (which ignores irrelevent files such as revision control files) is still too slow. ctags only finds declarations, which isn't what I need. I thought something like strigi might work, but I haven't tried it.
I'm on linux, using vim and the GNU toolchain, on a largely C++ codebase.
Use find and fgrep. A decent find will restrict the set to files matching some set of criteria, and fgrep will search within each file. Note that fgrep will be faster than grep.
You could also use an IDE's code indexing features. For example, Eclipse will allow you to go to a function's declaration, and it builds the relevant set in the background while you work. Even vim 7 has some such features, although I don't remember exactly what it does beyond code completion.

What interesting things can be done with dcop?

I have been using UNIX for years and the dcop command only recently came to my attention. What is its primary purpose and is using it in any process a sign of poor design?
is using it in any process a sign of poor design?
Probably not, as long as you're using DCOP to perform some task that would ordinarily be done with a mouse click or hotkey. It's certainly way better design than trying to control the mouse to do what you want to do with the program!
But if you use DCOP to do something really simple that could be done directly with a shell command or a few lines of C code, without invoking a KDE application at all, it's probably better to just do it the direct way. (e.g. don't use DCOP to tell Konqueror to remove a directory tree, just run rm -rf)
I suspect it mainly exists because everything in UNIX ought to be accessible from the command line.
The Wikipedia article on dcop is worth a read.
It's great for any scripts to use to control processes, there's also good information on controlling Amarok via dcop.

Do you know of any "best practice" or "what works" vi tutorial for programmers?

There are thousands of vi tutorials on the web, most of them generically listing all the commands. There are even videos on youtube which show basic functionality.
But does anyone know of a vi tutorial which focuses on the needs of programmers?
For example when I program in Perl with vi, moving to the "next paragraph" is meaningless.
I want to know which commands seasoned vi users combine to e.g:
copy everything inside of parentheses
copy a function
copy and paste a variable (e.g. 2yw)
etc.
I am sure there are lots of functions using multiple-file capability, and the maps, macros, reading in of files for template code, regular expression search, jumping to functions, perhaps minimal code completion, or other features that emulate what programmers have gotten used to in Visual Studio and Eclipse, etc.
A nice collection of vimtips.
And the best Vim cheatsheet around.
I just ended up reading the vim manual a few times, over the years, picking up useful features on each iteration.
One thing that really made vim work for me as a perl IDE was starting to use tags, as explained here: http://www.vim.org/tips/tip.php?tip_id=94. Using the pltags script that ships with vim, you can jump around between modules to find your functions, methods, etc.
If you are a beginner, vimtutor would be a good way to start with. (Type vimtutor on your shell and get going).
And once you get hold of the basics of vim, you can look around web and figure out things for yourself.
This and this may be an interesting read.

Resources