LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (http://www.linuxquestions.org/questions/programming-9/)
-   -   bash script to delete files (http://www.linuxquestions.org/questions/programming-9/bash-script-to-delete-files-256720/)

c0d3 11-19-2004 01:48 AM

bash script to delete files
 
Hi to all. I've read the posts about script programming but i haven't found anything similar to my problem. I need a script to delete files but the files in a certain directory but before I delete those files, I need to check something from an oracle database and save it to a txt file. I already figured out the algorithm but I don't know the syntax and if its possible to put it in a script.

1. Query from the database and save the result in a txt file. (result.txt). The output of the query would be something like this:
file1_1_91000.log.

2. Read from the result.txt file and get the sequence number of the result (91000). I was hoping to use substr to get only 91000 from the original value file1_1_91000.log
3. From the result, I would be deleting all files in a directory whose filename is
less than the result.txt (file1_1_91000).

Hoping that someone could help me. Thanks in advance.

Hko 11-19-2004 04:31 AM

Try this:

Create some dummy files to test with:
Code:

bash$ for i in $(seq 10 200) ; do touch /tmp/file1_1_${i}.log ; done
bash$ for i in $(seq 10000 10200) ; do touch /tmp/file1_1_${i}.log ; done
bash$ for i in $(seq 90000 90200) ; do touch /tmp/file1_1_${i}.log ; done
bash$ for i in $(seq 91000 91200) ; do touch /tmp/file1_1_${i}.log ; done

Script:
Code:

#!/bin/bash

# Directory to delete file from
#
DIR="/tmp"

# Prefix for the files to delete
#
PREFIX="file1_1_"


# Get te query result
RESULT=$(cat result.txt)

# Extract the number from it.
NUMBER=${RESULT##*_}
NUMBER=${NUMBER%.log}

# Loop for files in DIR starting with PREFIX, ending with '.log':
for FILE in ${DIR}/${PREFIX}*.log ; do

        # Extract the number from this file
        NR=${FILE##*_}
        NR=${NR%.log}

        # If this NR is less than NUMBER: delete
        if [ "$NR" -lt "$NUMBER" ] ; then
                echo rm $FILE  # Remove "echo" to really do the delete.
        fi
done


zulfilee 11-19-2004 04:38 AM

Filename=`tail -n 1 result.txt` #Hope the filename gets appended at end of result.txt

Length=${Filename##*_}
Length=${Length%%.*}
for (( i = 0 ; i < $Length ; i++ ))
do
if [ -f file1_1_"$i".log ]
then
## Before trying it out use echo instead of rm to verify if files names are OK
rm file1_1_"$i".log
fi
done


Cheers
Z

c0d3 11-19-2004 05:58 AM

Thanks Hko and zulfilee for your replies, I've both tried your code and it worked. I adopted the code of Hko because I can understand it a little. I'll try to study harder to code in bash script. Thanks a lot.

c0d3 12-03-2004 09:46 AM

bash script to delete files (Solaris)
 
Guys, I need help on this script. I would like to ask why this script won't run automatically usring crontab in Solaris,when I run it manually,there is no problem,but when I run it in crontab,it does not execute? What should be done? Please advice. Thanks in advance.

P.S. Got this coe from Hko.

#!/bin/bash

# Directory to delete file from
#
DIR="/tmp"

# Prefix for the files to delete
#
PREFIX="file1_1_"


# Get te query result
RESULT=$(cat result.txt)

# Extract the number from it.
NUMBER=${RESULT##*_}
NUMBER=${NUMBER%.log}

# Loop for files in DIR starting with PREFIX, ending with '.log':
for FILE in ${DIR}/${PREFIX}*.log ; do

# Extract the number from this file
NR=${FILE##*_}
NR=${NR%.log}

# If this NR is less than NUMBER: delete
if [ "$NR" -lt "$NUMBER" ] ; then
echo rm $FILE # Remove "echo" to really do the delete.
fi
done

jlliagre 12-03-2004 06:45 PM

I see no initial cd in your script.
My guess is cron is not executing in the directory you expect him to do.

c0d3 12-04-2004 06:49 AM

jlliagre,
I already tried to add cd in the initial location but still to no effect. is the bash environment in Linux the same with Solaris 8?

zulfilee 12-04-2004 08:00 AM

I ve got a few things to say
First the script you have given has an 'echo' and not 'rm'
So it will not echo when its run through crontab [crontab doesnt run on a terminal]. When run manually however you get the files list as you are running it on a terminal.

Second check if the file has exec permission.

Third kindly post the crontab entry you have put

Cheers
Z

jlliagre 12-04-2004 03:41 PM

Quote:

is the bash environment in Linux the same with Solaris 8?
Not the same, but similar enough for your script to work the same.

Quote:

So it will not echo when its run through crontab [crontab doesnt run on a terminal].
It won't indeed echo on the terminal, but the job output (both stdout and stderr), if any, is mailed to the associated user. So c0d3, have you checked your local mail on this machine ?

c0d3 12-05-2004 10:45 PM

Guys,I finally make it to work automatically on Solaris. I just debugged the script line per line and found out that it would work if I removed some characters. First, I removed the "${}" characters in my variables and still worked fine in Solaris, second I changed the ${} with "`" in the cat command program. I dont know what's the explaination why it worked when I changed it. By the way, the echo command was not removed because I am making the test in our production servers. I also made sure that I have execute permission on the file. my crontab entry looks like this: "30 9 * * * sh /log_data/scripts/deletefile.sh" Thanks for all the replies and support...


All times are GMT -5. The time now is 10:38 PM.