Live data from Hacker News

Unix Commands I Wish I’d Discovered Years Earlier

spin.atomicobject.com

161–170 of 259 posts

Re: Unix Commands I Wish I’d Discovered Years Earlier

#161

Earlier quoted context omitted.

make a small change: for host in ... ; do ssh -l root $host "..." & done; wait and it's very parallel. All that key exchange takes a while, so it's not ridiculously fast, but it's pretty good. If you've got ControlMaster set up already to be per-host, this might very well be very fast the second time.

You may want to look into using GNU parallel with that.

Also check out xargs with the -P 10 -n 1 flags (for example) -- to limit at most 10 parallel tasks running at the same time. Useful if you don't have GNU parallel installed where you happen to be working (everyone has xargs installed).

Re: Unix Commands I Wish I’d Discovered Years Earlier

#162
post #58
post #28

One of the more useful bits of ssh is not mentioned: remotely running commands. Example: ssh username@host "echo $HOSTNAME && sudo somecommand && cat somecommand.log" There's probably a better way to do this, but in a pinch I can fix a problem on dozens of machines just by altering the host string.

The single most useful thing about ssh to me is that it connects stdout/in across the channel (this is behavior inherited from rsh). This allows for e.g.: % (cd /foo; tar cpf - .) | ssh bar \(cd /baz\; tar xpf -\) Or: % cat ~/.ssh/id_rsa.pub | ssh bar tee -a .ssh/authorized_keys

ssh-copy-id is made for exactly this:

  ssh-copy-id  -  install  your  public  key in a remote machine’s authorized_keys

Re: Unix Commands I Wish I’d Discovered Years Earlier

#165

Mac OS: pbpaste & pbcopy For instance: pbpaste | fgrep -i "`pbpaste -pboard find`" To search the copy clipboard with the find clipboard.

On X11 systems, there's also xclip: xclip -o -selection clipboard

There's also xsel, which seems to be installed by default more often than xclip.

  … | xsel -b  # Copy.
  xsel -b | …  # Paste.

Re: Unix Commands I Wish I’d Discovered Years Earlier

#167

Earlier quoted context omitted.

Forgive my ignorance, but I don't know what what it means to connect "across the channel", but it sounds important. Do you know a link that explains the concept? I've failed to Google it.

It means that whatever you put into the ssh process's STDIN gets sent to the STDIN of the remote process. For example you can do this: tar cj $bigdir | ssh host 'tar xj' That creates a tar.bz2 archive on your machine, sends it over ssh to the remote host, where it's unpacked. Ad hoc compressed and encrypted file transfers made easy.

That's a good illustration, but rsync, if available, would generally be preferred for that use case:

  rsync -az $bigdir host:$bigdir

Re: Unix Commands I Wish I’d Discovered Years Earlier

#168

Earlier quoted context omitted.

I lean towards nohup time whatever >whatever.out & because so many programs behave differently when attached to a tty (like prompting for input) and I just want a plain text log that says when it succeeded or where it blew up.

Well that is fine, but that is only covering a very small sliver of what screen/tmux are used for.

Agreed, and my problem with even the usage above is that I often realize too late that "whatever" is not going to finish before I need to unplug. Then I have to kill "whatever" and restart with nohup and logging. tmux/screen always has me covered.

Re: Unix Commands I Wish I’d Discovered Years Earlier

#169
post #6

I want a command that counts files in a tree like du does sizes but without having to pipe find through wc which is crazy for hundred thousand files. Can't they just directly access inodes for high speed counting?

I have this in my .bashrc:

function lc() { if [[ "$#" -gt 1 ]]; then for DIR in "$@"; do echo -n "$DIR - " ; ls -AU1 $DIR | wc -l ; done ; else ls -AU1 "$@" | wc -l ; fi; }

Then "lc dir/" will print the number of files in the directory, and "lc dir/*" will print the number of files in each of the subdirectories. This exactly what you're looking for because it doesn't descend into the tree. If you're working with directories that may have tens or hundreds of thousands of files in them, it's useful to check first.

Re: Unix Commands I Wish I’d Discovered Years Earlier

#170

Earlier quoted context omitted.

Well that is fine, but that is only covering a very small sliver of what screen/tmux are used for.

Agreed, and my problem with even the usage above is that I often realize too late that "whatever" is not going to finish before I need to unplug. Then I have to kill "whatever" and restart with nohup and logging. tmux/screen always has me covered.

Obviously I think tmux/screen is the better option, but bash's builtin 'disown' may have you covered for that specific usecase.
Post reply on HN