A script within a folder to delete the folder, script, and the folder's contents
I was on a different forum asking this question, but since it ended up destroying part of my system, I'm wary about any response from there at the moment. That's why I decided to come here.
This is what I'm trying to do... I need a script that can be executed in its directory to delete the directory and its contents. The following is a more detailed view of what I'm doing. I hope to execute tasks.sh inside of each folder and have it carry out its commands. Scripts: 1. mkdir.sh 2. tasks.sh 3. copy.sh 4. execute.sh mkdir.sh Code:
#!/bin/sh Code:
#!/bin/sh Code:
#!/bin/sh Code:
#!/bin/sh The idea is that after all tasks.sh scripts are done executing, I will be left with the main directory. |
Why would you want to do this? Smells malicious to me.
|
Deleting a directory from within itself is awkward. You simply cannot do it the straightforward way. Here's me playing around with the idea:
Code:
[a@e temp]$ mkdir delme And yes, why would you want to do this? |
( THISDIR=$(pwd) ; cd .. && rm -rf $THISDIR )
You'll need to make your script executable to do it easily though, instead of calling it with bash. |
Quote:
|
I think this implies he's not malicious:
Quote:
will be occupied by your shell process. you can only do it from another directory. |
Anyway: rm -r $(pwd) should work
|
you should test first Alien ;)
Code:
$ rm -r $(pwd) |
The example of alien did work for me on my linux box.. but in cygwin it will not work.
But I, too, would like to know what is the purpose of the thread starter for this. |
Quote:
|
yeah but no but :)
Code:
billym.>rm -r $(pwd) |
So then I don't know more. Is there a Nexenta os live CD? Then I could try if this depends on used kernel or not.
|
It's my understanding that the Linux kernel references executables by inode, not file name. So, on a Linux system (for most Linux file systems), you can remove an executing file system entry with no problem. (That's why, for example, you can update executing programs -- if you're using a Linux file system. That's also why "defragmenting" is unneeded for most Linux file systems.)
So, as mentioned above, a simple rm -rf ./ should work for you. Aside: You might also consider using the pushd and popd bash commands instead of simple cd commands. |
er, am I the only one who's actually tried it?
:( |
Code:
$ mkdir test |
All times are GMT -5. The time now is 05:56 PM. |