Question

How can we archive old directories and delete them after? If we suppose an old directory is one who has modified last time at least three days ago, I am able to get my directories list with

find . -mindepth 2 -type d -mtime 3

which return a list like

./dir1/1394547493
./dir2/1394525075
./dir2/1394531732
./dir3/1394546562

Now we need for any directory from this list to create a .ZIP archive in coresponding dirX containing ONLY the files from it (there are no more other directories in those directories), and delete the subdirectory right after. In the end our structure should look like this

./dir1/1394547493.zip
./dir2/1394525075.zip
./dir2/1394531732.zip
./dir3/1394546562.zip

Resulting archives must not contains any paths.

Was it helpful?

Solution

Try this one-liner:

    for dir in $(find . -mindepth 2 -type d -mtime 3); do cd "$dir" && zip ../$(basename "$dir") * && cd - && rm -rf "$dir" || cd -; done

This one-liner enters each directory, zips its contents without parent directories and removes the directory on success, but leaving the directory in place in case of failure.

Good luck :)

Edit: Your directory names are required to not include spaces or tabs

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top