Having trouble formulating a shell script and I don't know if what I want to do is even possible

I run a paperless lifestyle. At the end of the year, I archive my financials yearly. So basically, I move all the contents into a zip file, then move that zip file out of my documents and into an archives folder. I then have to remake all of the folders.

This is what I would like to do:

  1. The script copies the folder structure (probably will require a recursive flag of some sort). (Not sure how to do this part).

  2. The script compresses the folder (containing all of the other folders) and moves the zip to the archives folder. (This is easy, it’s just zip and mv commands)

  3. Take the copied folder structure and essentially rebuild it back without all of the actual files. (This part depends on 1)

I originally was just going to individually type the “mkdir /users/name/documents/financials/banking/whatever here” lines, but the script would need modified every time I open a new line of credit or need a new folder for whatever reason.

As I was typing this, I’m thinking maybe it could be done in Python as well. I’d have to research what the OS library in Python can do. Especially if I can just loop through and store the current working directory.

Edit: I haven’t tested it yet, but apparently something like “find -type d -links 2 -exec mkdir -p “/path/to/backup/{}” ;” will work.

Could you simplify the process to:

  1. ZIP your source folder to the destination file.
  2. Delete all files (i.e. non-folders) within the source folder.

If so, totally untested, but maybe something along these lines via a shell script?

zip -r ~/backup/last_stuff.zip ~/stuff
find ~/stuff -maxdepth 10 -type f -delete
2 Likes