Skip to content

Instantly share code, notes, and snippets.

@lopes
Created January 2, 2013 18:40
Show Gist options
  • Save lopes/4436750 to your computer and use it in GitHub Desktop.
Save lopes/4436750 to your computer and use it in GitHub Desktop.
A deprecated code that was used to manage my receipt files.
#!/bin/bash
#chloe.sh
# A receipt files' manager.
#
# Author.: José Lopes de Oliveira Júnior
# Website: http://joselop.es
# Licence: GPLv3+
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
function main(){
#
# cd to directory ($1) and works there. Reads all items. If its
# a file, then gets its new filename and renames it.
#
cd "$1"
for filename in *; do
if [ -d "$filename" ]; then
main "$filename"
cd ..
continue
elif [ -f "$filename" ]; then
local aux=$(new_filename "$filename")
[ -f $aux ] && aux="C$aux" # Avoids equal names.
mv "$filename" $aux
fi
done
}
function new_filename(){
#
# Receives the filename in $1 and returns the new filename
# according to the filename modification datetime.
#
# The pattern of return is ISO 8601 compliant:
# [YYYY][DD][MM]T[hh][mm]Z <.extension>
#
#TODO Deal with files without extension.
#
local aux=$(stat --format="%y" "$1" |
awk '{ print $1 $2 }' |
sed -e "s/[-:]//g" |
cut --delimiter="." -f1).$(echo ${1##*.} |
sed -e "# Lower case
y/ABCDEFGHIJKLMNOPQRSTUVWXYZ/abcdefghijklmnopqrstuvwxyz/
y/ÀÁÂÃÄÅÈÉÊËÌÍÎÏÒÓÔÕÖÙÚÛÜÇÑ/àáâãäåèéêëìíîïòóôõöùúûüçñ/")
echo "${aux:0:8}T${aux:8:4}Z${aux:14}"
}
#
# Main
#
if [ -d "$1" ]; then
main "$1"
elif [ -f "$1" ]; then
path=${1%/*}
file=${1##*/}
[ "$path" == "$file" ] && path="."
cd "$path"
mv "$file" "$(new_filename "$file")"
else
echo "Chloe: Path/File not found - $1"
exit 1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment