Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
nasty *~ files clustring everything
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks
View previous topic :: View next topic  
Author Message
lasrach
n00b
n00b


Joined: 19 Jun 2005
Posts: 3
Location: Switzerland

PostPosted: Sun Jun 19, 2005 9:54 pm    Post subject: nasty *~ files clustring everything Reply with quote

Hey there

Most file editors create some tmp files when editing - some of them leave them sitting on your drive for the fun of it ... so i made myself this little script to help me keep my files clean ;) ... just throw it into /usr/bin/clean and enjoy.

CAUTION: this script currently removes anything matching the patterns (see Line 100) IRREVERSIBLY ... consider changing the rm into more paranoid "mv -i" command and doublecheck the patterns - the ones i used may not apply to you :)

ADVICE: check what it does by using the -pv option first ;)

hints/comments/suggestions r welcome

Code:

#!/bin/bash

# Author: Ruben Andrist <lasrach at gmx dot ch>
# Licensed under the GNU Public License, version 2.
# Copyright © Ruben Andrist 2005
# See Line No 100 to change the patterns looked for

# default behaviour:
RECURSE=$false
VERBOSE=$false
PRETEND=$false
QUIET=$false
HELP=$false
USAGE=$false
PATHS=""

for arg in $@; do
  case "$arg" in
    --*)
    case "$arg" in
      --recursive) RECURSE=true ;;
      --verbose) VERBOSE=true ;;
      --pretend) PRETEND=true ;;
      --quiet) QUIET=true ;;
      --help) USAGE=true; HELP=true ;;
      *) USAGE=true; echo " unknown parameter '$arg'" ;;
    esac
    ;;

    -*)
    i=1; while (( i < ${#arg} )); do
      case ${arg:$i:1} in
      R|r) RECURSE=true ;;
      V|v) VERBOSE=true ;;
      P|p) PRETEND=true ;;
      Q|q) QUIET="true" ;;
      H|h) USAGE=true; HELP=true ;;
      *) USAGE=true; echo " unknown short parameter '-${arg:$i:1}'" ;;
      esac
      i=$(( i+1 ))
    done
    ;;

    *) PATHS="$PATHS $arg" ;;
  esac
done

if [ -z "$PATHS" ]; then
  PATHS="."
fi

PATHERROR=$false;
for p in $PATHS; do
  if [ ! -d "$p" ]; then
    if [ -z "$QUIET" ]; then
      echo " specified path is invalid: '$p'"
    fi
    USAGE=true; PATHERROR=true;
  fi
done

if [ -n "$QUIET" ] && [ -n "$VERBOSE" ]; then
  echo " --quiet and --verbose are mutually exclusive"
  USAGE=true
fi

if [ -n "$USAGE" ] && [ -z "$QUIET" ]; then
  echo " usage: clean [OPTIONS] [PATH]"
  if [ -n "$HELP" ]; then
    echo -n "
 Options:
   --recursive  -R   include subfolders
   --verbose    -v   more output
   --pretend    -p   merely show the commands (use with -v)
                     (does not delete anything)
   --quiet      -q   no output (may not be used with -v)
   --help       -h   shows this

 Directory:
   Any Parameter not starting with a dash (-) is considered
   a directory to be cleand. Default is . if none specified
"
  else
    echo " see 'clean --help' for detailed options."
  fi

  exit -1

else

count=0
echo $count > ~/.cleancount

RCODE="";
if [ -z "$RECURSE" ]; then
  RCODE="-maxdepth 1"
fi

for p in $PATHS; do

  ## insert here the patterns you wish to remove
  find $p $RCODE -iname "*~" -or -iname "*semantic-cache" -or -iname "*_region_*" | while read file
  do
    if [ -n "$VERBOSE" ]; then
      echo "rm $file"
    fi
    if [ -z "$PRETEND" ]; then
      # straight forward:
      rm $file
      # paranoid setting:
      # mv -i $file ~/trash
    fi
    count=$(( $count + 1 ))
    # storing to file is an ugly hack, because the 'find | read'
    # takes the variable outta scope ...
    echo $count > ~/.cleancount
  done
done

count=$(cat ~/.cleancount)
rm ~/.cleancount

action="collected"
if [ -n "$PRETEND" ]; then
  action="found"
fi

if [ -z "$QUIET" ]; then
  if [[ $count -eq 0 ]]; then
    echo "No garbage found"
  else
    if [[ $count -eq 1 ]]; then
      echo "1 garbage file $action."
    else
      echo "$count garbage files $action."
    fi
  fi
fi

 exit $count
fi

# unexspected
exit -3
Back to top
View user's profile Send private message
jasperbg
n00b
n00b


Joined: 02 Mar 2005
Posts: 62
Location: Christchurch, New Zealand

PostPosted: Sun Jun 19, 2005 9:58 pm    Post subject: Reply with quote

Cool script, but you could just tell the editors not to leave backup files. Every editor I've used that creates the *~ backup files can easily be configured not to.
Back to top
View user's profile Send private message
lasrach
n00b
n00b


Joined: 19 Jun 2005
Posts: 3
Location: Switzerland

PostPosted: Sun Jun 19, 2005 10:03 pm    Post subject: -> configure the editor Reply with quote

hm, yes ... thats true. but is there a way to - for instance - tell the tex compiler not to create the tmp files? ... i found it somewhat more troublesome to go look for the causes than to just cure the symptoms ... not the best opinion to have, i know :P
Back to top
View user's profile Send private message
lasrach
n00b
n00b


Joined: 19 Jun 2005
Posts: 3
Location: Switzerland

PostPosted: Sun Jun 19, 2005 10:06 pm    Post subject: -> configure the editor++ Reply with quote

++ it's not that i dont want backup files - it's just that i dont want them forever ... (i.e. I might use a backup file right after I mess something up - but then again I usually don't mess things up days after the backup has been made...)
Back to top
View user's profile Send private message
jasperbg
n00b
n00b


Joined: 02 Mar 2005
Posts: 62
Location: Christchurch, New Zealand

PostPosted: Mon Jun 20, 2005 12:05 am    Post subject: Re: -> configure the editor++ Reply with quote

lasrach wrote:
++ it's not that i dont want backup files - it's just that i dont want them forever ... (i.e. I might use a backup file right after I mess something up - but then again I usually don't mess things up days after the backup has been made...)

If that's the case, then your script should probably take the backup files' mtimes into account.
Back to top
View user's profile Send private message
BlindSpy
Apprentice
Apprentice


Joined: 20 Mar 2004
Posts: 263

PostPosted: Wed Jun 22, 2005 12:22 am    Post subject: Reply with quote

AWESOME SCRIPT! This always bothers me and its a pain in the ass to remember to configure emacs to not leave *~ files every time I reset my settings. Also other programs leave them that I use once or twice and dont want to waste time figuring out how to make them skip the backup. Thanks again for the great script.
_________________
Symlinks to:
xorg.conf
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Documentation, Tips & Tricks All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum