Hacker News new | ask | show | jobs
by AdieuToLogic 480 days ago
> I was doing some file management last weekend and wanted a little script to remove any numbers at the start of a file name and any region values at the end. I also wanted to ensure any duplicate files were moved into a separate folder so i could remove them.

> I could have written that code.

Not knowing precisely your desired result, the benefit of going through the effort of writing a script is experienced gained and deepening one's understanding of the tools involved.

For example, assume this file structure exists:

  .
  ├── 001name-eu-central1.ext
  ├── 001name-us-west.ext
  ├── 002name-eu-central1.ext
  ├── 003name-eu-central1.ext
  ├── keep
  └── research
The script logic to do what you describe could be similar to:

  for file in [0-9]*
  do
    dest="$(echo $file | sed -E -e 's/^[0-9]*//' -e 's/-(eu-central1|us-west)//')"

    if [[ -f "keep/$dest" ]]
    then
      mv "$file" "research/$file"
    else
      mv "$file" "keep/$dest"
    fi
  done

This results in:

  .
  ├── keep
  │   └── name.ext
  └── research
      ├── 001name-us-west.ext
      ├── 002name-eu-central1.ext
      └── 003name-eu-central1.ext

The net-net is that the journey is sometimes more valuable than the destination.
1 comments

I get your sentiment, but I work 40+ hours a week as a swe. I write plenty of code. I don't see how writing a loop with some conditionals and regex would be beneficial. It is something that is not difficult to do, but would take time. The destination, and getting there quickly, was the only goal I was looking for.
> I get your sentiment, but I work 40+ hours a week as a swe. I write plenty of code. I don't see how writing a loop with some conditionals and regex would be beneficial.

No worries, I get it that this example might be a bit contrived and respect the demands engineers have on our time. The reason I went into such detail was to illuminate potential benefits of "exercising mental muscles" as it relates to knocking out similar solutions quicker each time they are needed.