A script within a folder to delete the folder, script, and the folder's contents
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
#!/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.
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.
Last edited by bigearsbilly; 10-16-2007 at 03:01 AM.
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.
Last edited by Alien_Hominid; 10-16-2007 at 07:12 AM.
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
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.
$ 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
Last edited by PTrenholme; 10-16-2007 at 10:47 AM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.