Hacker News new | ask | show | jobs
by m463 2019 days ago
My way is:

Try to write small loops and pipes on the command line. I do things like this all the time:

  for i in foo*.txt; do mv $i OLD-$i; done
You'll find this breaks sometimes, so use control-r to recall the command and fix it

  for i in foo*.txt; do mv "$i" "OLD-$i"; done
if it gets too long, copy/paste it into a file like ~/bin/renamer and multiple lines and indenting and make it a more permanent part of your collection.

Also, shell isn't the end. I move to python when shell scripts get cumbersome. python can easily parse arguments (argparse), handle filenames with weird characters in them (os.path), walk trees of files (os.walk) and more.

3 comments

Even better - in BASH and ZSH you can open up your preferred editor and compose a long command in that with "^x e" (or CTRL-x, e) - saving and closing will print the contents into your prompt, ready to be executed with an enter.
I'm often hesitant to run stuff like this because there's a non-zero chance you'll lose important data or bork your OS install with a bad command or typo. I've considered writing an alias that dockerizes my current directory and opens a shell in the new container to test risky commands. Like a localhost staging environment.
Hi,

As of yours loop, such thing, when renaming files in bash, you can do `mv {,OLD-}foo*.txt`

Great practice would be to write report scripts which aggregate multiple data sources into single array of predefined objects which are finally sent into `stdout`, `/var/spool/user` or anywhere else.

> As of yours loop, such thing, when renaming files in bash, you can do `mv {,OLD-}foo*.txt`

I don't think so. Doesn't this break when the glob matches more than one?