Live data from Hacker News

What's the one Linux command you wish you knew years ago?

reddit.com

81–90 of 216 posts

Re: What's the one Linux command you wish you knew years ago?

#81

these are great ones! But here is mine that I recently learned: sort -h compare human readable numbers (e.g., 2K 1G)

Canonical example: du -h | sort -h

Sort to the same output file: sort -o foo.txt foo.txt

You can't do sort foo > foo because you'll be left with a blank file.

Re: What's the one Linux command you wish you knew years ago?

#82
post #76
post #52

Earlier quoted context omitted.

Many comments seem to be shell- and bash-related. Perhaps the thread exposes how difficult to read the bash manpage is. On my system, the bash 4.1 manpage is 41026 words long, or about 80 pages. Manpages are most useful to me when they are short and to-the-point so I can find what I am looking for before I lose interest in skimming through an 80 page book.

I'm baffled all the time by the lack of clear examples on many manpages.

Yep, a short list of useful one-liners would go a long way. Often, your use-case will be a straight specialisation of one of the examples, and if not, chances are you can cobble together something from several examples. I'd like to have a auxiliary help command for this, e.g. example ffmpeg in addition to man ffmpeg (another 18k words manpage).

Edit: This works rather well:

  function cmdfu() {
         curl "http://www.commandlinefu.com/commands/matching/$@/$(echo -n $@ | openssl base64)/plaintext" --silent | sed "s/\(^#.*\)/\x1b[32m\1\x1b[0m/g"
  }
From http://www.commandlinefu.com/commands/view/3086/search-comma..., it uses the commandlinefu API: http://www.commandlinefu.com/site/api

Re: What's the one Linux command you wish you knew years ago?

#83
post #36
post #33

Earlier quoted context omitted.

every developer should know sed and awk. it is amazing how many devs write cumbersome scripts or little programs to handle tasks that are built for sed and awk. my ~/bin folder has ~50 different little scripts that handle all sorts of small tasks.

I don't really know a lot about this stuff, but my understanding is that Perl was more or less replaced sed and awk.

I code in Perl at my day job, and I still use awk and grep. Sometimes it's quicker to do it in awk than perl, especially when you're doing it in command line and have to figure out quotes.

awk -F, '{print $3}' foo | xargs -I{} echo 'command -x -y -z {}'

perl -e 'open F, ") { my($a,$b,$c,@rest) =split /,/; system("command -x -y -z $c"); }'

I think I could do the split into an array and then take the third element, but I'm just trying to do an elementary example. When you want to do more things to this argument you necessarily have to grow this, and at one point I got to the point where I started having to escape quotes. That's rather difficult to read when you're just trying to do something quick in the command line and you mismatch a pair.

Re: What's the one Linux command you wish you knew years ago?

#84
post #65

Earlier quoted context omitted.

How would you make the bash manpage short and to the point, considering the complexity that it implements?

While it doesn't fix the issue, zsh's man page is divided into different sections, which at least alleviates it.

And zsh has the user-friendly users guide: http://zsh.sourceforge.net/Guide/

This is how I learned unix shell back around 2000ish. Aside of giving an excellent insight into zsh, it also gives many good hints and notes about unix shells in general.

Re: What's the one Linux command you wish you knew years ago?

#85

emacs --daemon (or after you start emacs, M-x server-start). This starts up the emacs server, so that it preserves your open files, and you can start new emacs instances immediately with emacsclient. (I actually symlink emacsclient to vi since it loads as fast as vi, which was one of the weakest points of emacs for me.) Very useful if you are using a remote box (like a vps) as your dev environment.

In my .bashrc I have:

  export ALTERNATE_EDITOR=''
  alias e='emacsclient -t'
Now

  $ e file.txt
will fire up emacsclient to connect to a running emacs daemon. If there is no emacs daemon running it will start one for you and then connect to it.

You can

  export VISUAL="emacsclient -t"
  export EDITOR='emacsclient -t'
for good measure if you like.

Re: What's the one Linux command you wish you knew years ago?

#86
post #13

Earlier quoted context omitted.

You'll want to quote .java so the shell doesn't interpret it as a glob, and find doesn't use GNU style flags. You can also pass wc the filenames directly like this: wc -l $(find . -name '*.java')

That has different semantics (it prints one line per file) and can have line length limitations. This has the same semantics as the parent's version, but without line length issues. find . -name \*.java -exec cat {} + | wc -l

Too many invocations of "cat"

find . -name \*.java -print0 | xargs -0 cat | wc -l

Edit: Dammit. Others have already posted this exact version further below. :-(

Re: What's the one Linux command you wish you knew years ago?

#88
post #46

Not really a command but this: mv /path/to/some/file.{jpg,png} Has been a huge timesaver. It's the same as mv /path/to/some/file.jpg /path/to/some/file.png But saves you the typing/copy pasting the path for the second argument. Can also be used with cp etc.

Either that or rename jpg png /path/to/some/file.jpg

I didn't know you could do that with rename. I would have done

    rename 's/jpg/png/g' /path/to/some/file.jpg

Re: What's the one Linux command you wish you knew years ago?

#89
post #81

Earlier quoted context omitted.

Canonical example: du -h | sort -h

Sort to the same output file: sort -o foo.txt foo.txt You can't do sort foo > foo because you'll be left with a blank file.

sponge(1) from "moreutils"

Although sort(1) handles this for you as you mention, the general problem in the shell of wanting to overwrite your input file with the output file, but not being able to redirect to it for the reason you mention, is solved with sponge. Rather than:

$ grep "foo" bar.txt >baz.txt; mv baz.txt bar.txt

instead:

$ grep "foo" bar.txt | sponge bar.txt

Re: What's the one Linux command you wish you knew years ago?

#90
post #66

Earlier quoted context omitted.

Just diff the two directories. $ ls a b a: 1 2 3 4 b: 1 2 3 # Yours: $ diff -u Maybe I'm missing something?

The OP's example is diffing listings of the two directories whereas your command is asking diff to directly work on the directories themselves.

Which means for example you could get that listing in any format, so you could for example spot changes in permissions or ownership if you included it in the listing.
Post reply on HN