Nick's Tech Blog: Linux > tag files in a directory by directory name

Pages

Tuesday, August 23, 2011

Linux > tag files in a directory by directory name

Work in progress...


#!/bin/bash
#
# "tagdirs.sh"
#
# Simple script which iterates over the first level folders in a given directory,
# and tags the files in those subdirectories with the parent folder's name.
#
# Usage: ./tagdirs.sh
#
# N.B. Requires 'tracker-tag'.
#

# The folder to iterate over.
FOLDER=$1

# Check param is a directory...
if [ -d "${1}" ] ; then
echo "Processing directory '${1}'..."
else
echo "Error: bad argument. Expected a valid directory name for the first argument"
echo "Bad directory name = ${1}"
exit 1
fi

(
IFS=$'\n'
# For each directory...
for dir in $(find $FOLDER -maxdepth 1 -mindepth 1 -type d); do
# Strip directory to folder name only...
tag=${dir##*/}
# For each file in the directory...
for file in $(find $dir -maxdepth 1 -mindepth 1 -type f); do
echo "Adding tag '${tag}' to file '${file}'...";
tracker-tag --add="${tag}" "${file}"
done
done
)

exit 0

1 comment:

  1. Useful Script

    Keep Posting !!
    You can find Linux related more information at :
    Latest Updated on Linux

    ReplyDelete