Live data from Hacker News

Linux utils that you might not know

shiroyasha.io

121–130 of 159 posts

Re: Linux utils that you might not know

#121
Although I frequently use cal, the article mentioned cal -3 which will be useful near the end of each month. Previously I always displayed the calendar for the whole year or next month instead.

Going through the core utils manual [1], I found a couple other useful looking commands I didn't know about. Previously I used awk in bash scripts primarily for printf, but apparently it's part of coreutils too. The timeout command also looks useful.

1. https://www.gnu.org/software/coreutils/manual/coreutils.html

Re: Linux utils that you might not know

#122
post #101

Anyone here will probably enjoy checking out commandlinefu: http://commandlinefu.com Especially looking down the list of all time greats: http://www.commandlinefu.com/commands/browse/sort-by-votes

Bringing this on a small tangent, but I've never been comfortable with 'sudo !!' and I can't really articulate why aside from wanting to be as explicit as possible. , , type sudo and enter is nearly as quick and much more explicit for me.

My zsh is set up (don't know if this is a default or not; I'm using Oh My Zsh) to not run the command if you use any history expansions in a command, instead it'll give you a new prompt line with the substitutions already filled in. That way you can check that the command is correct before running it.

Re: Linux utils that you might not know

#123
post #88

Earlier quoted context omitted.

A large proportion (if not the majority) of the world starts their calanders on Monday. While the ordering is arbitrary, I always thought of the weekend as a single block of days so it's odd to say that the week ends in the middle of the weekend (rather than at the end of the week end ).

I always saw "weekend" in the same way as "book end", that is, that one is at the beginning of the week (one starting end), and one is at the conclusion of the week (the finishing end). Because our weeks come successively, the end of one is immediately followed by the beginning of the next, hence the "block" called the "weekend".

I'd never thought of it that way. I still think it's odd because book ends aren't made of chapters of a book (and weekends are days like any other).

All-in-all it's arbitrary anyway. :P https://xkcd.com/1073/

Re: Linux utils that you might not know

#124
post #76

Earlier quoted context omitted.

Although these tools are great, I'm afraid these are of limited use, as these aren't preinstalled but must be explicitly installed. For personal use these are great. But if you write a shell script to be executed on different machines where you can't install anything, these tools won't be available to you. And if you don't have that requirement, i.e. you can install everything, just install appropriate Perl/Python/Ru…

Why assume python? It's huge compared to bash or perl. Lots of minimal configuration operational systems won't have python. More likely than go/ruby/node though.

Is Python that much bigger than Perl?

And from your list, Go doesn't really belong there, it outputs statically linked binaries. You can just plonk them anywhere.

Re: Linux utils that you might not know

#125
post #100

Anyone here will probably enjoy checking out commandlinefu: http://commandlinefu.com Especially looking down the list of all time greats: http://www.commandlinefu.com/commands/browse/sort-by-votes

Thanks for the second link. I go there every few weeks but never thought to check out the top voted. ...In hindsight, of course... The gem I just plucked out is something I've been curious about for a while but never looked up: CTRL-X e The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR [then run it when saved] Similar to `fc` except you don't need t…

Also works in vi mode (I think the default for bash is emacs mode - for editing commands, that is):

Run:

set -o vi

once after you log in (for ksh / bash and compatible shells only, maybe, not sure about csh), or (better) put that line in your .bashrc or similar startup file, so it runs each time you log in. (I used to use "ksh -o vi" earlier, before I knew about "set -o vi" or before it existed, but in that case, it has to be the last line in your startup file, otherwise the other lines below will not run until you exit that (sub)shell.)

Then, when typing a command at the command line, just press ESC then v ; it does the same as what you said.

You can also do ESC :q! (in the editor, if it is vi) to quit without running the command you just edited, or save the command to another file for editing later at leisure, then quit without running it right now.

In fact, "set -o vi" also enables limited editing in vi mode right on the command-line, after you press ESC - you can use the command-mode commands of vi (h, l, b, w, f, F, and more) to move around, change characters or words, can also overwrite or append or insert text, etc.

You can even use / and ? and n and N to search backward and forwards in the commmand history to find (by substring) a previous command, to edit it. Once you find the right commmand, just press v.

Great for productivity.

Re: Linux utils that you might not know

#126

Even as a decades long 'nix user, I had never used join until recently, which joins lines of two files on a common field. I used to turn to fgrep or a Python script to solve a whole class of problems join can help solve on the command-line.

I recently got to use `join`. However, without reading documentation on outer joins. I went ahead and wrote my own version:

  join -t$'\t' 
Then I stumbled on an easier version with using just join

  join -a1 -a2 -o auto f1 f2
There are really too many unexplored things on the linux command line for a typical dev.

Re: Linux utils that you might not know

#127
post #108

Earlier quoted context omitted.

You can use comm from coreutils for that too: `comm -1 file1 file2`

Yep, but you'll have to read the man everytime to figure out if you need -1, -2 or -3, or a combination of those. combine gives you a better interface with boolean operators you know.

The 1st column is the lines unique to the 1st file, the 2nd column is the lines unique to the 2nd file, and the 3rd one is the lines that are not unique. -1, -2, -3 allow to disable those columns. Pretty easy to remember.

Ah, HN downvoting neutral comments again and I don't like it, but I have a policy of not upvoting anything out of pity.

Re: Linux utils that you might not know

#128
post #125
post #100

Earlier quoted context omitted.

Thanks for the second link. I go there every few weeks but never thought to check out the top voted. ...In hindsight, of course... The gem I just plucked out is something I've been curious about for a while but never looked up: CTRL-X e The shell will take what you've written on the command line thus far and paste it into the editor specified by $EDITOR [then run it when saved] Similar to `fc` except you don't need t…

Also works in vi mode (I think the default for bash is emacs mode - for editing commands, that is): Run: set -o vi once after you log in (for ksh / bash and compatible shells only, maybe, not sure about csh), or (better) put that line in your .bashrc or similar startup file, so it runs each time you log in. (I used to use "ksh -o vi" earlier, before I knew about "set -o vi" or before it existed, but in that case, it…

Awesome, thanks for the tip!

Re: Linux utils that you might not know

#129
post #127
post #108

Earlier quoted context omitted.

Yep, but you'll have to read the man everytime to figure out if you need -1, -2 or -3, or a combination of those. combine gives you a better interface with boolean operators you know.

The 1st column is the lines unique to the 1st file, the 2nd column is the lines unique to the 2nd file, and the 3rd one is the lines that are not unique. -1, -2, -3 allow to disable those columns. Pretty easy to remember. Ah, HN downvoting neutral comments again and I don't like it, but I have a policy of not upvoting anything out of pity.

Ahaha, then I guess I don't use it that often. I'll try to remember this mnemonic.

PS: do not worry about karma ^^

Re: Linux utils that you might not know

#130
post #89
post #72

My favorite: basename Returns just the filename plus extensions e.g. basename a/b/c/d/foo.php returns foo.php Very useful for shell scripting.

With shell one can just use "${path_var##*/}" to get the base name from the string stored in $path_var. This also works if $path_var is just a base name already without any slashes. EDIT: fixed the substitution.

> With shell one can just use "${path_var##*/}"

The reason I use python whenever I need something "shell" like. Cryptic symbol salad that makes code golf fanatics green with envy.

Post reply on HN