The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
Thread Thread Starter Forum Replies Last Post
Count occurances in a file samit_9999 UNIX for Dummies Questions & Answers 2 04-23-2008 07:56 AM
Count occurances of a character in a file Shivdatta Shell Programming and Scripting 6 12-24-2007 01:23 PM
awk command to find the count of files ina directory sish78 Shell Programming and Scripting 11 07-19-2007 05:00 AM
count the number chracters occurances in a line skyineyes Shell Programming and Scripting 6 06-26-2007 06:09 AM
How to find the count of files in a directory sish78 UNIX for Dummies Questions & Answers 5 02-01-2007 12:58 PM

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 08-17-2005
Registered User
 

Join Date: Jul 2004
Posts: 7
Find occurances of a directory and count

I am writing my first shell script to do the following:
  1. Find all occurrences of a directory from the pwd
  2. Delete the directory (which is a hidden directory)
  3. Provide feedback w/ the number of directories deleted
The problems I am having are two-fold:
  1. The user may not have the appropriate privileges to delete the directory
  2. grep -c counts the total number of lines returned, rather than the total number of unique occurrences of the directory
For example, I want to let the user know how my directories called "delete_me" were deleted. If the directory structure is /.delete_me/subdir1/subdir2, grep -c returns "3" (/.delete_me, /.delete_me/subdir1, and /.delete_me/subdir1/subdir2). I want to tell the user that 1 directory called ".delete_me" was deleted. How can this be done?

My other problem deals w/ the user not having privileges to delete the directory, or getting feedback on all the directories accessed during the "find" that they don't have permission to read. Ideally, the script would be run w/ root privileges, but I can only get it to run w/ the privileges assigned to the user running the script.

A copy of my code is below. I am just testing, so right now I am not deleting any directories, just finding them and printing to stdout.
Code:
echo "Are you sure you want to recursively delete all .delete_me directories under the current directory? [y/n]: "
echo "Current Directory: `pwd`" 

read answer
case "$answer" in
  y)
    find . -name .delete_me -print
    echo "Number of .delete_me Directories Deleted:"
    find . -print | grep  -c '.parent' 
  n)
    echo  "<--- Operation Aborted! --->";;
  *)
    echo \""$answer"\" is not an option.  Please select \"y\" or \"n\".;;
esac
exit 0
Any help would be greatly appreciated. Thanks in advance.

Ken
Reply With Quote
Forum Sponsor
  #2  
Old 08-17-2005
blowtorch's Avatar
Supporter
 
Join Date: Dec 2004
Location: Singapore
Posts: 2,328
What you want to do can be done as follows:
Code:
echo "Are you sure you want to recursively delete all .delete_me directories under the current directory? [y/n]: "
echo "Current Directory: `pwd`" 

read answer
case "$answer" in
  y)
    find . -name .delete_me -print
    echo "Number of .delete_me Directories Deleted:"
    find . -name .delete_me -print | grep  -c '.parent' 
  n)
    echo  "<--- Operation Aborted! --->";;
  *)
    echo \""$answer"\" is not an option.  Please select \"y\" or \"n\".;;
esac
exit 0
Reply With Quote
  #3  
Old 08-18-2005
Registered User
 

Join Date: Jul 2004
Posts: 7
Thanks Blowtorch, that worked.
Reply With Quote
Google The UNIX and Linux Forums
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 12:38 AM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0