Operating System - HP-UX
1751732 Members
5420 Online
108781 Solutions
New Discussion юеВ

Re: delete everything and check whether everything is deleted or not

 
SOLVED
Go to solution
pareshan
Regular Advisor

delete everything and check whether everything is deleted or not

Hi Everyone,

How Can I delete everything from directory
( including hidden files, system files, ready only, .extension **Nothing should be there at all).
and after deleting I need to check also to make sure everything is working fine and there is nothing left. If there are something which I cant delete then I have to send mail to my email address.
I am trying so many things but nothing is completely working. I believe there is a way to do these things efficiently.

Thanks In Advance
I will appreciate any help

18 REPLIES 18
James R. Ferguson
Acclaimed Contributor

Re: delete everything and check whether everything is deleted or not

Hi:

Use this very carefully as root and *nothing* including the directory will be left...and there will be no need to check:

# rm -rf /path

Regards!

...JRF...
Mark McDonald_2
Trusted Contributor

Re: delete everything and check whether everything is deleted or not

As JRF says - rm -rf /path

If you want to keep the directory its self, you could use JRF's method then recreate the empty dir or

rm -rf /dir/*
rm -rf /dir/.* (to remove hidden files)


>>>> and after deleting I need to check also to make sure everything is working fine and there is nothing left. If there are something which I cant delete then I have to send mail to my email address.

You need to count the files or check for file left in the directory after the delete. shouldnt be too hard.

if [[ -f /dir/* ]] (or something similar)
Mark McDonald_2
Trusted Contributor

Re: delete everything and check whether everything is deleted or not

Perhaps put a check before the delete too:

if [[ $path != "/" ]] Just an idea....
then
rm -rf /$path/*
fi
Dennis Handly
Acclaimed Contributor

Re: delete everything and check whether everything is deleted or not

>Mark: rm -rf /dir/.* (to remove hidden files)

You don't want that, that will remove the parent directory.
Better to remove the whole directory as JRF said, rather than make this mistake.
This should work: ll ..?* * .[_0-9A-z]*
But this won't find ".+", unless added above.
\ls -a /dir | fgrep -x -v -e . -e ..
Will exclude "." and "..".
Dennis Handly
Acclaimed Contributor

Re: delete everything and check whether everything is deleted or not

Ok, this works better, the first two for hidden files:
ll .[!.]* ..?* *
pareshan
Regular Advisor

Re: delete everything and check whether everything is deleted or not

James & Mark,

U R right but I cant remove the directory itself. I dont have privilege to delete and make another one. Only thing I can do here is I can delete whatever is inside the directory and its must for me I have to make it empty in any case and put new files there.

Dennis,
>> ll .[!.]* ..?* * --> is this for testing whether anything is left in the directory? or have to use with rm -rf to remove everything?

that means to remove everything
rm -rf dir/*
rm -rf dir/.*
will work?

Im confused. Could U plz help me.
thanks alot
OldSchool
Honored Contributor

Re: delete everything and check whether everything is deleted or not

"Only thing I can do here is I can delete whatever is inside the directory and its must for me"

if you are saying you only want to delete the files within the directory that you own, as opposed to those that are there and you can access, that's something else altogether.

It would help if you clearly explained precisely what is is you need to accomplish (ie a statement of the end result rather than the actions you want to do to get there)
pareshan
Regular Advisor

Re: delete everything and check whether everything is deleted or not

I just want to delete the contains of the directory inlcuding (hidden files, ready only, system files) I mean everything to be precise. And while deleting in case there are files or directories which I cant delete coz of any reason or I dont have permissiom to delete those I should send email to admin or responsible person saying script is causing error coz I dont have privilege to delete everything or there are files or directories which is not deletable.

I need this coz I am trying to delete the old contains of directories and paste new version of files in it.

the script I have tried is

rm -rf ${dest_dir}/*
rm -rf ${dest_dir}/.*

if [[ ll .[!.]* ..?* * |wc -l != 0 ]]; then
echo "! Tar Error!! Scipt is not able to delete ${dest_dir} in ${DHOST} server while code push \n"|mail -x "Urget Tar Error" support@gmail.com
exit

else
echo "${dest_dir} is empty now U can proceed \n"
fi


But I am not sure about my code but I believe there is a better way to write it
Dennis Handly
Acclaimed Contributor
Solution

Re: delete everything and check whether everything is deleted or not

>I can't remove the directory itself. I don't have privilege to delete and make another one.

That may make it easier. Just "rm -rf" the directory, then only the last step will fail. Then check to see if there is anything left in it. (Make sure you try it first to see if it never starts because of the permissions.)

> ll .[!.]* ..?* * --> is this for testing whether anything is left in the directory? or have to use with rm -rf to remove everything?

Both.

>that means to remove everything
rm -rf dir/*
rm -rf dir/.*

No, exactly as I had:
rm -rf dir/* dir/.[!.]* dir/..?*

>I just want to delete the contents of the directory including (hidden files, read only, system files)

There is no such thing as system files as a file property. Hidden files are just a shell and ls(1) convention.

>I don't have permission to delete those I should send email to admin or responsible person

If the rm exit status is bad, you can send a message. But you may want to check the ls output for a more positive check. ls -R would actually list the files in question too.

>if [[ ll .[!.]* ..?* * |wc -l != 0 ]]; then

I doubt this works. You need to test this:
lines=$(ls -a ${dest_dir} | wc -l)
If there is nothing there, you may still see one line, or 3 for . and ..? If so, adjust the number below.
if [ "$lines" -gt 1 ]; then
...