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


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script that delete a File when a DEL File will be placed in same folder
# 1  
Old 07-19-2010
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 Smilie

Regards
Bjoern

Last edited by Bjoern28; 07-19-2010 at 05:16 AM..
# 2  
Old 07-19-2010
Code:
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

# 3  
Old 07-19-2010
Code:
[ -f /usr/test/test.del ]  && rm /usr/test/test.csv && /usr/test/test.del




or name independent:

Code:
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
# 4  
Old 07-19-2010
Hello,

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

*.csv and *.del

But when i use this:

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

I get the error:

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

Can you help here?

regards and thanks for your great help Smilie

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

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

Regards
# 5  
Old 07-19-2010
Hi.

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

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


Last edited by Scott; 07-19-2010 at 07:32 AM.. Reason: typo
# 6  
Old 07-19-2010
Thanks scottn,

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

Thanks
# 7  
Old 07-19-2010
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: https://www.unix.com/answers-frequent...n-crontab.html
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Request for Shell script to move files from Subfolder to Parent folder and delete sub folder

Hi Team, I am new to shell script and there is a requirement where files should be moved from Subfolder to parent folder. Eg: parent folder --> /Interface/data/test/IN Sub folder -->/Interface/data/test/IN/Invoice20180607233338 Subfolder will be always with timestamp... (6 Replies)
Discussion started by: srivarun15
6 Replies

2. Homework & Coursework Questions

Oracle dump file (del format) import into db2

1. The problem statement, all variables and given/known data: are the oracle dump files compatible to direct import into db2? I already tried many times but it always truncated results. anyone can help/ advice or suggest? 2. Relevant commands, code, scripts, algorithms: exp... (3 Replies)
Discussion started by: Sonny_103024
3 Replies

3. Shell Programming and Scripting

Del: line 13: syntax error: unexpected end of file

1 echo -e "Enter a filename" 2 read filename 3 if 4 then 5 echo -e "do you want to delete?" 6 read answer 7 if 8 then rm myfirst 9 else 10 echo -e "file not deleted" 11 fi 12 exit0 (1 Reply)
Discussion started by: Speedy
1 Replies

4. Shell Programming and Scripting

How to delete more than 7 days file from Target folder?

Hi Team, I have to remove files which are more than 7 days in my target directory. I have this script but using this i faced some problems like, removed library files of unix. for abc in `find /xxx/abc/OutputFiles/xxxx_*.txt -type f -mtime +5|xargs ls 1` do echo "xxx files are:"$abc ... (1 Reply)
Discussion started by: harris
1 Replies

5. UNIX for Dummies Questions & Answers

To delete the oldest files in a file when file count in the folder exceeds 7

Hi All, I need to delete the oldest file in folder when the file count in the folder exceed 6 ( i have a process that puts the source files into this folder ) E.x : Folder : /data/opt/backup 01/01/2012 a.txt 01/02/2012 b.txt ... (1 Reply)
Discussion started by: akshay01987
1 Replies

6. Shell Programming and Scripting

not del a file not affecting other permissions

i was faced by a question from a friend. i found it very tricky. all my months of learning unix i never can figure it out still. heres the question he faced me with. wondering if you all can help me figure a solution. ill let him know the forums helped me out :D What command would you use... (10 Replies)
Discussion started by: sunny231
10 Replies

7. Shell Programming and Scripting

rsync delete single file from the target folder

Hi We want to delete a single file from the destiantion directory using rsync. Is it possible to do this ? or Do we have any alternate approaches in rsync( for ex applying some filters ..etc) For ex: ----------------------------------------------- Source (Folder) ... (3 Replies)
Discussion started by: MVEERA
3 Replies

8. Shell Programming and Scripting

A script to find dir, delete files in, and then del dir?

Hello!! I have directories from 2008, with files in them. I want to create a script that will find the directoried from 2008 (example directory: drwxr-xr-x 2 isplan users 1024 Nov 21 2008 FILES_112108), delete the files within those directories and then delete the directories... (3 Replies)
Discussion started by: bigben1220
3 Replies

9. UNIX for Dummies Questions & Answers

Delete file from folder

Hi all, I amd new in UNIX programming. I have a query. I need to delete some files (like .dec files) from some folders. I have a list of folders. What will be command for it. Please help me. Thanks in Advance. (6 Replies)
Discussion started by: eclairs
6 Replies

10. Shell Programming and Scripting

Bash script to delete folder based on text file information

I have been working on a script to list all the name's of a subfolder in a text file then edit that text file and then delete the subfolder base on the edited text file so far I have been able to do every thing I just talked about but can't figure out how to delete the subfolers base on a text file... (8 Replies)
Discussion started by: bone11409
8 Replies
Login or Register to Ask a Question