making a script

Silver-0-surfer

Well-Known Member
Joined
Jan 5, 2008
Messages
317
Reaction score
7
Location
CPT
Hi

first off, im no programmer Im just playing around here and I wasn't really sure were to post this.

I just want to run a little script that runs through a folder 'test' and removes files larger that a specific size , lets say 1 meg.

this is what I have ATM


#!/bin/sh ##I have no idea what thats for but aparently is needed


for dfile in /home/Test/* ;do ##my for do loop, running for all files in a directory
if [ $dfile -ge 1 ] ##if the file is bigger than 1 meg
then
rm -f $dfile ##delete
fi
done


I get errors like
./script: line 5: [: /home/Test/test: integer expression expected

If any of you guys know whats going on, please help. Shouldn't b 2 difficult...:D
 
try "" around 1? Not sure, think it was changed in bash a while ago.

edit: also what is dfile? :)
edit2: might be unsafe to rm $variable, I'd first check to see if it has the full path
 
Last edited:
well the way i understand it, dfile is going to be every file in my directory so
dfile will be test 1 and test 1 > 1mg so will be deleted then
dfile will be test 2 and test 2 < 1mg so will stay
and so on..

thnx will try the "1".

edit: i check it and getting

./script: line 5: [: too many arguments
./script: line 5: [: /home/Test/test: integer expression expected


so it looks like it is the correct path.

lol, i feel like i'm trying to speak french :(
 
Last edited:
I'm not familiar with bash scripting (I use Python for this type of thing), but I'm gonna give you advice anyway.

$dfile -ge 1
Seems to compare the filename to the number one. What I think you should be using is the du -m command. This wouldn't work though because du prints the filesize and then the filename next to it. If you can remove the filename then I think you're golden.
 
Which shell are you using? Had to run a script in Ubuntu a while ago and needed to change the first line to !/bin/bash for it to work. !/bin/sh gave me an error.
 
Here you go dude. Only because I'm procrastinating my ass off right now. You can read the man pages for grep and du for explananation of the options used (I used -k instead of -m for du because -m give 1 for any files less than 1meg).

Code:
for dfile in * ;do ##my for do loop, running for all files in a directory
if [ $(du -k "$dfile" | egrep -m 1 -o "^[1234567890]+")  -ge 1024 ] ##if the file is bigger than 1 meg
then
echo "$dfile" #Change to you remove file command (Remember the quotes!')
fi
done

So anyway, you need quotes around $dfile because in filenames containing spaces bash assumes each word counts as one argument. Hence the too many arguments error.
 
Last edited:
Here you go dude. Only because I'm procrastinating my ass off right now. You can read the man pages for grep and du for explananation of the options used (I used -k instead of -m for du because -m give 1 for any files less than 1meg).

Code:
for dfile in * ;do ##my for do loop, running for all files in a directory
if [ $(du -k "$dfile" | egrep -m 1 -o "^[1234567890]+")  -ge 1024 ] ##if the file is bigger than 1 meg
then
echo "$dfile" #Change to you remove file command (Remember the quotes!')
fi
done

So anyway, you need quotes around $dfile because in filenames containing spaces bash assumes each word counts as one argument. Hence the too many arguments error.


WOW, thanks alot man!! can only check it on Monday tho- bt u really helped me out. I must have spent a good 2 hours on what I did

/hides in shame

but Im gna read over it and try make some sense outa it. shot:D:D
 
Top
Sign up to the MyBroadband newsletter
X