PDA

View Full Version : [SOLVED] BASH Directory Recursion



Barriehie
June 10th, 2008, 01:57 AM
I've looked at this for hours and can't find it!!! Can anyone please tell me why my LoopCounter isn't incrementing??? At least it appears to not be. When I run this I only get a directory listing consistent with dirr[0].

Thank You,
Barrie

It's BASH and not PHP / First time around I'm just ls'ing the files and not rm'ing them.
I read the FAQ. :)



#!/bin/bash

#
# Recourse through .thumbnail folder and sub-folders removing the .png files.
#

#
# Define variables
#
dirr[0]="/home/barrie/.thumbnails/normal/*.png"
dirr[1]="/home/barrie/.thumbnails/large/*.png"
dirr[2]="/home/barrie/.thumbnails/fail/gnome-thumbnail-factory/*.png"
dirr[3]="/home/barrie/.thumbnails/fail/gqview-1.0/*.png"
dirr[4]="/home/barrie/.thumbnails/fail/thunar-vfs/*.png"

LIMIT=5
LoopCounter=0

#
# Loop through dirr[array] and delete the thumbnails
#
while [ "$LoopCounter" -lt "$LIMIT" ]
do
#
# Get the recursion to work then we'll rm the files.
#
ls -c ${dirr[$(LoopCounter)]}
let "loopcounter += 1"
done
#
# Bye
#
exit 0

Phenax
June 10th, 2008, 02:06 AM
Well, you could do this much easier with the find command.


find /home/barrie/.thumbnails/ -iname \*.png -exec rm {} \;

Barriehie
June 10th, 2008, 02:16 AM
Well, you could do this much easier with the find command.


find /home/barrie/.thumbnails/ -iname \*.png -exec rm {} \;


Well, that knocked about 32 lines of code out of my backup script and enlightened me to yet another command. Thank you Phenax! I'ld still like to know why my script doesn't work though :confused:

Barrie

ghostdog74
June 10th, 2008, 02:20 AM
look at your spelling please.

HalPomeranz
June 10th, 2008, 02:30 AM
I'd still like to know why my script doesn't work though

Your while loop is a bit busted. Try this on for size:



while [ "$LoopCounter" -lt "$LIMIT" ]
do
#
# Get the recursion to work then we'll rm the files.
#
ls -c ${dirr[$LoopCounter]}
LoopCounter=$(( $LoopCounter + 1 ))
done

Barriehie
June 10th, 2008, 04:18 AM
Thank you all for your prompt responses. I'll have to look at that later; or come up with another beginner script. I almost had it and then broke it!