Operating System - HP-UX
1755702 Members
2578 Online
108837 Solutions
New Discussion юеВ

delete everything and check whether everything is deleted or not

 
SOLVED
Go to solution
Bill Hassell
Honored Contributor

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

> I don't have permission to delete those...

This is easy to determine before you start. You cannot delete *anything* in a directory where your userID and groupID do not have write access. Here is are the rules:

- A file's permission controls the contents of the file.

but

- The directory's permission control the existence of any files or subdirectories.

If you have write permission in a directory, then you can remove or rename anything in the directory. There may be a file in the directory that is owned by root and has permission 600 - you cannot read or write that file, but if you can write to the directory, you can remove that file. Same for directories inside that directory.

The one exception is if the directory has the "sticky" bit set. This is a special setting that is often used on 777 directories like /tmp and /var/tmp. Rather than allow a free-for-all setting where anyone can trash the contents of an open directory, the sticky bit requires that the user removing or renaming a file in that directory actually own that file or the directory with the file in it.

To see if the sticky bit is set, use this command on the directory:

ll -d /path_to_directory

A typical setting for /tmp would be drwxrwxrwx but with the sticky bit set, the permissions will be drwxrwxrwt (the "t" specifies this option).

> including hidden files, system files, ready only, .extension ...

These are PC concepts and don't apply in Unix. A hidden file is nothing more than a file or directory name that starts with a period ".". It is not really hidden -- shell and other file listing tools like ls and ll will by default, not show dot files and directories. That ends the 'special' meaning for filenames. Most sysadamins will alias ls and ll to always set the -a option to always show dot files. Note that the current (pwd) and parent directory have the names: . and .. which will show up with ll -a.

Read-only for files is not meaningful for removal, and extensions have no special meaning in HP-UX.


Bill Hassell, sysadmin
Bill Hassell
Honored Contributor

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

One additional comment:

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

rm -rf dir/* is OK for files without a leading dot.

rm -rf dir/.* is very bad! The ".*" pattern match will return all dot files INCLUDING the current directory *and* the ".." or parent directory. To see what .* matches, use these commands:

echo .*
ll .*

Avoid using ".*" as a pattern until you verify all the matches.


Bill Hassell, sysadmin
Sajjad Sahir
Honored Contributor

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

Dear
rm -rf is the command to delete everything
but should careful to use this command.
if u are using always u should present dir
ok
Bill Hassell
Honored Contributor

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

And just to clarify a bit:

> but I cant remove the directory itself. I dont have privilege to delete and make another one.

If you cannot re-create the directory, then you cannot remove it. Thus, the simplest form mentioned at the beginning:

rm -rf /path_to/my_directory

will remove everything in the directory and return an error message that it cannot delete my_directory. If my_directory is indeed removed, then you have the ablity to run mkdir my_directory and put the directory back again. As mentioned above, you cannot delete files or directories unless you can write to the parent directory. If you can't make another directory then you cannot remove it either.


Bill Hassell, sysadmin
Fadia Almarei
Super Advisor

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

just remove the directory completly and then recreate it
cd $DIR.PATH
rm -rf dir
mkdir dir
fadia.marei
Fredrik.eriksson
Valued Contributor

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

As they said, if the files are read only you can't delete them without answering yes.

#!/bin/sh
dirpath=/path/to/directory
chmod -R 666 $dirpath
rm -rf $dirpath
mkdir -p $dirpath

This will make all files recursively writeable and delete the lot then create the directory again (-p just says create the whole tree structure).

This will only work if you own the files or do this as root.
You can extend this by adding a test (like [ -d $dirpath ] after the "rm -rf") to check if the directory is really gone and then add a mail or whatever.

Best regards
Fredrik Eriksson
Dennis Handly
Acclaimed Contributor

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

>Fredrik: if the files are read only you can't delete them without answering yes.

The -f removes them without a query. But you are correct if you don't have write permission to directories.

>chmod -R 666 $dirpath

This isn't going to help if you remove execute permission. Better to use:
chmod -R a+w $dirpath

>This will only work if you own the files or do this as root.

And for the latter, if over NFS, they must be mounted with root as root.
pareshan
Regular Advisor

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

Thaks alot everybody. I really appreciate you guys help. I have done using what Dennis said and its working. Bill the way you said is also right but I found it easier to use the other one.

Thanks again
pareshan
Regular Advisor

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

Everything is working fine the way i wanted. Thats why I am closing it.