LinuxQuestions.org
Visit Jeremy's Blog.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-19-2004, 01:48 AM   #1
c0d3
LQ Newbie
 
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16

Rep: Reputation: 0
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.
 
Old 11-19-2004, 04:31 AM   #2
Hko
Senior Member
 
Registered: Aug 2002
Location: Groningen, The Netherlands
Distribution: Debian
Posts: 2,536

Rep: Reputation: 111Reputation: 111
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
 
Old 11-19-2004, 04:38 AM   #3
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
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
 
Old 11-19-2004, 05:58 AM   #4
c0d3
LQ Newbie
 
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16

Original Poster
Rep: Reputation: 0
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.
 
Old 12-03-2004, 09:46 AM   #5
c0d3
LQ Newbie
 
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16

Original Poster
Rep: Reputation: 0
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
 
Old 12-03-2004, 06:45 PM   #6
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
I see no initial cd in your script.
My guess is cron is not executing in the directory you expect him to do.
 
Old 12-04-2004, 06:49 AM   #7
c0d3
LQ Newbie
 
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16

Original Poster
Rep: Reputation: 0
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?
 
Old 12-04-2004, 08:00 AM   #8
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
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
 
Old 12-04-2004, 03:41 PM   #9
jlliagre
Moderator
 
Registered: Feb 2004
Location: Outside Paris
Distribution: Solaris 11.4, Oracle Linux, Mint, Debian/WSL
Posts: 9,789

Rep: Reputation: 492Reputation: 492Reputation: 492Reputation: 492Reputation: 492
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 ?
 
Old 12-05-2004, 10:45 PM   #10
c0d3
LQ Newbie
 
Registered: Nov 2004
Location: Philippines
Distribution: Red Hat Advanced Server
Posts: 16

Original Poster
Rep: Reputation: 0
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...
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Script to delete files with 0k file size in a directory justgiver Linux - Newbie 4 01-28-2008 04:56 AM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 10:54 AM
Delete old files and folders Script? AsteX Linux - General 4 11-11-2004 06:26 PM
Why does this script just delete my new wav files all the time? kleptophobiac Linux - Software 5 01-15-2004 09:06 PM
Script on linux to delete certain files ForumKid Linux - General 2 06-22-2002 01:09 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 07:21 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration