Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

delete files using find command -help needed 1

Status
Not open for further replies.

prog3

Programmer
Jun 3, 2003
9
US
Hi,

I am writing a script using find command to search for particular files in a directory and delete the files.
The directory structure is like this - /home/load and /home/load/processed.
The files are named as *.pass files and it is present in both /home/load and /home/load/processed directory.
I want to delete the *.pass files only in /home/load directory and not from the subdirectory processed.

The script I have written deletes in both /home/load and /home/load/processed directory.

Following is my script.

#!/bin/sh
RPT_TO=/home/load
cd ${RPT_TO}
for myFile1 in $(find . -name "*.pass"); do
echo $myFile1
rm ${myFile1}
done

How do I prevent it from deleting from the sub-directory.

Please provide me with any ideas.
I appreciate all your help.
Thanks in advance.
 
# no need to 'cd'
# man find

find ${RPT_TO} -type f -name "*.pass" -prune

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
wait a minute.....

isn't it easier to do?:
rm /home/load/*.pass

vlad
+----------------------------+
| #include<disclaimer.h> |
+----------------------------+
 
Hi

I am writing the deleted pass file to a log file. So, I need to loop thru one file after the other.
I followed -prune command and wrote the following script.

#!/bin/sh

RPT_TO=/home/load
RPT_LOG=/home/

rm $RPT_LOG/delete.log

for myFile1 in `find $RPT_TO \( -type d -name Processed -prune -print \) -
o \( -type f -name &quot;*.pass&quot; -print \)`;do
if [ &quot;$myFile1&quot; != &quot;/home/load/Processed&quot; ]; then
echo $myFile1 >> $RPT_LOG/delete.log
fi
rm ${myFile1}
done

*.pass files in Processed directory is not deleted. But when I execute the script, I get the following message.


rm: 0653-603 Cannot remove directory /home/load/Processed.


How do I prevent the above message from appearing?

Thanks in advance.
 
By default, error messages are sent to your screen (stdout). To direct error messages to the &quot;bucket of certain death&quot;, use the following...

rm ${myFile1} 2> /dev/null
 
Try this in your loop:
Code:
if [ &quot;$myFile1&quot; != &quot;/home/load/Processed&quot; ]; then
  echo $myFile1 >> $RPT_LOG/delete.log
  rm ${myFile1}
fi

Hope This Help
PH.
 
for file in /home/load/*.pass
do
echo $file
rm $file
done > $RPT_LOG/delete.log 2>&1
 
Yo only remove files from the /home/load directory:
Code:
#!/bin/sh
RPT_TO=/home/load
cd ${RPT_TO}
find . \( -type d ! -name . -prune \) -o \( -type f -name &quot;*.pass&quot; -print \) | xargs rm
Cheers, Neil
 
Thanks a lot guys.
Wow!! it is amazing to know the different ways of scripting. I appreciate all your help.

 
Hi,

Here is a script I wrote to delete log files (using find) which are 90 days or older and only in the current directory - does not go to subdirectories.

________________________________________________________
cd /export/home/app/logs/
find . \( -type d ! -name . -prune \) -o \( -mtime -90 -print \) -exec /bin/rm -f LOG*.* {} ';'
__________________________________________________________

rm -f will suppress the messages of any subdirectories it may encounter.

Hope this helps

Jamie


 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top