Script to delete folders and files from a prompt


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Script to delete folders and files from a prompt
# 1  
Old 06-05-2013
Hammer & Screwdriver Script to delete folders and files from a prompt

Hi Everyone,

I work for GE Money IVR as a DB analyst and the environment on which I work is Solaris 5.0 server and Oracle 11g.

I got a project in which I have to clean up the folders and files which are not used in DB.

I copied an existing script and edited it, dont know this is the best way.

==============================================
Code:
if [ $# -ne 1 ]
then
   echo "\nUse this to delete folders/files services\n"
 echo "\nUsage: servmenu <sso>\n"
 exit
fi
sso=$1
echo "\n"
echo "******************************************************************"
echo "Delete Options, 2nd Generation************************"
echo "******************************************************************"
echo "1. Delete an empty folder"
echo "2. Delete a folder containing files"
echo "3. Delete a file"
echo "0. Exit" 
echo "\nWhat's your choice? \c"
read answer
if [ $answer -eq "1" ]
then
   echo "/home/mhabeeb : What's the folder name? \c"
   read folder_name
   echo "Delete Folder $folder_name (y/n)? \c"
   read confirmation
   if [ $confirmation = "Y" ] || [ $confirmation = "y" ]
   then
   
   echo "\nserv_load.g2 : Get RCF or PS tables? \c"
   read tables
   
   if [ -z $tables ]
  then
   echo "\nInvalid answer. Exiting...\n"
   exit
  elif [ $tables != "RCF" ] && [ $tables != "rcf" ] && [ $tables != "SF" ] && [ $tables != "sf" ] && [ $tables != "PS" ] && [ $tables != "ps" ]
  then
   echo "\nInvalid answer. Exiting...\n"
   exit
   fi
   if [ "$tables" = "SF" ] || [ "$tables" = "sf" ] || [ "$tables" = "PS" ] || [ "$tables" = "ps" ]
   then
        sf_serv_get.g2 $sso $serv_name
   else
         serv_get.g2 $sso $serv_name
  fi
   
   else
      echo "\nConfirmation failed.\n"
   fi
   exit
fi
if [ $answer -eq "2" ]
then
   echo "\nserv_load.g2 : What's the service name? \c"
   read serv_name
   echo "Load service $serv_name (y/n)? \c"
   read confirmation
   if [ $confirmation = "Y" ] || [ $confirmation = "y" ]
   then
   
   echo "\nserv_load.g2 : Load RCF or PS tables? \c"
   read tables
   if [ -z $tables ]
then
   echo "\nInvalid answer. Exiting...\n"
   exit
  elif [ $tables != "RCF" ] && [ $tables != "rcf" ] && [ $tables != "SF" ] && [ $tables != "sf" ] && [ $tables != "PS" ] && [ $tables != "ps" ]
then
   echo "\nInvalid answer. Exiting...\n"
   exit
fi
    if [ "$tables" = "SF" ] || [ "$tables" = "sf" ] || [ "$tables" = "PS" ] || [ "$tables" = "ps" ]
   then
      sf_serv_load.g2 $sso $serv_name
   else
      serv_load.g2 $sso $serv_name
  fi
                
   else
      echo "\nConfirmation failed.\n"
   fi
   exit
fi
exit

====================================

Or if any one have a better Idea then please share.

Details of the project:

The folder names are like JCPENNY, WALMART, OLDNAVY, SHOPNBC, etc..

Few folders are empty and should be deleted.
Few folders have files of around 20 but none of them are used so need to delete the files including that folder.
Few folders consist of some used and some unued files from which only unsed files should be delete.


I informed Client that I will delete them manually but they said that this is good for development environment and we need a script that will be used for production environment.

Please share your knowledge...

Thanks
Habeeb

Last edited by Franklin52; 06-05-2013 at 10:38 AM.. Reason: Please use code tags
# 2  
Old 06-05-2013
I'd use zip -9 to save what I delete. You need to show the folder names at that level and have a good way to select, say by numbered lines, so they can select what to delete (and zip). For selected name, all folders and files are deleted recursively. See man zip.
# 3  
Old 06-06-2013
Hey thanks for the reply.....

If I zip all the files and folders using -9 then can I extract a single file from it if needed in future..

And regarding the script do I have to mention the delete command with the folder name..?

I am worried because from those 86 folders if some unused folders are already deleted then again I have to change the script... I want some thing which prompts for folder name because the path for all is same... /voice1/vxml/resources/audio/ this is the path....
# 4  
Old 06-10-2013
You can even peruse, or email, any file on unzip stdout without making a copy.

You need a script to identify one of more directories or files to be archived, to build your zip line. If there is a mistake, the files should still be in the zip. Just make sure that you zip all files relative to a common parent directory. Often it is best to use a relative path from there, so the unzip can be alid down anywhere as files and subtrees of that $PWD.
# 5  
Old 06-11-2013
How about the script below....

Code:
#!/bin/sh
echo "Backup / Delete Script************************"
echo "1. Backup Only"
echo "2. Delete Only"
echo "3. Backup and Delete"
echo "0. Exit" 
echo "\nWhat's your choice? \c"
read answer
############## Backup Script ####################
if [ $answer -eq "1" ]
then
   echo "\nFolder name to backup?\c"
   read folder_name
   echo "Backup Folder $folder_name (y/n)? \c"
   read confirmation
   if [ $confirmation = "Y" ] || [ $confirmation = "y" ]
   then
       tar -cvf /voice1/vxml/resources/audio/$folder_name.tar /home/mhabeeb/test 
     else
      echo "\nConfirmation failed.\n"
   fi
   exit
fi
############## Delete Script ####################
if [ $answer -eq "2" ]
then
   echo "\nFolder name to delete? \c"
   read folder_name
   echo "Delete Folder $folder_name (y/n)? \c"
   read confirmation
   if [ $confirmation = "Y" ] || [ $confirmation = "y" ]
   then
       rmdir /voice1/vxml/resources/audio/$folder_name  
   else
      echo "\nConfirmation failed.\n"
   fi
   exit
fi
exit


Last edited by Franklin52; 06-11-2013 at 06:26 AM.. Reason: Please use code tags
# 6  
Old 06-11-2013
Well, I meant never allow a delete without a backup, perhaps swithcing to a special archive for virtual deletes, so they can be ignored in searches. Production data loss is a big sin.

The usual style of tar is not not have absolute paths. You can cd to the target dir and "tar cvf tarfie *" or use an option to achieve the same relative path effect.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Script to compare files in 2 folders and delete the large file

Hello, my first thread here. I've been searching and fiddling around for about a week and I cannot find a solution.:confused: I have been converting all of my home videos to HEVC and sometimes the files end up smaller and sometimes they don't. I am currently comparing all the video files... (5 Replies)
Discussion started by: Josh52180
5 Replies

2. UNIX for Advanced & Expert Users

Help with creating script to delete log files/folders

Hi I am new to Linux / scripting language. I need to improve our Linux servers at work and looking to claim some space my deleting log files/ folders on a 5 day basis. Can someone help me with creating a script to do so. Any sample script will be helpful.:b: Regards (2 Replies)
Discussion started by: sachinksl
2 Replies

3. Shell Programming and Scripting

Shell Script to delete files within a particular time frame under multiple sub folders

Greetings! I'm looking for starting information for a shell script. Here's my scenario: I have multiple folders(100) for example: /www/test/applications/app1/logs /www/test/applications/app2/logs Within these folders there are log files files that need to be deleted after a month. ... (3 Replies)
Discussion started by: whysolucky
3 Replies

4. UNIX for Dummies Questions & Answers

[SOLVED] Delete files and folders under given directory

I have a requirement to delete the files and folders under a given directory. my directory structure is like this.. Data | A(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files |--files | B(Directory) |_PDF(Directory)----pdf files |_XML()Directory --xml files ... (1 Reply)
Discussion started by: ramse8pc
1 Replies

5. Shell Programming and Scripting

Loop folders, delete files, copy new ones

Folks, I am hopeful that you may be able to help me out with writing a script that can be run nightly (as cron?) to loop through all subfolders within the "/media" directory, delete all of the files in each of them, and then copy in all of the files from the "/home//sansa" directory to each of... (6 Replies)
Discussion started by: acraig
6 Replies

6. Shell Programming and Scripting

delete folders with no mpgs or nuv files

I need some help writing a simple script that will delete all subfolders that contain no mpg or nuv files, if they have anything else or are empty the folder should be deleted I got this far but I'm pretty hopeless foo=0 if then foo=1 fi if then foo=1 fi if then echo found... (9 Replies)
Discussion started by: mrplow
9 Replies

7. UNIX for Dummies Questions & Answers

Delete folders and files in it - UNIX

I need to delete a folder and files in it of yesterdays and simply put this in cron. Folder name - "2010-03-2010" File name - "eprod_06_23.dmp" and "eprod_06_23.exp" Actually this folder and file is been created by a script which takes a backup of DB everyday.Thats why it creates folder and file... (3 Replies)
Discussion started by: j_panky
3 Replies

8. Shell Programming and Scripting

Compare files in two folders and delete missing ones

I do not know much about shell scripting so I am at a loss here. If someone can help me, that would be great! I have two directories /dir1 /dir2 I need to delete all files from /dir1 and that does not have a correspondent file in /dir2. It should NOT check file suffixes in /dir2 . Why?... (20 Replies)
Discussion started by: kaah
20 Replies

9. Shell Programming and Scripting

delete files and folders older than 3 days

find /basedirectory -type f -mtime +3 >> /tmp/tempfile find /basedirectory -type d -mtime +3 >> /tmp/tempfile mailx -s "List of removed files and folders" myemail@domain.com < /tmp/te mpfile rm /tmp/tempfile find /basedirectory -type f -mtime +3 -exec rm {} \; find /basedirectory -type d... (7 Replies)
Discussion started by: melanie_pfefer
7 Replies

10. Shell Programming and Scripting

delete all folders/files and keep only the last 10 in a folder

Hi, I want to write a script that deletes all folders and keep the last 10 recent folders. I know the following: ls -ltr will sort the folders from old to recent. ls -ltr | awk '{print $9}' will list the folder names (with a blank line at the beginning) I want to get the 10th folder from... (3 Replies)
Discussion started by: melanie_pfefer
3 Replies
Login or Register to Ask a Question