Live data from Hacker News

Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

github.com

41–50 of 114 posts

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#41
post #16

I love these one-liners. It's also about knowing your tools better. I hadn't known about `look` [0], which is great. The writer looks to be a bioinformatician, so it might be a bit out of scope, but I also found `socat` [1] quite a good serial communication helper tool. [0] https://man7.org/linux/man-pages/man1/look.1.html [1] https://linux.die.net/man/1/socat

And, fun fact, "socat" is how "kubectl port-forward" works so it'll likely be present on any machine behaving as a kubernetes Node

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#42

I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo`…

>a copy of your original directory, with only the modified files actually taking up space

What's the use case for this sort of thing?

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#43
post #13

sed -i Watch out, that's a Linux-ism and macOS's sed will cheerfully use the thing after it as the backup expression; as far as I know, the absolute safest choice is to always specify a backup extension "sed -i~" or "sed -i.bak" to make it portable, although there are plenty of work-arounds trying to detect which is which and "${SED_I} -e whatever" type silliness My contribution (and yeah, I know, PR it ...) is that…

Using raw escape codes is ugly and device-dependent. People learned this in the 1970’s, and created libraries to get away from having to hard-code escape codes.

Here’s a device-independent variant:

  title(){ tput tsl || tput -T xterm+sl tsl; printf %s "$*"; tput fsl || tput -T xterm+sl fsl; }
Note: if the terminal does not advertise support of the necessary capabilities, it falls back to using the XTerm escape sequences.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#44

I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo`…

>a copy of your original directory, with only the modified files actually taking up space What's the use case for this sort of thing?

At work, we have a codebase that is used by numerous projects. The solution for project specific changes is 'checkout the repo, and make your changes locally'.

What that has turned into is 'checkout the repo and make a new directory structure that symlinks everything back to the original'. The keeps the maintenance burden relatively small as you can easily update the base version, and only need to worry about the files you changed.

I certainly wouldn't recommend using this approach for anything; but it is not as terrible as it sounds.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#45
post #23
post #21

Earlier quoted context omitted.

I don't recall where I heard it, but my understanding is that socat is the sort of successor to good ol' netcat. (Of course, don't ask me to compare each, nor know what socat brings that netcat lacks, etc.)

`socat` is netcat but for serial. [0] [0] https://serverfault.com/questions/246347/whats-the-differenc...

No, socat is for sockets, not for “serial”.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#46

I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo`…

>a copy of your original directory, with only the modified files actually taking up space What's the use case for this sort of thing?

When I need to shuffle around and/or rename my media files, for instance, it's risky to operate on the originals themselves. I've screwed up hundreds of mp3 files by issuing a bad `rename` command. I've lost the hierarchical structures of genres and artists and albums and such by accidentally moving them into the same directory together. And so on.

If you `lndir` your mp3 directory to a functional copy of it, a playground of sorts, you can move things around and rename them without having to worry about scenarios like having to listen to a bunch of mp3s in order to put them back to where they're supposed to be.

When you're satisfied with the re-organization of your files, you can replace your symlinks with the original files. Since none of the directories are symlinked, you never have to worry about `cd`ing into a place you didn't intend to.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#47
> $SHELL current shell

No, $SHELL is the user’s default shell; i.e. the shell started in a new terminal or when logging in on a console or remotely. If another shell program is started, $SHELL will still refer to the default shell, not the running shell program.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#48

I was surprised to see `$()` missing from this (otherwise quite extensive) list. There are a few commands listed which employ it, but it absolutely deserves its own entry. That and `readlink -f` to get the absolute path of a file. (Doesn't work on MacOS; the only substitute I've found is to install `greadlink`.) And `cp -a`, which is like `cp -r`, but it leaves permissions intact - meaning that you can prepend `sudo`…

FWIW `realpath` on macOS should be functionally equivalent to `readlink -f` - particularly if you ignore all the other functionality `readlink` provides.

Nice, thank you! I did not know about `realpath`. Piping absolute paths and copying them to the clipboard is a pattern I use a lot, so this tip will get substantial mileage out of me.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#49
post #16

I love these one-liners. It's also about knowing your tools better. I hadn't known about `look` [0], which is great. The writer looks to be a bioinformatician, so it might be a bit out of scope, but I also found `socat` [1] quite a good serial communication helper tool. [0] https://man7.org/linux/man-pages/man1/look.1.html [1] https://linux.die.net/man/1/socat

I've used `look` for years when I'm not sure how to spell some obscure word and I'm in a context where there isn't a built-in spellchecker (e.g. editing source code). I was today years old when I learned that looking up words from the system dictionary is just the convenient default and you can use it to search lines from any file.

Re: Bash-oneliner: A collection of handy Bash one-liners and terminal tricks

#50
post #43
post #13

sed -i Watch out, that's a Linux-ism and macOS's sed will cheerfully use the thing after it as the backup expression; as far as I know, the absolute safest choice is to always specify a backup extension "sed -i~" or "sed -i.bak" to make it portable, although there are plenty of work-arounds trying to detect which is which and "${SED_I} -e whatever" type silliness My contribution (and yeah, I know, PR it ...) is that…

Using raw escape codes is ugly and device-dependent. People learned this in the 1970’s, and created libraries to get away from having to hard-code escape codes. Here’s a device-independent variant: title(){ tput tsl || tput -T xterm+sl tsl; printf %s "$*"; tput fsl || tput -T xterm+sl fsl; } Note: if the terminal does not advertise support of the necessary capabilities, it falls back to using the XTerm escape sequenc…

As a point of comparison, I ssh-ed into my mac and ran "tput -T xterm+sl tsl" in order to see what it would output, and it hung my connection

So, I'll stick to my printf thanks

Post reply on HN