Script that delete a File when a DEL File will be placed in same folder

Hi there,

i have a question.

I have a folder called /usr/test

There is a file in it.... test.csv

I need not a shell script that checks if there is a file called: test.del

And if the file is in the same folder then the script should delete the test.csv and also the test.del.

Hope someone can help me :slight_smile:

Regards
Bjoern

if [ -f /usr/test/test.del ]
then
echo "Delete test.csv and test.del"
rm /usr/test/test.csv
rm /usr/test/test.del
else
echo " test.del not found"
fi
[ -f /usr/test/test.del ]  && rm /usr/test/test.csv && /usr/test/test.del

or name independent:

for i in $(ls *.del) ; do rm $(echo $i | sed 's/\.del/.csv/g') && rm $i ;  done

perhaps you will need some error handling, but thats the basic script

Hello,

thanks.. yes i need it for independent files..

*.csv and *.del

But when i use this:

for i in $(ls *.del) ; do rm $(echo $i | sed 's/\.del/.csv/g') && rm $i ;

I get the error:

./del.sh: line 4: syntax error: unexpected end of file

Can you help here?

regards and thanks for your great help :slight_smile:

I am really sorry but i am not familiar with shell scripts... so i hope you can help me here with my problem :slight_smile:

and i dont know if this is important.. the script should run alone as a cronjob :slight_smile:

Regards

Hi.

I wouldn't use ls. It's not necessary, and will just complicate things if your filenames have spaces.

for i in *.del; do
  rm "$i"
  rm "${i%.del}.csv"
done

Thanks scottn,

this works great.. so hope it works also in a cronjob :slight_smile: Just test it :slight_smile:

Thanks

Hi.

There are usually little issues that can creep into scripts when running with cron, or at least things you should be aware of.

Have a read of: cron and crontab

Thanks,

so yes i have a problem...

when i start the script from the command line all works perfect...

But not from the cronjob

In the crontab i entered:

0,5,10,15,20,25,30,35,40,45,50,55  * * * * /download/test/del1.sh > /download/test/test.log 2>&1

The log says:

rm: cannot remove `*.del': No such file or directory
rm: cannot remove `*.csv': No such file or directory

But there are also a CSV and a DEL File in the folder... as i say.. when i start the script on the command prompt all works fine...
Please assist :slight_smile:

regards
Bjoern

Hi.

You should cd into the directory where the script is.

One way:

0,5,10,15,20,25,30,35,40,45,50,55  * * * * cd /download/test; ./del1.sh > test.log 2>&1

If your script is more complicated, then you should also source your profile (as would be described in the link I showed you).

And just to make your crontab a bit more readable, you could change it to

0/5 * * * * cd /download/test; ./del1.sh > test.log 2>&1

Check if it's supported on your system: man 5 crontab

ah OK.. thanks :slight_smile:

Will try it with CD