News for hackers. Stuff that matters.
Re:An Illustrated Guide to Useful Command Line Tools by dickeytk on Saturday October 26, 2019 @01:43PM (#21365098)
I’ve actually found myself using these to get me much more proficient in the Shell overall. fd in particular is so much easier to use I find myself doing things like:

  fd .log$ -x mv {} {.}.bak

  (Rename *.log to *.bak)
Can I do that with xargs, awk, and find? Yes, but every time I have to look up the man page to at least one of them and it’s enough friction that I might just open it in finder if it’s a handful of files.

Having some of these utilities around is a crutch that let me leverage the entire ecosystem much more when I don’t have it. And when I do log into a shared enviroment and don’t have my crutch, then it’s easy to fill in the missing puzzle piece because it’s one part that’s missing and I’ve got the rest of the environment down.

Of course if all you do is shared environments I wouldn’t suggest these, but I would encourage people to use these to get more familiar with the CLI ecosystem.

Re:An Illustrated Guide to Useful Command Line Tools by jcranberry on Saturday October 26, 2019 @04:26PM (#21365976)
Doing this with find is only marginally more difficult.

  find . -name '*.txt' -exec rename .log .bak {} \;
If your rename is prename by default you can use sed style replacement. Not to disagree with your overall point.
Re:An Illustrated Guide to Useful Command Line Tools by rhizome on Saturday October 26, 2019 @02:20PM (#21365304)
bash/zsh: for f in *.log; do mv "$f" "${f/%log/bak}"; done

a bit more generic, `for` loops and parameter expansion are good to know for proficiency, and also I dislike typing curly braces.

Re:An Illustrated Guide to Useful Command Line Tools by mtzet on Saturday October 26, 2019 @02:27PM (#21365339)
This doesn't move files in subdirectories? Or am I missing something?
Re:An Illustrated Guide to Useful Command Line Tools by pfortuny on Saturday October 26, 2019 @02:37PM (#21365394)
You are right, it does not. But the find command is not too complicated, or different from the one above.
Re:An Illustrated Guide to Useful Command Line Tools by dickeytk on Saturday October 26, 2019 @02:48PM (#21365469)
find has awful ergonomics that are completely unlike any other common unix tool. I can never remember the syntax, how the flags work, or what order things need to be in.

Let's use an example I just dug up of using find:

To list and remove all regular files named core starting in the directory /prog that are larger than 500KB, enter:

  find /prog -type f -size +1000 -print -name core -exec rm {} \;
OK first, how in the hell is 1000 == 500kb? Is that a bug in my example[0]? What does `-print` do exactly? And that backslash at the end? I have no clue what that signifies. I'm never going to remember this madness. I'd probably have resorted to writing a bash script in a file by now. But with fd it becomes manageable and memorable for future tasks:

To list and remove all regular files named core starting in the directory /prog that are larger than 500KB, enter:

  fd --type=file --size=+500k ^core$ ./prog -x rm {}
Now that's something that makes immediate sense even if you've never touched the tool before. Not to mention, it doesn't end up in my .git and other ignored directories.

[0] https://kb.iu.edu/d/admm

Re:An Illustrated Guide to Useful Command Line Tools by jovdg on Saturday October 26, 2019 @03:55PM (#21365818)
While I agree that find has weird quirks to my liking, your example is not a great one. I would write the find as follows:

`find /prog -type f -size +500k -name core -delete`

Alternative:

`find /prog -type f -size +500k -name core -exec rm -v {} +`

For extra context:

`-print` will just show you the results before `rm`'ing.

When the command ends with `\;`, the command will be repeated for every match. If the command ends with `+`, the results are appended until max args is reached (and then repeated). This is not always possible, but when it is, it's way easier to use. Less calls to the command, but certainly useful when appending the command after an ssh command, which would mean any number of extra `\` to escape the original `\`...

Re:An Illustrated Guide to Useful Command Line Tools by dickeytk on Saturday October 26, 2019 @04:04PM (#21365858)
TIL, but that’s my point. If after many years of doing this and leading the development on one of the top utilities on homebrew I don’t immediately know the answer to these things, how could anyone.

UX is FAR more important in the CLI than even on the web. Users don’t just have to be able to learn what they want to do, for these common tools they have to memorize it or they won’t use it. Minor things like the order of the flags and bits like having to escape the semicolon don’t just make it challenging. I would argue for 99% of users they make it impossible.

In other words, I think 99% of users don’t know how to use this level of find and will never learn. That’s a problem and no amount of education is going to fix it. The tool itself is broken.

Re:An Illustrated Guide to Useful Command Line Tools by jovdg on Saturday October 26, 2019 @04:24PM (#21365964)
I can't entirely agree with you. Some points:

* The find syntax for this use case is almost identical to the syntax for fd (you may nitpick about "file" vs "f")

* Education would certainly help! How do you think anyone (me, as a data point) learned?

* Tab completion in the shell goes a long way. You will find yourself using the same flags often.

That being said, `find` brings with it a long legacy, which we don't all care for. Many of the options are practically unused, certainly by regular developers.

I find myself using ripgrep instead of grep, but still use find instead of fd.

And I still have a hell of the time as soon as I want to `prune`. I'd much rather `grep -v` at that point, but then I probably need to invoke `xargs` in the next step...

Re:An Illustrated Guide to Useful Command Line Tools by jodrellblank on Saturday October 26, 2019 @05:01PM (#21366155)
Your fd command:

Why type=file and a size? What else has size in 500k range on a filesystem, except files?

=+500 isn’t how math works, that’s implying it could be -500 or using equals for an inequality because the commonly used greater than symbol is off limits, it’s a learned bodge.

500kwhats? Bits? Bytes? Base2? Base10? Can I put a suffix on, is it kB and kb case sensitive?

Why is your filename on the left of the folder you want to look in? That’s so backwards to the left to right order /folder/files are normally written.

Why do you have some arguments with double dash, some with single dash, some with no arg name at all, what’s the pattern for any of that?

What’s -x and why is it short for a word beginning with E?

Why is the find command executing anything at all?

It’s no more immediately sensible than find, it’s inconsistent scribble and workarounds you’ve learned instead of the same that you haven’t learned.

Re:An Illustrated Guide to Useful Command Line Tools by coldtea on Saturday October 26, 2019 @06:32PM (#21366678)
>Why type=file and a size? What else has size in 500k range on a filesystem, except files?

Directories. They have a size depending on the metadata list of included files they contain. And can retain their size even if their inner files are deleted (until some cleanup style process is run).

>It’s no more immediately sensible than find

Oh yes, it is.

The same nitpicking for find would take 10 days, and wont be as contrived...

Re:An Illustrated Guide to Useful Command Line Tools by rovr138 on Saturday October 26, 2019 @02:22PM (#21365311)
Hah, thankfully I refreshed. Was about to post something similar.

No need for xargs, awk or find

Re:An Illustrated Guide to Useful Command Line Tools by nebulous1 on Saturday October 26, 2019 @02:28PM (#21365344)
You might like zsh if you haven't tried it. Has a builtin `zmv` that does this. Check out oh-my-zsh
Re:An Illustrated Guide to Useful Command Line Tools by dickeytk on Saturday October 26, 2019 @02:38PM (#21365402)
This suggestion is exactly why fd is so helpful. With zmv I can rename files in a directory. Great.

But what if I want to count the number of total files within each directory? Create tarballs out of each directory? Rename files that contain the word "FOOBAR" in them?

I can do this with fd and similar tools with slight modifications. With zmv I can rename files.

Re:An Illustrated Guide to Useful Command Line Tools by nebulous1 on Saturday October 26, 2019 @05:18PM (#21366278)
Cool! zsh also does a few things other than renaming files.
Re:An Illustrated Guide to Useful Command Line Tools by jdc on Saturday October 26, 2019 @03:05PM (#21365537)
How do you rename files based on contents with fd?
Re:An Illustrated Guide to Useful Command Line Tools by dickeytk on Saturday October 26, 2019 @03:13PM (#21365579)
I would use rg. My original point is not that fd is a do anything tool, it’s that fd and others are a much better replacements for find, grep, etc that when I want to do something complex they’re much better starting places.