Files delete script


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Files delete script
# 1  
Old 07-28-2014
Files delete script

Friends, I had written a small script to delete files from deletefiles.txt file. However, I want to add one more piece to script, so as to check if the file(file abc) which was already deleted earlier exists in 'deletefiles.txt' file, script should comment out that "file abc doesnt exist". Can anyone suggest me on how to proceed. Below is the script

Code:
echo "CAUTION : Files once deleted cannot be restored"
echo 'Would you like to continue? ("yes" or "no")'
  read INPUT
for i in `cat deletefiles.txt`
do
  if [ $INPUT = "yes" ]
  then
        echo " Deleting files" $i >> $LOGFILE
        rm -rf $i >> $LOGFILE
        echo "Files Deleted"

 elif [ $INPUT = "no" ]
  then
        echo " Deleting files cancelled" $i >> $LOGFILE

  else
        echo "Invalid choice, choose again"
  fi
done


Last edited by Don Cragun; 07-28-2014 at 01:55 AM.. Reason: Add CODE tags.
# 2  
Old 07-28-2014
Quote:
Originally Posted by fop4658
Friends, I had written a small script to delete files from deletefiles.txt file. However, I want to add one more piece to script, so as to check if the file(file abc) which was already deleted earlier exists in 'deletefiles.txt' file, script should comment out that "file abc doesnt exist". Can anyone suggest me on how to proceed. Below is the script

Code:
echo "CAUTION : Files once deleted cannot be restored"
echo 'Would you like to continue? ("yes" or "no")'
  read INPUT
for i in `cat deletefiles.txt`
do
  if [ $INPUT = "yes" ]
  then
        echo " Deleting files" $i >> $LOGFILE
        rm -rf $i >> $LOGFILE
        echo "Files Deleted"

 elif [ $INPUT = "no" ]
  then
        echo " Deleting files cancelled" $i >> $LOGFILE

  else
        echo "Invalid choice, choose again"
  fi
done

What you want to do is not at all clear.

Are you saying that if there is a line in deletefiles.txt that is abc, that line should be ignored?

Are you saying that if there is any line that appears more than once in deletefiles.txt, all but one of them should be ignored?

How do you want to
Quote:
comment out that "file abc doesnt exist"
when that text never appears in your script? Are you saying that you want to hide all diagnostic messages produced by rm?

Is deletefiles.txt supposed to contain a list of regular files? Or are some lines in that file the names of directories that are to be removed after removing the entire file hierarchy rooted in that directory?

Why isn't your script checking whether or not you want to continue before you get into the loop? Wouldn't it make more sense to check that once at the beginning of your script rather than checking it once for very file that is to be processed?
# 3  
Old 07-28-2014
hello Don,

Sorry for my question not being clear. Apologize for the confusion here, as my script requirement changed so we can skip all the questions in your comments.

When I ran the script for testing now, its actually displaying "Files Deleted" for every file deleted. However, I want my script to display "Files Deleted" only once after successful deletion of all files. Below is output I get now for every file deleted.

Code:
Files Deleted
Files Deleted
Files Deleted
Files Deleted
Files Deleted

# 4  
Old 07-28-2014
You can skip answering my questions if you want. I will assume that you now know exactly what to do to solve your problem.

If you would like my help in making your script do what you want, you need to answer my questions so I can figure out what you are trying to do. Since your requirements have changed, answer the questions I asked before with respect to your new requirements.

If you have changed your script to meet your new requirements, show us your new script (using CODE tags).
This User Gave Thanks to Don Cragun For This Post:
# 5  
Old 07-28-2014
hey Don,

here are my comments:

Are you saying that if there is a line in deletefiles.txt that is abc , that line should be ignored? - Earlier, I wanted my script to test the existence of my file before its deleted, however now I feel that isnt important so we can ignore this check.

Are you saying that if there is any line that appears more than once in deletefiles.txt , all but one of them should be ignored? - Again, as there are no two similar files to be deleted, we wouldnt have same line appearing more than once here. Also as each line is input manually in 'deletesfiles.txt' file we wouldnt expect having same line repetition.

How do you want to
Quote:
comment out that "file abc doesnt exist"
- As earlier my question was wrong, I do not require this step any more.

Why isn't your script checking whether or not you want to continue before you get into the loop? Wouldn't it make more sense to check that once at the beginning of your script rather than checking it once for very file that is to be processed? - My previous post :When I ran the script for testing now, its actually displaying "Files Deleted" for every file deleted. However, I want my script to display "Files Deleted" only once after successful deletion of all files. Below is output I get now for every file deleted. I guess the issue is related and my knowledge is limited here and require assistance with this step.

Nothing less, I have added timestamps to my existing script.
Code:
echo "CAUTION : Files once deleted cannot be restored"
echo 'Would you like to continue? ("yes" or "no")'
  read INPUT
for i in `cat deletefiles.txt`
do
  if [ $INPUT = "yes" ]
  then
        echo "`date +%Y%m%d-%H:%M` GMT - [*] - Deleting files" $i >> $LOGFILE
        rm -rf $i >> $LOGFILE
        echo "Files Deleted"

 elif [ $INPUT = "no" ]
  then
        echo "`date +%Y%m%d-%H:%M` GMT - [!] - Deleting files cancelled" $i >> $LOGFILE

  else
        echo "Invalid choice, choose again"
  fi
done

# 6  
Old 07-28-2014
You didn't answer whether or not entries in deletefile.txt are supposed to be individual files or could be directories. I will assume that they can be either since you specified rm -rf.

You echo'ed Files Deleted, but using rm -f you have no idea whether or not the individual file or the directory and all of the files under it was removed. I removed the -f and captured diagnostics for files that couldn't be removed in your log file and added Files Deleted to the log file only if all of the files for that invocation of rm succeeded.

You did not have a loop around the evaluation of your response to the prompt asking if the user really wants to delete files. So, Invalid choice, choose again didn't make any sense to me. Therefore, I combined the no and unrecognized responses to just log the time when the script was called and note that the request was cancelled.

You use $LOGFILE, but never set it in your script. I set it to the base name of your script with .log appended.

You have a log file, but you didn't capture any indication that the requested actions completed successfully or failed. The log file now captures the argument given to rm, any diagnostics produced by rm, and a success indication if rm completed successfully for each invocation of rm.

If the user replies no (or any other answer that is not yes), you will only get that message in the log file once; not once per line in deletefiles.txt.

You also didn't say what OS or shell you're using. The following script was written and tested using the Korn shell, but will work with any shell that supports the basic syntax specified by the POSIX Standards:
Code:
#!/bin/ksh
IAm=${0##*/}
LOGFILE="$IAm.log"
echo "CAUTION : Files once deleted cannot be restored"
printf 'Would you like to continue? ("yes" or "no"): '
read INPUT
case "$INPUT" in
(yes)	while read -r i
	do	date -u "+ %Y%m%d %H:%M  GMT -[*] - Deleting files $i" >> \
			$LOGFILE
		rm -r "$i" 2>> $LOGFILE && echo "Files Deleted" >> $LOGFILE
	done < deletefiles.txt;;
(*)	date -u '+ %Y%m%d %H:%M GMT - [!] - Deleting files cancelled' >> \
		$LOGFILE;;
esac

If you run this in a directory where deletefiles.txt contains:
Code:
file1
non-existent_file2
file3

and the files file1 and file3 are existing regular files, but non-existent_file2 does not exist, then running this script produces a log file containing:
Code:
 20140728 09:03  GMT -[*] - Deleting files file1
Files Deleted
 20140728 09:03  GMT -[*] - Deleting files non-existent_file2
rm: non-existent_file2: No such file or directory
 20140728 09:03  GMT -[*] - Deleting files file3
Files Deleted

if you respond yes to the initial prompt.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script needed to delete to the list of files in a directory based on last created & delete them

Hi My directory structure is as below. dir1, dir2, dir3 I have the list of files to be deleted in the below path as below. /staging/retain_for_2years/Cleanup/log $ ls -lrt total 0 drwxr-xr-x 2 nobody nobody 256 Mar 01 16:15 01-MAR-2015_SPDBS2 drwxr-xr-x 2 root ... (2 Replies)
Discussion started by: prasadn
2 Replies

2. Shell Programming and Scripting

Script to SFTP files then delete them.

Hi, I want to create a script.sh over my local server doing the following: 1- There are files are creating over a directory over my local server "DIR_1". 2- I need to SFTP transfer these files to another Host "HOST_1" by "USER_1"/ "PASS_1". 3- Then Delete the transferred files from my Local... (2 Replies)
Discussion started by: eng_galileo
2 Replies

3. HP-UX

Need help to create a script to delete the files

Hi All, I want to delete all core* files in below file system in Unix server. File system: /usr/sap/P01/JC00/j2ee/cluster/server0 I want to setup a cron job every ten minutes to delete the core *files Thanks N Rao (2 Replies)
Discussion started by: YNRao24
2 Replies

4. Shell Programming and Scripting

Need script to delete old files

Hi, I need a script to delete files older than 2 years or a year. I have around hundreds of old files which needs to be deleted. Could you please help. (2 Replies)
Discussion started by: sv0081493
2 Replies

5. Shell Programming and Scripting

Need help creating a script to FTP files to a server and then delete the files that were transfered.

I am trying to FTP files to a Windows server through my Linux machine. I have setup the file transfer with no problems but am having problem deleting those files from the Linux box. My current non-working solution is below. Any ideas, anyone?? :wall: Please be gentle, I'm fairly new to this... (4 Replies)
Discussion started by: jmalfhs
4 Replies

6. UNIX for Dummies Questions & Answers

script to delete old files

Hi, I want to delete files that are older than 60 days.... i need to execute the script in 7 differnt folders.... i can run the script in crontab to regularly check.... I am struck @ finding out how the file is 60 days old or not... Can u please help me on this? Thanks, NithZ (6 Replies)
Discussion started by: Nithz
6 Replies

7. Shell Programming and Scripting

Script to delete files

Hi, I am looking for a BASH script that deletes old files except the last three recent ones. (8 Replies)
Discussion started by: newuser_25
8 Replies

8. Shell Programming and Scripting

perl script to check if empty files are created and delete them and run a shell script

I have a local linux machine in which the files are dumped by a remote ubuntu server. If the process in remote server has any problem then empty files are created in local machine. Is there any way using perl script to check if the empty files are being created and delete them and then run a shell... (2 Replies)
Discussion started by: hussa1n
2 Replies

9. Shell Programming and Scripting

script to delete files

I have 1000 directories named: 0 - 999 which should contain 1000 files named 0 - 999. But some of these directories contain file whose names are greater than 999 and I need to delete those. I wrote the script below but that doesnt work. Any ideas? #!/bin/bash DIRS=999 for (( j = 0 ; j <... (3 Replies)
Discussion started by: looza
3 Replies

10. Shell Programming and Scripting

script to delete files

hi guys, i need a script to delete files that have core in their name ...it might be part of the file name or as a .core extension ...any file that has core as its extension.... i am only able to delete files which just have thier name as core using this : find $1 -type f -name "core"... (12 Replies)
Discussion started by: vats
12 Replies
Login or Register to Ask a Question