What’s the best way to handle file operations with scripts?

0
7
Asked By CuriousCoder42 On

I'm looking for feedback on a dual-script solution I created for managing file operations. My approach involves a main script that reads configurations from a YAML file to copy, move, or delete files in designated folders. The configuration specifies the operation type, source and destination directories, and the interval between operations. For example, a configuration named 'Books' moves all .epub files from /downloads to /ebooks every 1440 minutes. The main script copies a base script, modifies it using sed to insert the configuration values, and schedules it with cron. I'm curious if this two-script method is a solid approach or if others would suggest different strategies. Any thoughts?

5 Answers

Answered By FileNinja88 On

Your script sounds interesting, but I'd suggest being cautious with how you're handling the substitutions. Depending on the OS, like a Unix-based system, filenames can contain a wide range of ASCII characters. So just make sure that your script properly handles all possible filenames without breaking!

CuriousCoder42 -

Thanks for the heads up! I'm using Debian 13 and the command gets evaluated with `eval`, like `cp /downloads/*.epub /ebooks`. Do you think using `cp` will manage filenames correctly?

Answered By CodeMasterX On

I'd also recommend replacing your current method with a single cron job that runs an rsync command rather than using cp. Rsync logs issues, handles errors gracefully, and can even remove files from the source after transferring, which could simplify your workflow.

Answered By ShellScripter1 On

What if you used one shell script that accepts different YAML files as input? It could parse the YAML and perform operations accordingly, simplifying your structure.

Answered By DevWhiz201 On

I see where you're going, but your strategy isn't quite what I would have chosen. You've created a sort of template engine, which could lead to issues when evaluating user input. I recommend using a reliable template engine like jinja2 instead. It would streamline your script generation and simplify the cron setup! An alternative would be to just run a command like `my_tidy_script books.yaml` directly as a cron job, which might prevent redundancy if you need to update the template later.

CuriousCoder42 -

Interesting point! I actually have a control script that detects changes in the YAML file and regenerates the script if needed!

Answered By TechGuru77 On

Have you considered using rsync? It might be a better alternative, given that it handles many edge cases automatically, like file existence and spaces in filenames, without having to stop for errors.

CuriousCoder42 -

Great suggestion! I initially thought bash would be simpler, but I'll definitely look into rsync options.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.