LinuxQuestions.org
LinuxAnswers - the LQ Linux tutorial section.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices



Reply
 
Search this Thread
Old 12-12-2004, 06:56 AM   #1
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Rep: Reputation: 39
Recursive search in bash scripting !


hi ,

I`ve got a dir (say /tmp)
And I have to scan through all the directories inside /tmp one by one find if the files in it have a root permission and if not delete that file.
I`ve got to scan the subdirectories as well.



The best I came up for this recursive scan is using 'tree' command and a lot of processing.
ls -R
also helps but needs further processing of o/p.

Any help will be great

Cheers
Z
 
Old 12-12-2004, 10:01 AM   #2
homey
Senior Member
 
Registered: Oct 2003
Posts: 3,057

Rep: Reputation: 56
hi zulfilee,

Maybe this will work for you. The man page for find shows that you can use group or user when executing a find operation. So, ! -user 0 is would seem to fit in this case.

Code:
#!/bin/bash
find /tmp/* ! -user 0 -exec ls -l {} \;
find /tmp/* ! -user 0 -exec rm -rf {} \;
 
Old 12-12-2004, 08:14 PM   #3
bigrigdriver
LQ Addict
 
Registered: Jul 2002
Location: East Centra Illinois, USA
Distribution: Debian Squeeze
Posts: 5,805

Rep: Reputation: 324Reputation: 324Reputation: 324Reputation: 324
Bash supplies the codes needed to determine file type. A for-next loop to cycle through filenames and an if-then-elif-fi agrument to make decisions based on file type and permissions would complete the process.

Here is a script I wrote for my Linux class a couple of years ago. It includes some of the code you would need. The script is documented so that you can understand what is supposed to happen (it works, btw).

All you need to do is study the code in this script, and adapt the processes to your needs.

Don't forget 'man bash' and 'info bash'. Also, the 'Advanced Bash Scripting Guide'.

Code:
#!/bin/bash 
# lvidrineproj2.sh 
# The program takes two command line arguments. If more or # less than two args  are given, the user gets an appropriate # error message. Then, give the  user information about the #arguments: are they file names; are they 
# regular files or directories; who owns the files; which file is #newer.
  
echo; echo  
if [ $# -ne 2 ]; then    
echo "Please supply two filenames, no more and no less."    echo "Usage: $0 <filename_1> <filename_2>" 1>&2    
echo "Exiting"    
exit 10 
fi  

# have user verify that file names are entered correctly 

echo; echo "You entered '$1' and '$2' for testing." 

echo; echo "Please confirm that you entered the arguments correctly,"
 
echo "no typographical errors. Enter y for yes; n for no."
  
read confirm
  
while [ "$confirm" != "y" -a "$confirm" != "n" ] do  
     echo "Enter y or n only." 
     echo "Re-enter your answer: y or n." 
     read confirm 
done  

if [ "$confirm" = "n" ]; then
    echo "You say you made a typo." 
    echo "Exiting this script."
    echo   
    exit 20 
fi  

# check for valid filename entries in this directory
  if [ -e "$1" -a -e "$2" -a -r "$1" -a -d "$2" ]; then
    echo "$1 is a regular file; $2 is a directory."
    echo "Exiting"
    exit 40 
 elif [ -e "$1" -a -e "$2" -a -d "$1" -a -r "$2" ]; then
    echo "$1 is a directory; $2 is a regular file."
    echo "Exiting"
    exit 40	 
 elif [ -e "$1" -a -e "$2" -a ! -d "$1" -a ! -r "$1" -a \    ! -d "$2" -a ! -r "$2" ]; then
    echo "Neither file is a directory or regular file."
    echo "Exiting"
    exit 30 
elif [ ! -e "$1" -a ! -e "$2" ]; then
       echo "Neither file exists in this directory."
       echo "Exiting"    
exit 30 
elif  [ ! -e "$1" -o ! -e "$2" ]; then 
   echo "One of the files doesn't exist in this directory." 
   echo "Exiting"
    exit 30
 else [ -e "$1" -a -e "$2" -a -r "$1" -a -r "$2" ]
    echo "Both files exist in this directory." 
   echo "Both files are regular files."
 fi

if [ "$confirm" = "y" ]; then        
 # does user own the files  being tested?   
  if test -O "$1"; then  
     echo "$1 is owned by $USER"    
  else  
     echo "$1 is not owned by $USER"    
  fi
  if test -O "$2"; then
       echo "$2 is owned by $USER"    
  else 
      echo "$2 is not owned by $USER"    
  fi 
fi 
  
echo  

# which file is newer? 
if test $1 -nt $2 ; then
    echo "'$1' is newer than '$2'" 
elif test $2 -nt $1 ; then
    echo "'$2' is newer that '$1'" 
else
    echo "'$1' and '$2' were created at the same time." 
fi 
echo 
echo
 
Old 12-12-2004, 11:40 PM   #4
zulfilee
Member
 
Registered: Apr 2004
Location: India
Distribution: Redhat,Fedora
Posts: 430

Original Poster
Rep: Reputation: 39
Thanks a lot for both the scripts.
The find script was pretty cool.

Cheers
Z
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Help with a recursive chmod script in bash lowpro2k3 Programming 11 07-25-2005 08:03 PM
Bash Programming, Recursive/Iterative Calls on Folder Contents gtwilliams Linux - Newbie 1 07-06-2005 07:44 PM
About bash scripting pazvant Programming 3 10-20-2003 12:12 PM
modify bash script - recursive action xscousr Programming 6 09-17-2003 02:52 PM
recursive search of a string in a directory ?! realos Linux - Software 4 11-27-2002 05:49 AM


All times are GMT -5. The time now is 01:42 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration