Archive Your Files Instead Of Deleting Them

This is a command-line tool to archive files instead of deleting them.

We hate deleting. This is why Gmail invites users to archive emails instead of deleting them. What if we apply the same idea to files ? By providing an easy way to archives files, this script will help you to keep your files organized and not being distracted or lost into tons of old files. In any directory, just type :

archive my_file.txt

to archive my_file.txt into the sub-directory Archives/. If Archives/ does not exist, il will be created.

#!/usr/bin/env bash

# stop on error
set -e

CURRENT_PATH=`pwd`
ARCHIVES_PATH="$CURRENT_PATH/Archives"

if [ ! -n "$1" ]; then
    echo "No file or directory to archive."
    exit 1
fi

# Creates the archive directory if doesn't exists
if [ ! -d "$ARCHIVES_PATH" ]; then
    mkdir $ARCHIVES_PATH
fi

for file in "$@"; do
    echo "Archiving $file"
    tar cfz ${ARCHIVES_PATH}/${file}.tar.gz $file
    rm -rf $file
done

Installing

Make it accessible from everywhere by placing it into a directory that is in your $PATH environment variable. If you don’t have such directory for your personal script, you can add one by doing something like :

$ mkdir ~/bin
$ cd ~/bin
$ vim archive
$ echo 'export PATH=~/bin:$PATH' >> ~/.bashrc

Instead of .bashrc it can be .bash_profile, .zsh/zshexport or else depending on the shell you are using.

Comments