News for hackers. Stuff that matters.
Re:Moreutils – Unix tools that nobody thought to write (2012) by rozab on Saturday August 22, 2020 @04:29AM (#24242664)
There's a few programs I wish were standard on all Unix systems by now:

* tree - print directory structure

* bat - cat, but actually designed for reading files. Syntax highlighting, line numbers, automatic paging.

* rg - better grep

* direnv - local environment variables

Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @04:57AM (#24242784)
One of the reasons is Rust, but another reason is that the standard shied away from adding anything with frills because UNIX-likes run on all kinds of systems, many which don’t have space for this. There is a reason why something as seemingly ubiquitous as bash or vim are not available everywhere (but sh and vi are). The base set of utilities is really “do more with less” and the ability to keep to this philosophy means that I can connect to a router with 2 MB of MMC storage and still be able to have an extremely functional base set of tools, rather than having some arbitrary subset of the standard utilities because they just can’t be supported on platforms like that.
Re:Moreutils – Unix tools that nobody thought to write (2012) by p00f on Saturday August 22, 2020 @05:32AM (#24242934)
Exactly, I can telnet into my shitty Chinese router and still have cat and sh
Re:Moreutils – Unix tools that nobody thought to write (2012) by rozab on Sunday August 23, 2020 @05:50AM (#24250813)
My phrasing was a bit narrow-minded there, I meant consumer *nix distributions like Debian and what have you. These all manage to make room for Thunderbird and whatever awful music client they've come up with, but lack handy CLI tools.
Re:Moreutils – Unix tools that nobody thought to write (2012) by kps on Saturday August 22, 2020 @01:53PM (#24245832)
A few that I wish hadn't been ignored from 4BSD:

    jot -- print sequential or random data 
    rs  -- reshape a data array
    vis -- display non-printable characters in a visual format
and from Unix >V7:

    apply -- apply a command to a set of arguments
    mc -- multicolumn print
and from AT&T:

    tw -- file tree walk
And then the obligatory personal tools that have stood the test of time:

    align -- align text columns
    crop -- crop lines to a width
    dabl -- delete adjacent blank lines
    dtb -- delete trailing blanks
    emboxxen / deboxxen -- convert to/from box drawing characters
    eol -- convert line endings
    field -- simple line field extraction
    freeze / thaw -- cross-shell synchronization
    mergl -- merge lines into previous lines' blanks
    pad -- pad lines to a width
    put / take -- cross-shell pipe
    uni -- unicode character properties and search
Re:Moreutils – Unix tools that nobody thought to write (2012) by vram22 on Saturday August 22, 2020 @05:22PM (#24247354)
Some of the above can easily be written in C or Python or Ruby (and some of the simpler ones even in shell), at least for simple versions, maybe without all the frills of the originals.

And seq in Linux is like jot for sequential data, at least.

And from my blog: An Unix seq-like utility in Python: https://jugad2.blogspot.com/2017/01/an-unix-seq-like-utility...

Re:Moreutils – Unix tools that nobody thought to write (2012) by kps on Saturday August 22, 2020 @06:41PM (#24247860)
Yes, they're generally available in some form (now generally including the ‘originals’ now that V8–V10 have been published). And they're all very simple, which is the whole point of Unix tools.

I've found I'm using the `put`/`take` pair, for splitting a pipeline across shells, more again in the WFH era where I often have multiple ssh sessions into a machine, when I would probably have used the clipboard in a GUI session. Also useful for things like seeing stderr from different parts of a pipeline in different windows. `put` is just `cat >>$(mkpipe "$1")` and `take` is `cat $(mkpipe "$1")` where `mkpipe` is

    fifo="${TMPDIR:-/tmp}/fifo-$(id -u)-$1}"
    test -p "$fifo" || mkfifo "$fifo"
    echo "$fifo"
Re:Moreutils – Unix tools that nobody thought to write (2012) by vram22 on Wednesday September 02, 2020 @04:37PM (#24357841)
>And they're all very simple, which is the whole point of Unix tools.

Not all Unix tools are simple. E.g. make. But I know what you mean - the Unix philosophy.

https://en.m.wikipedia.org/wiki/Unix_philosophy

The TAOUP book by Eric Raymond (The Art Of Unix Programming) has a lot about that.

https://en.m.wikipedia.org/wiki/The_Art_of_Unix_Programming

And my IBM developerWorks tutorial / case study on Developing a Linux command-line utility (in C) may be of interest to people who want to write their own Unix tools that play well with others.

https://jugad2.blogspot.com/2014/09/my-ibm-developerworks-ar...

Re:Moreutils – Unix tools that nobody thought to write (2012) by enriquto on Saturday August 22, 2020 @10:56AM (#24244543)
> tree

I agree, tree is great. It can be somehow replicated by using "find .", if you are in a hurry.

> bat

If you need that functionality, why don't you open the file with vim, for example?

> rg - better grep

Grep is pretty nifty, I don't see how could it be improved. What is the main advantage of of rg over grep?

> direnv - local environment variables

I read the manpage for direnv and I was really scared. What is it for? I never used an .envr. If I need to see my environment I can simply use "env". What is it missing?

Re:Moreutils – Unix tools that nobody thought to write (2012) by breuleux on Saturday August 22, 2020 @11:38AM (#24244816)
direnv is super useful if you're working in Python and have multiple projects: you can add an .envrc file that activates the right virtualenv whenever you enter a directory. It's one less thing to worry about.

I figure you can also use it in conjunction with `module load` to automatically load the right module when you enter a project's directory. Modules are used in many computing clusters to manage environments.

Also direnv will never execute an .envrc without permission, you need to run `direnv allow` whenever there is a new or newly modified .envrc.

Re:Moreutils – Unix tools that nobody thought to write (2012) by flukus on Sunday August 23, 2020 @02:06AM (#24249937)
I've got a hand rolled direnv that's quite simple:

  function lenv {
    path=$PWD
    while [[ ! -f "$path/.env" && "$path" != '/' ]]; do
      path=$(dirname "$path")
    done
    if [ -f "$path/.env" ]; then
      source "$path/.env"
    fi
  }
function cd { builtin cd "${@}"; lenv; }

It lacks the security but it works recursively and allows more than just environment variables. A common script I have is to automatically build for instance:

  here=$(dirname ${BASH_SOURCE[0]})
  function autobuild {
    (
      mkdir -p build
      cd $here/build
      ../configure
      while true; do
        make && make check
        inotifywait -qr -e close_write,delete $here/src $here/test $here/Makefile.am
      done
    )
  }
I've tried generalizing the latter but between different targets, different build systems, different projects structures etc, copy and pasting something like this per project is the least worst option.
Re:Moreutils – Unix tools that nobody thought to write (2012) by breuleux on Sunday August 23, 2020 @11:36AM (#24252605)
direnv does allow more than just environment variables, you can run arbitrary commands. Personally I wouldn't run potentially expensive commands like building the project, but if that's really what's best for your workflow, it can be done.

If there is no .envrc in the directory it will look for one in the parent directory, but it won't activate more than one.

Also one thing it does that your hand-rolled version does not is that it remembers what the environment variables were before and restores them to that state when you exit the directory.

Re:Moreutils – Unix tools that nobody thought to write (2012) by flukus on Sunday August 23, 2020 @09:41PM (#24256854)
> Personally I wouldn't run potentially expensive commands like building the project

My above example wasn't running the command, just defining it to be run manually. The .env becomes a dumping ground for all sorts of project specific stuff. The docs say direnv doesn't support this.

> Also one thing it does that your hand-rolled version does not is that it remembers what the environment variables were before and restores them to that state when you exit the directory.

I've thought of adding this, but apart from a couple of things I reset manually I haven't really found the need.

Re:Moreutils – Unix tools that nobody thought to write (2012) by breuleux on Monday August 24, 2020 @07:09PM (#24266535)
Ohh, okay, I see.

You're right, you can't define aliases/functions using direnv. That's a shame, that does seem useful.

Re:Moreutils – Unix tools that nobody thought to write (2012) by gpanders on Saturday August 22, 2020 @11:05AM (#24244601)
Re: bat. I agree with you, I don’t get the hype over this program either. It’s pretty easy to configure 'less’ to use syntax highlighting and ‘less’ is installed basically everywhere.

The main advantage of rg is how much faster it is than grep. It’s WAY faster.

I don’t use direnv but I have colleagues who do. I think the idea is that it can set environment variables/run arbitrary commands when you cd into a certain directory (such as activating a Python virtual env, for instance).

Re:Moreutils – Unix tools that nobody thought to write (2012) by haberman on Saturday August 22, 2020 @12:31PM (#24245222)
I also like that rg's default mode is to search all files recursively, skipping binary files and honoring .gitignore. 90% of the time this is just what I want.
Re:Moreutils – Unix tools that nobody thought to write (2012) by krick on Saturday August 22, 2020 @09:19AM (#24243928)
I don't think they should be standard at all. I am familiar with all of them pretty much since the moment they are born (which is not that long, TBH) and the only one I actually use is tree. Which is also quite a judgement call, since it can be replaced by exa, which is more powerful (I'm considering it; I'm pretty sure that you will like it, BTW, so take a look). bat is useless, since there is vim/view. rg is actually good, but cannot be used as a drop-in replacement for ag (I don't remember what the problem is exactly, but I tried several times and burntsushi even gave me some advice on that, and I still use ag, which is slower, so I assume there actually was some serious reason). And, by the way, no, neither of them is actually a replacement of grep.

(Also, yeah, let's ignore that Rust in the standard libraries is still a problem.)

Re:Moreutils – Unix tools that nobody thought to write (2012) by wodenokoto on Saturday August 22, 2020 @04:56AM (#24242781)
Good list of things you can install to better your CLI experience, but I am not sure if I agree that they fill out missing features.

Why is bat better than less?

I admit to catting files for reading as much as the next guy, but I almost always have the tiniest regret that I didn’t feed it into less and kept my terminal cleaner.

As for RG and AG. other than being faster, how are they better? I thought they were api complainant drop-in replacements so it’s not like they fill in a missing role.

Re:Moreutils – Unix tools that nobody thought to write (2012) by burntsushi on Saturday August 22, 2020 @08:21AM (#24243630)
> I thought they were api complainant drop-in replacements so it’s not like they fill in a missing role.

Could you please let me know how you came to that understanding? Because I'd really like to fix it. ripgrep was never intended to be POSIX compatible. It would have been a straight-jacket over its functionality. See: https://github.com/BurntSushi/ripgrep/blob/master/FAQ.md#pos...

> As for RG and AG. other than being faster, how are they better?

For ripgrep at least, a lot of it comes down to speed, better output formatting by default and suppressing results you probably don't want by default (but that can be turned off quite easily).

But there are other features, like better Unicode support, built-in support for searching compressed files, support for file types and a few other things. The README includes a bit more. ;-)

Re:Moreutils – Unix tools that nobody thought to write (2012) by wodenokoto on Sunday August 23, 2020 @04:49AM (#24250592)
Hi thanks setting me straight.

I have actually read a few of your (great) articles about ripgrep. I hope I didn’t come off as dismissive of your impressive work - though rereading my comment I should perhaps have chosen my wordS a bit more thoughtfully.

I did know that RG would skip more files than normal grep, so I guess I knew it’s not a safe replacement for all shell scripts.

But other than that, I’ve never thought of AG or RG as tools that brings something new to the table. I’ve always thought of them as a drop ins, with slightly new defaults and much faster speed. And the discussion related to TFA is tools that are missing, not tools that are better.

Maybe it’s a mix of the name and countess blog posts telling readers to use RG instead of GREP, that lead me to consider RG as a better grep and not a different tool.

Re:Moreutils – Unix tools that nobody thought to write (2012) by burntsushi on Sunday August 23, 2020 @08:39AM (#24251478)
Interesting. Hmm. Not quite sure how to combat that one. The issue is that even though ripgrep is not POSIX compatible, it can pretty easily replace grep in most or all circumstances. The similarities between the tools are, for example, much greater than their differences. I know for me at least, I don't ever run grep interactively. The only time I ever use it is in shell scripts.
Re:Moreutils – Unix tools that nobody thought to write (2012) by masklinn on Saturday August 22, 2020 @05:06AM (#24242833)
> As for RG and AG I thought they were api complainant drop-in replacements

To grep? Not that I know, I don't think either respects POSIX grep to say nothing of GNU grep. Though the trivial usage is identical, and many basic options carry over (e.g. -C and friends).

> other than being faster, how are they better?

Way better defaults for one. `rg` defaults to being recursive, ignoring binary files, respecting various types of ignore files, not being limited to BREs, coloring, …

They also have useful convenience features like "file categories" (e.g. `-t` will expand to a bunch of predefined include glob patterns so you don't have to input them by hand, which can be tedious), or parallelism (recursive ripgrep will parallelise searches across files, with grep you have to remember to combine `find`, `xargs -P` and `grep` for that to happen).

Re:Moreutils – Unix tools that nobody thought to write (2012) by pbhjpbhj on Saturday August 22, 2020 @08:20AM (#24243628)
>cat, but actually designed for reading files. Syntax highlighting, line numbers, automatic paging. //

`less` not good enough?

Re:Moreutils – Unix tools that nobody thought to write (2012) by atsaloli on Saturday August 22, 2020 @08:44AM (#24243745)
I’ve never seen syntax highlighting in “less”.
Re:Moreutils – Unix tools that nobody thought to write (2012) by atsaloli on Saturday August 22, 2020 @08:52AM (#24243781)
Ha! Here’s how to have syntax highlighting while using less: https://superuser.com/questions/71588/how-to-syntax-highligh...
Re:Moreutils – Unix tools that nobody thought to write (2012) by kps on Saturday August 22, 2020 @11:42AM (#24244834)
While I don't use syntax highlighting, I find `lesspipe` useful for files that have a reasonable ‘presentation form’ as well as a source form — html, markdown, etc.
Re:Moreutils – Unix tools that nobody thought to write (2012) by Gee19 on Saturday August 22, 2020 @02:38PM (#24246194)
bat is another great alternative

https://github.com/sharkdp/bat

Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @03:44PM (#24246719)
Mentioned at the top of this thread ;)
Re:Moreutils – Unix tools that nobody thought to write (2012) by beojan on Saturday August 22, 2020 @04:36AM (#24242700)
> rg

Never going to happen because it's written in rust. ag (https://github.com/ggreer/the_silver_searcher) on the other hand is written in C.

Re:Moreutils – Unix tools that nobody thought to write (2012) by nindalf on Saturday August 22, 2020 @04:46AM (#24242741)
Never say never :)

If Rust being added to the Linux kernel[1] isn't far-fetched, I don't think adding a utility in Rust is crazy either.

[1] - https://lore.kernel.org/lkml/CAKwvOdmuYc8rW_H4aQG4DsJzho=F+d...

Re:Moreutils – Unix tools that nobody thought to write (2012) by chalst on Saturday August 22, 2020 @12:37PM (#24245262)
Interesting.

Isn't it the case that LLVM doesn't support all architectures that Linux does?

Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @01:58PM (#24245882)
I think this is just for kernel modules that you can choose to compile out.
Re:Moreutils – Unix tools that nobody thought to write (2012) by geofft on Saturday August 22, 2020 @02:58PM (#24246335)
That's what I expected too (as one of the people on the thread / one of the authors of the farthest-along set of bindings for modules), but Linus and Greg KH both seem fairly inclined for it to be on by default and used for core code (small bits of core code, in the beginning) and not simply modules. I'm not sure how that's going to play out with architecture support, and that's one of the things I want to get out of the Plumbers session this upcoming week.

https://github.com/fishinabarrel/linux-kernel-module-rust/is... is a chart of Linux architectures and whether Rust and LLVM support them.

Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @03:43PM (#24246716)
Oh, wow, that would be huge. Not just architecture support, but even having a toolchain on some platforms might be problematic as I can imagine making rustc a requirement to build Linux to be fairly controversial in and of itself. And that architecture list is not looking very good…would be very interested to know what they decide on as well!
Re:Moreutils – Unix tools that nobody thought to write (2012) by feanaro on Saturday August 22, 2020 @04:38AM (#24242706)
What's never going to happen?
Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @04:40AM (#24242716)
A Rust program being standard on all UNIX systems.
Re:Moreutils – Unix tools that nobody thought to write (2012) by genuinebyte on Saturday August 22, 2020 @04:44AM (#24242730)
I'll bite: Why?
Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @04:50AM (#24242756)
Rust is designed for a fairly small number of relatively modern systems; there are many close-to-POSIX systems that can’t (and I would guess likely will never) be able to run Rust code in any formal capacity. To the hopeful: even C++ has been unable to win here, despite having many more years of a head start and much more accommodation for stranger platforms. It’s just not going to happen.
Re:Moreutils – Unix tools that nobody thought to write (2012) by MaxBarraclough on Saturday August 22, 2020 @05:28AM (#24242914)
What kind of platforms are you thinking of? People have got Rust running on the Raspberry Pi.

https://old.reddit.com/r/rust/comments/cdcads/

Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @05:43AM (#24242976)
Linux on ARM is a very mainstream platform from the perspective I’m looking at this from, I’m afraid. I’m talking about strange Unices running on architectures that GCC may or may not maintain a backend for, or maybe a BusyBox available on some debug interface, maybe a school project to build a simple POSIX-compatible OS or a bootstrap for a platform that had been recently jailbroken. In these cases C is almost always the go-to language and I suspect it will remain so for the foreseeable future as this is precisely the long tail of distributions that standards were intended to provide a base set of tooling for.
Re:Moreutils – Unix tools that nobody thought to write (2012) by dividedbyzero on Saturday August 22, 2020 @05:55AM (#24243024)
Could you give some actual examples? This sounds interesting but I'm not sure I've ever seen something like that in the wild.
Re:Moreutils – Unix tools that nobody thought to write (2012) by hvdijk on Saturday August 22, 2020 @07:18AM (#24243365)
This is clearly not one of the examples saagarjha was thinking of, but an actual example where Rust is not an option is the x32 ABI for x86_64 platforms. This ABI works well with GCC, but LLVM suffers from multiple code generation bugs. As Rust is based on LLVM, Rust for the x32 ABI does not work well either. While it's possible to use a mixture of ABIs, so that you have a mostly-x32 system but with some x64 binaries, this requires a multilib setup with multiple copies of system libraries. A pure x32 system cannot currently have Rust utilities.

(I have some patches to improve things but I have not been able to submit them to LLVM yet, and with those patches I did manage to get a working x32 build of rg on my system. I hope to be able to do so in the future.)

Re:Moreutils – Unix tools that nobody thought to write (2012) by pafje on Saturday August 22, 2020 @05:54AM (#24243020)
Rust supports many platforms but only has "tier 1 support" on a handful of mainstream architectures.

https://doc.rust-lang.org/nightly/rustc/platform-support.htm...

Re:Moreutils – Unix tools that nobody thought to write (2012) by dig1 on Saturday August 22, 2020 @06:01AM (#24243049)
MIPS and SPARC to name a few. Almost all routers are on MIPS and most of them are running some form of Linux or BSD.

AFAIK, rust is still marked as "guaranteed to build" on these platforms, but assume only Linux. BSDs, not so much.

Re:Moreutils – Unix tools that nobody thought to write (2012) by fiddlerwoaroof on Saturday August 22, 2020 @06:58AM (#24243274)
In my experience, most consumer routers are ARM?
Re:Moreutils – Unix tools that nobody thought to write (2012) by happycube on Saturday August 22, 2020 @12:10PM (#24245048)
Maybe new ones, but there's a ton of MIPS ones too.
Re:Moreutils – Unix tools that nobody thought to write (2012) by sannee on Saturday August 22, 2020 @06:11AM (#24243087)
The Debian ripgrep binary is 5.1MB. That would consume over 15% of a 32MB flash, which is the common constraint you have to work with in many (actually, discounting Android, it might even be most) Linux deployments.
Re:Moreutils – Unix tools that nobody thought to write (2012) by rcxdude on Saturday August 22, 2020 @07:00PM (#24247972)
You're not likely to even find full-fat grep on such platforms (only busybox)
Re:Moreutils – Unix tools that nobody thought to write (2012) by saagarjha on Saturday August 22, 2020 @04:51AM (#24242761)
bat is Rust too.
Re:Moreutils – Unix tools that nobody thought to write (2012) by isatty on Saturday August 22, 2020 @06:20AM (#24243119)
+1 for ag (the_silver_searcher)
Re:Moreutils – Unix tools that nobody thought to write (2012) by enedil on Saturday August 22, 2020 @07:12AM (#24243345)
Why do you want automatic paging by bat, if you can just open it in Vim.