shell script reqd - deleting files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting shell script reqd - deleting files
# 1  
Old 12-18-2008
shell script reqd - deleting files

I have written a script that deletes files:

Requirement: i need to delete the files and to know how many files are deleted i.e the count of files and even i need to display when the started time of deletion and the ending time of deletion. I need to display these two times.

script:

Code:
export j
j=0
for i in `find /opt/CGEYScripts -name "core*" -print`
do echo $i
rm -f $i
j=j+1
done
echo `j'

error:
test1:integ>./Purging.sh
/opt/CGEYScripts/coredumps.ctl
/opt/CGEYScripts/cored
./Purging.sh[8]: j: not found

Can anyone modify the script that meets my requirement, please?

Last edited by bakunin; 12-18-2008 at 07:05 AM.. Reason: added code-tags. Please use them yourself from now on
# 2  
Old 12-18-2008
First, you have used a backquote instead of a dollar sign in the last line where you display the variable j, which is causing the last error.

Second, you should not use backquotes any more, they are outdated. Use "$(...)" instead.

Third, - only if you use ksh - use "print" instead of "echo", because "print" is a built-in while "echo" is an external command.

Fourth, you should not use "for"-loops to cycle through arbitrary lists, because they could become longer than what your shell could fathom. Use a pipeline instead:

Code:
typeset -i DeletedFiles=0
print - "Started purging at: $(date +'<your preferred format here>')"
find opt/CGEYScripts -name "core*" -print | while read filename ; do
     print - "Deleting: $filename"
     rm -f $filename
     (( DeletedFiles += 1 ))
done
print - "Finished purging at: $(date +'<your preferred format here>')"
print - "Deleted $DeletedFiles files."

For a fitting format string for "date" consult the manpage of "date".

I hope this helps.

bakunin
# 3  
Old 12-19-2008
Thanks a lot. It works wonderfully. However, i need to add something to this.

Desired Output:

want to display the output as:

Deleting from the <directory>
<<file1>>
<<file2>>
<<file3>>
;
;
;
<<filen>>

Looking forward...

Thanks in advance,
venkat
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Bash script deleting my files, and editing files in subdirectories question

#!/bin/bash # name=$1 type=$2 number=1 for file in ./** do if then filenumber=00$number elif then filenumber=0$number fi tempname="$name""$filenumber"."$type" if (4 Replies)
Discussion started by: TheGreatGizmo
4 Replies

2. Shell Programming and Scripting

Shell : deleting only first 2 files in a directory

I have 4 files in a directory and want to delete only first 2 files only.. $ ls -ltr total 640 -rw-r--r-- 1 user other 148779 Oct 12 10:50 file1.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file2.xls -rw-r--r-- 1 user other 148779 Oct 12 10:50 file3.xls... (4 Replies)
Discussion started by: giridhar276
4 Replies

3. Shell Programming and Scripting

Help reqd in shell scripting..

Hi guys, I am new to shell scripting and I need urgent assistance. I have an xml like : <AgreementNumberFull>13-WY-84252</AgreementNumberFull> <AgreementNumberAbbr>WY84252</AgreementNumberAbbr> <LineOfBusiness>F</LineOfBusiness> <CompanyCode>0005</CompanyCode> <UniqDigit/> <StateCode/> ... (9 Replies)
Discussion started by: puneetkanchi
9 Replies

4. Shell Programming and Scripting

Need help with shell script for moving/deleting/renaming files

Hi. I need help with a little script that will be used to move some files to their parent directory, delete the directory, rename one file in the parent directory and delete another, then continue to the next. Here's an example: /var/media/Music/Genesis/1970 album - Trespass (2008 Box -... (4 Replies)
Discussion started by: aflower
4 Replies

5. Shell Programming and Scripting

Shell Script - help Reqd

Hi, I need someone's help in writing a shell script. Since am very new i am stuck . I have 2 files in the same dir. ============================================== FileA Table1~07/07/2009 00:00:00~4 Table1~07/06/2009 00:00:00~41 Table1~07/08/2009 00:00:00~4 ... (8 Replies)
Discussion started by: vijayarajvp
8 Replies

6. Shell Programming and Scripting

deleting files inside shell script - ( using find)

Hi, I am using the command find /apps/qualdb/gpcn/scripts/cab_outbound/archive -name 'z*' -mtime +28 -exec rm {} \; in unix command prompt for deleting the files in my unix system under the specfied folder. It was succesfull. But when i running this command inside a shell script name... (2 Replies)
Discussion started by: Jayaram.Nambura
2 Replies

7. Shell Programming and Scripting

unix shell script reqd...

Task: Short Description: To find the files in a particular directory for the previous day, sort them by date and time and e-mail it across to a particular id. And the time is divided into eight fields and based on the time the respective field should be updated with the flag 1. Eight... (7 Replies)
Discussion started by: venkatesht
7 Replies

8. Shell Programming and Scripting

Kron Shell: deleting all but most recent log files

I am trying to create a Korn Shell script to be run every 5-10 minute from a crontab. This script needs to look for log files (transaction_<date>.log). If there are more than 5 such files, it needs to delete all but the most current 5. How often these files are create varies - can be every minute... (2 Replies)
Discussion started by: WmShaw
2 Replies

9. Shell Programming and Scripting

Checklist for Shell Script reqd

Hi, Can anyone provide me with the Code Review Checklist for Shell scripts ?? Thanks in advance. (2 Replies)
Discussion started by: Shivdatta
2 Replies

10. Shell Programming and Scripting

shell script: deleting files from a directory

i have the following problem: use shell script: Two directories have to be searched for files havin the same name. then delete these files from one of these directories. the directory names have to be accepted as command line arguments. what i have done till now is: 1. run a loop... (1 Reply)
Discussion started by: onlyc
1 Replies
Login or Register to Ask a Question