You are not logged in.

#1 2010-06-23 02:35:19

humanshell
Member
Registered: 2010-06-23
Posts: 2

[Solved] Removing temp files in backup bash script

Hello all!! This is my first post in the Arch forums! smile

I am posting this in the General Programming section cause its related to my personal bash backup script.

Because I'm a little anal (and a little OCD) I'd like to remove all those pesky temp files that end in a "~" before I backup my home folder to my external drive.

I am considering adding the following code to the beginning of the script so the temp files will be deleted before the rest of the rsync backup script gets executed:

find ~ | grep '~'  | while read LINE
  do
    rm $LINE
  done

Just wondering if anyone knows of any files that I may be inadvertently deleteing that I should be keeping?

Last edited by humanshell (2010-06-23 13:25:16)

Offline

#2 2010-06-23 02:38:25

brisbin33
Member
From: boston, ma
Registered: 2008-07-24
Posts: 1,799
Website

Re: [Solved] Removing temp files in backup bash script

that might work unless you had some "fi~les" on your system. grep '~$' would help that.

but,

find "$HOME" -name '*~' -delete

might be better; you could also setup your editor to not make the files to begin with.

Offline

#3 2010-06-23 03:19:04

humanshell
Member
Registered: 2010-06-23
Posts: 2

Re: [Solved] Removing temp files in backup bash script

brisbin33 wrote:

that might work unless you had some "fi~les" on your system. grep '~$' would help that.

but,

find "$HOME" -name '*~' -delete

might be better; you could also setup your editor to not make the files to begin with.

I ran a wc -l on my code vs using the regexp "$" from your post. Looks like there's 1 file that contains a ~ somewhere other than at the end. Thank you for that tip.

I also looked at the find man page (which I shoulda done before I posted) and see that using your code I can consolidate the find and delete into one command rather than piping to read and running that until while exits.

Thanks

Offline

#4 2010-06-23 04:52:13

Wintervenom
Member
Registered: 2008-08-20
Posts: 1,011

Re: [Solved] Removing temp files in backup bash script

Depends on srm.

#!/bin/bash
### Superclean Incenerator #####
# Version 0.2 By Scott Garrett #
# Wintervenom [(at)] gmail.com #
################################
# Arguments                    #
# -q  Be quiet.                #
# -r  Rush mode.               #
# -f  Wipe free space.         #
################################

###############
### Globals ###
###############

kill_processes=(firefox chromium midori)

# Incinerate files and/or directories at these paths:
static_incinerate=(
  $HOME/.{recently-used,thumbnails}
  $HOME/.mozilla/firefox/*/{Cache,cookies,downloads,formhistory,sessionstore}*
  $HOME/.bash_history
  $HOME/.secondlife/{cache,logs,*/{screen_last,url_history}*}
  $HOME/{.purple,.weechat,.irssi}/{logs,Logs,icons}
  $HOME/.cache/{Thunar/thumbnailers.cache,chromium}
  $HOME/.config/chromium/*/{History,Cookies,Current,Thumbnails,Visited,Archived}*
  $HOME/.local/share/{Trash,user-places*}
  /tmp/{ff-cache,*.torrent}
)

# Find and incinerate things matching these patterns:
scan_incinerate=(
  '*~'
  '*.tmp'
  '*.temp'
)

# Directory to store free-space wiping files.
pass_dir="$HOME/.pass"
incinerate_mode='D' #DoD seven-pass mode.




############
### Main ###
############
clear

if [ "$1" != '-q' ]; then verbose=1; else shift; fi
if [ "$1" != '-r' ]; then norush=1; else incinerate_mode='s'; shift; fi
if [ "$1"  = '-f' ]; then wipefree=1; else shift; fi

for process in ${kill_processes[@]}; do
  if pidof $process &>/dev/null; then
    [ $verbose ] && echo "Sending terminate signal to process '$process'..."
    killall $process
  fi
done

[ $norush ] && {
  [ $verbose ] && echo "Giving processes time to act..."
  sleep 5
}

for process in ${kill_processes[@]}; do
  if pidof $process &>/dev/null; then
    [ $verbose ] && echo "'$process' is still running -- forcing it to die."
    killall -9 $process
  fi
done

[ $verbose ] && echo "Processes terminated.  Time to destroy shit."

for target in ${static_incinerate[@]}; do
  if [ -e "$target" ]; then
    [ $verbose ] && echo "Incinerating '$target'..."
    srm -${incinerate_mode}rf "$target"
  fi
done

for target in ${scan_incinerate[@]}; do
  [ $verbose ] && echo "Scanning for and incinerating '$target'..."
  find "$HOME" -name "$target" -exec srm -${incinerate_mode}rf {} \;
done

[ $verbose ] && echo "Vacuuming Firefox SQLite databases..."
for f in $HOME/.mozilla/firefox/*/*.sqlite; do sqlite3 $f 'VACUUM;'; done

if [ $wipefree ]; then
  [ $verbose ] && echo "Wiping free space (this may take a long-ass time)..."
  mkdir -p "$pass_dir"
  while dd if=/dev/urandom of="$pass_dir/$RANDOM$RANDOM$RANDOM$RANDOM" bs=1M count=256; do
    true
  done
  sync
  rm -rf "$pass_dir"
fi

[ $verbose ] && echo "Cleaning complete."

Last edited by Wintervenom (2010-06-23 06:00:21)

Offline

#5 2010-06-30 14:24:50

nescafe1001
Member
Registered: 2010-04-09
Posts: 3

Re: [Solved] Removing temp files in backup bash script

If you are using bash 4:

for f in *~ **/*~; do
    [[ -f $f ]] || continue
   rm "$foo"
done

Offline

Board footer

Powered by FluxBB