LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   A script within a folder to delete the folder, script, and the folder's contents (https://www.linuxquestions.org/questions/programming-9/a-script-within-a-folder-to-delete-the-folder-script-and-the-folders-contents-592096/)

Cyberman 10-15-2007 09:23 PM

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
mkdir ./1/
mkdir ./2/
mkdir ./3/

tasks.sh *note: This is just an example...

Code:

#!/bin/sh
echo "bark"

copy.sh
Code:

#!/bin/sh
cp ./tasks.sh ./1/
cp ./tasks.sh ./2/
cp ./tasks.sh ./3/

execute.sh
Code:

#!/bin/sh
cd ./1/
bash tasks.sh
cd ..
#
cd ./2/
bash tasks.sh
cd ..
#
cd ./3/
bash tasks.sh
cd ..

Now, what I want to do, is insert some commands into the tasks.sh script. After it echos "bark," I want the script to delete the folder it is inside of. I also want the script to delete itself plus the contents of the folder it is inside.

The idea is that after all tasks.sh scripts are done executing, I will be left with the main directory.

matthewg42 10-15-2007 09:52 PM

Why would you want to do this? Smells malicious to me.

student04 10-15-2007 10:54 PM

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
[a@e temp]$ cd delme
[a@e delme]$ rmdir .
rmdir: .: Invalid argument
[a@e delme]$ rmdir ../delme
[a@e delme]$ pwd
/home/a/temp/delme
[a@e delme]$ ll
total 0
[a@e delme]$ touch afile
touch: cannot touch `afile': No such file or directory
[a@e delme]$ mkdir adir
mkdir: cannot create directory `adir': No such file or directory
[a@e delme]$ cd ..
[a@e temp]$ ls
test  test.s  test.s~
[a@e temp]$

Why does the script need to delete the folder it is currently operating in? Can it not simply go up one and delete it that way?

And yes, why would you want to do this?

gnashley 10-16-2007 01:21 AM

( 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.

Alien_Hominid 10-16-2007 02:37 AM

Quote:

Originally Posted by matthewg42 (Post 2925570)
Why would you want to do this? Smells malicious to me.

Malicious sounding questions are not illegal to ask. ;) Like Packet Storm Sec is not illegal to use just because it has Viral DB.

bigearsbilly 10-16-2007 02:58 AM

I think this implies he's not malicious:

Quote:

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.
you won't be able to do it in the directory because it
will be occupied by your shell process.
you can only do it from another directory.

Alien_Hominid 10-16-2007 03:02 AM

Anyway: rm -r $(pwd) should work

bigearsbilly 10-16-2007 03:31 AM

you should test first Alien ;)

Code:

$ rm -r $(pwd)
rm: cannot remove directory `/cygdrive/c/11': Device or resource busy


yongitz 10-16-2007 03:53 AM

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.

Alien_Hominid 10-16-2007 07:08 AM

Quote:

Originally Posted by bigearsbilly (Post 2925830)
you should test first Alien ;)

Code:

$ rm -r $(pwd)
rm: cannot remove directory `/cygdrive/c/11': Device or resource busy


Cygwin uses Windows system API for his Linux emulation so you can't expect that emulation will be 100% accurate, because there are some operations which Windows doesn't allow. Eg. try removing a directory, while you have it opened in another window.

bigearsbilly 10-16-2007 07:17 AM

yeah but no but :)

Code:

billym.>rm -r $(pwd)
rm: Cannot remove any directory in the path of the current working directory
/export/home/billym/1

billym.>uname -a
SunOS primadtpdev 5.8 Generic_108528-20 sun4u sparc SUNW,Ultra-250


Alien_Hominid 10-16-2007 07:22 AM

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.

PTrenholme 10-16-2007 09:13 AM

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.

bigearsbilly 10-16-2007 09:24 AM

er, am I the only one who's actually tried it?

:(

PTrenholme 10-16-2007 10:45 AM

Code:

$ mkdir test
$ pushd test

~/test ~
$ rm -rf $(pwd)
$ popd

~
$ ls -l | grep ^d
total 7208
drwxr-xr-x 2 Peter Peter    4096 2007-07-05 12:34 Desktop
drwxr-xr-x 2 Peter Peter    4096 2007-08-17 10:12 Documents
drwxr-xr-x 6 Peter Peter    4096 2007-10-16 09:17 Downloads
drwxrwxr-x 5 Peter Peter    4096 2007-09-27 15:49 FEBE Data
drwxr-xr-x 3 Peter Peter    4096 2007-04-24 13:29 MRTrch
drwxr-xr-x 2 Peter Peter    4096 2007-04-27 15:23 Music
drwxr-xr-x 2 Peter Peter    4096 2007-04-27 15:23 Pictures
drwxr-xr-x 2 Peter Peter    4096 2007-04-27 15:23 Public
drwxr-xr-x 3 Peter Peter    4096 2007-08-13 20:24 Scripts
drwxrwxr-x 3 Peter Peter    4096 2007-05-28 15:01 smb4k
drwxrwxr-x 2 Peter Peter    4096 2007-08-17 13:53 Sound
drwxr-xr-x 2 Peter Peter    4096 2007-04-27 15:23 Templates
drwx------ 2 Peter Peter    4096 2007-05-05 10:18 tmp
drwxr-xr-x 2 Peter Peter    4096 2007-04-27 15:23 Videos
$ uname -r
2.6.22.9-91.fc7



All times are GMT -5. The time now is 05:56 PM.