- 01 Mar 2012 12:21:12 and 28 May 2015 02:53:23
Here I outline some of my favorite tips and tricks for using GNU/Linux and UNIX-like command lines. Keep in mind I hate the mouse, love the command line, enjoy hacking, and study nuclear physics.
There are tons and tons of resources on Linux, so I just offer some of my ideas here, mostly for experiment collaborators who know me and see me using my laptop or other local machines in some way which appears magical.
What's your computer busy doing, anyway?
If you want live monitoring of your CPU and/or memory usage, try the command line program top, the GUI xosview, or the simple X GUI Xload. In fact, I had a dedicated work space with all of these (sadly some present settings on a new install made this difficult, and I was lazy to replicate it yet), which you can see. What you see is three frameless terminals (try hitting F10?) cleverly arranged, one running top, one that killed a process eating one of my CPU cores with ps aux | grep cpuhungry (Protip: pgrep is better) to find cpuhungry program and kill it, xosview, and xload (hidden under another transparent terminal). I didn't figure out yet the time axis units, but Xload is showing you probably the last hour of my activity (or something of that order); the bar in the middle is suggesting I have two cores...and yes sometimes I max out the entire CPU doing physics. (If you wonder how to move windows without frames, hold Alt and click anywhere on the window...please don't ask me how I discover these obscure facts, because I have no clue!)
The best feedback is your hardware fan(s). If the fan starts going and you didn't initiate some high powered code...something is wrong. Of course, UI lag is another clear clue, but I guess you know that! Either way, go see what's hogging your computer, and consider your options (kill the program or get a cup of coffee usually).
Shells can be your friend, but...
You should prefer to configure your terminal shell a lot, and even think about what shell you use. Personally I am loving zsh for a long time, although bash is basically fine by me, too. For my zsh hacks, you can check out my dotfiles which begin with 'z'.
I have a feeling people who watch me enter commands sometimes try to replicate them and get confused about my cleverness for aliases, scripts, or even invented programs that accomplish simple tasks. Here are some of my favorites:
".." is "cd .. && ls" (go up one directory and show me the contents)
"..." is "cd ../.. && ls" (go up two directories and show me the contents)
I might have iterated that to several more layers in recent years, but four or five starts to get excessive and I'd forget how far back based on my shell prompt, where I wanted to go, and counting periods past four I think.
"cdl" is "cd && clear" (give me a blank terminal in my home directory, although thought logic and command order are inverted here.)
To view ROOT Trees, I love to do 'tb' (check under Programs for TBrowser at my nucleardata.org page). Wow, 48 downloads on nucleardata.org, it must be a record! Apparently I am not the only lazy person in the world! If I get enough downloads at nucleardata, maybe I'll suggest this as an obvious inclusion to official ROOT. Anyway, compile that and toss it in your PATH.
Some other things I couldn't do via aliases I put on GitHub.
Shell hot keys you should know
In alphabetical order…
Ctrl+C: Force quit (kill) the present program, if it isn't horribly frozen. If so, see below for Z…
Ctrl+D: Exit many programs safely, or exit the shell. Easier and faster than most commands to exit or quit.
Ctrl+O: Far and away the best owing to obscurity (no one knows this) and difficulty (no option is better) to accomplish by another means. It is generally similar to hitting Enter. However, there's an additional feature in bash and zsh (definitely doesn't work in csh and tcsh, but I hate those anyway). If you Ctrl+O from your shell history (the part where you used the up arrow further than the latest command), it will execute the command, and put on the command line the next item in the shell history. This is a simple way to cycle between a series of several commands, once you browse the shell history a bit to set up the loop you want, as the command sent by Ctrl+O is also appended to the shell history. Imagine a simple case of three commands you want to send, perhaps modifying one or two of them slightly. Set up your three-item shell history, and start running Ctrl+O through it. This is basically the nebulous realm between when you don't want to type out all your commands nor write a shell script to iterate a series of actions. Amazing.
Ctrl+Z: Suspends the present program. You can use jobs to see which number it is in the shell to kill it if it was failing to reply to Ctrl+C etc, or use it for program-cycling with fg. See my favorite command line tools below for more on that.
Arbitrary top 10 command line programs
Why choose ten instead of eleven or nine? Either way, it's a number and I'm sure I can list ten in some kind of order. I don't choose this based on which commands I might use the most often, such as cd, ls, cp, etc, but more based on utility. I also don't include an editor in this list on purpose. No shells are listed because that's where you run commands from, not the commands you run in them. The order is fairly arbitrary, but was just what came to mind as I went, and may have some order of relevance though not inherently my preference (which anyway depends on the task at hand).
1. grep: By far and away my favorite command-line program. Why? For one I don't think there's anything really like it outside the command line. It solves an unimaginable number of different problems, between finding the file you want based on a regular expression (even recursively with the -R flag), piping to search the output of other programs, doing tricky shit in shell scripts, and other nameless nonsense. This program's obscure name, like most UNIX command line tools, is historical. It references some syntax from ed and effectively means global regular expression print. (These days in vi-like editors or sed, the g goes at the end, not the beginning.) Basically, use it to search files or direct input of a pipe. The first non-flag input is the term to search for (put in quotes if you have spaces, etc, it's easier); the second optional field is for the file name (omit with pipes, and use the wildcard character * liberally). I decided not to include sed or awk in my list since I find it more useful in scripting applications rather than command-line applications, but it has some equally amazing, just slightly less commonly relevant, applications.
2. screen: The terminal's terminal. This is wonderful not only for the kinds of extensions it gives to basic shell features, but for remote applications with ssh as well. As a general fact, I use this as way to tab terminal sessions, similar to the way most people use web-browsers these days. Many terminal emulators also offer tabbing features, but it seems like a pointless feature since screen was invented so much earlier, and packs in so many extra features. Feel free to grab my dotfile as a starting place, since configuring screen correctly is half the battle. Some of the additional features I like are as follows. I like having the clock displayed, since I don't have a visible taskbar with a clock (that's not an accident...it's because of screen partly and actualy monitor real-estate optimization). I like being able to scroll up, and even search easily, the terminal contents. I like being able to use keyboard hotkeys to copy terminal text easily. I like being able to suspend screen on a remote connection where the job its running isn't killed, or when I didn't want to suspend the screen session but was punted from my remote connection, was happily just a screen -R away from getting back. As for how to use screen, firstly you need to know the toggling hotkey, which is by default Ctrl+A (which I will denote as ^A as standard). This program works a bit like Emacs, where you hit one hotkey, followed by another key entry; here I won't separate the keystrokes by spaces, but assume they are pressed in sequential order and not at once. ^Ac creates a new screen session. ^An moves to the next (numeric) session. ^Ap moves to the previous (numeric) session. (^Ab moves back a session with my dotfile, since 'n' and 'b' are next to each other on a QWERTY keyboard layout.) ^A[Esc] (Escape) puts you into a scrolling mode, which allows searching (use /yoursearchterm with Enter to search from your cursor and below for yoursearchterm), paging up and down easily, and copying easily (press Space to start highlighting, scroll around, and Space to finish highlighting and copy) and pasting easily to any other session in that screen (use ^A[ to paste). It must have a huge array of other features, but these reasons alone are enough for my number two.
3. fg and jobs: This is mainly a classic coding trick I learned from the renowned Yamaguchi Hidetoshi. The essential philosophy here is how to couple fg with the Ctrl+Z shell hotkey. The concept here is that, within one terminal, you can have an editor running on some kind of code source, make some changes to the code, save, suspend (Ctrl+Z), test the code (either compiling, running, etc), then use fg to bring an (editor) back into the foreground for more editing or debugging. There's not much else to say, and several terminal windows, or screen, or a proper development environment may suit you better, but I like this way. jobs is a good way to see how many suspended editors you have running on which programs, when you really go full in to this approach. By default, fg will bring back the most recently suspended program (that's indicated by the + in jobs output). If you want to get to a specific job, use % followed by the job number for the first field of fg. If I'm working on physics analysis, since I attempt to modularize my code, I can easily get to four to seven jobs in one screen session suspended. And I can do this in several screen sessions for different code sets. Sorry but I don't really want ten terminal windows, nor ten text editors, opened all at once, nor several IDE sessions for several different programs. fg will get the job done! (Har har)
4. bc: Called the basic calculator, that's all it is. It's just a command-line method to calculate. Please check my dotfiles for a nice math extension from Steffen Brinkmann. Also you might as well alias bc to bc -lq which makes it do long calculations (did you want to divide for an integer result ever?!) and quiet (do you want to see the version and the license info each time you load it?!). The only trick you may want to know is that . (dot) is the previous result. Then you can do a calculation, hit enter, and dot it to another calculation. This allows you to not only easily see multiple steps of a calculation, but to avoid too many braces for orders of operations. Are there other ways to make simple calculations? Yes. Are there others I prefer more? No. Although this can be used via scripting, if you want to script numbers in the shell, use awk instead!
5. feh and gqview: feh is more properly a command-line tool than gqview but I think they both count, and they are image viewing tools. Although they are both capable of making GUI pop-outs, considering the task at present is image viewing, this is a requirement for the desired function. feh just opens the given image only, in the most simplistic window, which can be escaped easily to get back to the command-line of preference. It can also be run without opening the window to obtain some information, like an image's size, resolution, or other demographics (perfect for scripting applications). gqview is an ideal program for parsing and browsing images from the command line, as it will open a GUI with a simple file-structure hierarchy, which contains only images, and showing those images, which can easily be scrolled through with your favorite keys. Both of these have several obvious hotkeys for enlarging (+) or full-screen mode, as you may desire. There are plenty of full-blown GUIs which may do better, but for viewing and/or getting information about pictures quickly and easily from the command-line, these are my essential go-to tools. Besides, a list of command-line programs would be incomplete without some ideas proving that the command line can, in fact, do everything, including things that require graphics.
6. sudo: Because I decided to include something canonical I can't live without. If you like to live on the edge, after putting daid into visudo, just toss something like daid ALL=(ALL) NOPASSWD: ALL. It's not exactly what I'd call secure, but if anyone (or at least me) has physical access to your PC, I can chroot you straight to hell, anyway, changing any passwords, viewing any files, and generally having a field day in the park on your computer. And if some nefarious person accessed your user remotely by whatever means for any reason, I guess hacking in as root wasn't going to be that much more of a burden. Protip: Turn off sshd when you aren't using it, and set your port to something wild besides 22.
7. mplayer: Not just the best command-line media player, but pretty much one of the best ever for local audio and video. No-nonsense, low overhead, with good hotkeys, and programmable. If you need more than that, check out the description and code for my mplall script, which makes it even better.
8. alsamixer: Just a simple way to set your audio levels. See my page on alsamixer transparency for any additional geek points.
9. pdftk and imagemagick: This is what I use to manipulate existing pdf and image files when it's not so complicated as to require really modifying them. pdftk is described by the developers as something like a stapler, scissors, and glue for any pdfs. Make a pdf into files for each one page! Put lots of different pdf items together as one! Rotate everything! And so much more! I'd give examples, but sometimes they change the nomenclature, and it's slightly obtuse in any case, and I'm getting tired. Use google I guess, and assuming commands like "burst" and "cat" are good ones. imagemagick is an unusual program in that I've only ever used the convert program it installs. You hardly need to care how convert program works. Give it the first field as some visual document you have (even a pdf) and the second as the new file you'd like to have, with its name and file type just as you want, and it's fucking magick! It has a slew of features, but I only ever cared to change the -density for resolution.
10. profanity: I figured even if I set out to make a useful list, it should also be an honest list. ProfanityFE is a great command line Front End for playing a text-based game I love, GemStone IV. It's a text-based fantasy game, and in my opinion the best game ever created. It's how I learned to type, and it's a lot of fun, at any hour of the day. Tillman is widely known within that game for creating a scripting engine called lich, and this was a Front End he made for playing the game on a simple GNU/Linux terminal. It's simpler than most, but it runs easily on my computer, and is no-bullshit. Also, computing and gaming both make me swear, and it's a great name!
/ / (__)
/ PhD \ (oO)
/ | /| daid ||
* || ||------||