Tuesday, October 9, 2007

Tag Local Files

Nowadays we use tags for almost every online service: blogs, photos, videos etc. They are quite helpful for organizing contents. How about our local files? We still need them for various reasons e.g. archive, privacy, security and so on. I'm going to discuss a simple way to tag local files. We assume you are using a Unix-like system (Linux in the example), where soft link is an inherent system feature.

The idea is quite simple: we setup directory ~/tags and make a subdirectory for each tag. For every file/directory with tags, one soft link to the original file is put under each tag subdirectory. To make tags, we use a command called tag which has the following synopsis:

tag src tag-name...

where src is the path to the original file/directory, and tag-name... are the list of tags to be applied delimited with spaces.

For example, assume there is a image file ~/photo/wonderful.jpg. When you change to directory ~/photo and type the command

tag wonderful.jpg france paris 2007 october

(assume that the image is taken in Paris in October 2007), there will be four directories created: ~/tags/france, ~/tags/paris, ~/tags/2007, ~/tags/october, and each directory contains a soft link to the original file.

Following is a snippet of the content of script tag.

#!/usr/bin/env bash

src=$1
dir=`pwd`
shift 1
for x in "$@"
do
  dst=~/tags/${x}/
  mkdir -p $dst
  ln -s $dir\/$src $dst
done

In the above script, we create directory for each tag and then make soft link within that directory.

To install this script, simply make it executable (by typing command chmod 755 tag), and copy it into a directory included in your search path (e.g. /usr/local/bin, you might need to su to root to gain access for these system directories).

In addition to the operation of adding tags as shown above, you might wonder how can we handle other operations on tags, e.g. deletion, search, and renaming. Fortunately, we don't need additional scripts any more. The reason is that tags are themselves ordinary directories, therefore we can use normal console commands to perform these tasks (e.g. rm for tag deletion, find for search and mv for renaming).

Enjoy organizing your local files with tags!

Update (October 10, 2007) As pointed out by aspir8or: The original script creates links whose paths are missing. He also provides a correction to the issue. The current script is based on his update, with slight changes purely from the author's own preference on variable naming.

4 comments:

  1. Just tried your script, and the links are not created correctly. All that is created is the filename, the path is missing. I'll see if my rusty scripting skills can fix it. I'll post back if I do. Just a matter of adding the cwd to the script.

    ReplyDelete
  2. #!/usr/bin/env bash
    dr=`pwd`
    src=$1
    shift 1
    for x in "$@"
    do
    dst=~/tags/${x}/
    mkdir -p $dst
    ln -s ${dr}\/$src $dst
    done

    ReplyDelete
  3. Hi aspir8or, thanks for your nice comments and your modification is absolutely correct! I'm sorry for my carelessness: during my test, I'm just pretty satisfied when I see that the link is there, but I did not check the actual content. I will revise the post soon (one minor change is that I prefer to use variable "dir" instead of "dr" that you used in the comments and I hope you are OK with this). Thanks again!

    ReplyDelete
  4. Great idea! Just what I was looking for. Thanks!

    ReplyDelete