LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
Thread Tools
Old 10-15-2007, 10:23 PM   #1
Cyberman
Member
 
Registered: Aug 2005
Distribution: Debian Stable
Posts: 117
Thanked: 1
A script within a folder to delete the folder, script, and the folder's contents


[Log in to get rid of this advertisement]
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.
Cyberman is offline     Reply With Quote
Old 10-15-2007, 10:52 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu (x86), Debian (PPC)
Posts: 3,495
Thanked: 6
Why would you want to do this? Smells malicious to me.
matthewg42 is offline     Reply With Quote
Old 10-15-2007, 11:54 PM   #3
student04
Member
 
Registered: Jan 2004
Location: Atlanta
Distribution: XP in VMware, CentOS 5.3, Ubuntu, Gentoo
Posts: 647
Thanked: 4
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?
student04 is offline     Reply With Quote
Old 10-16-2007, 02:21 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 3,697
Thanked: 103
( 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.
gnashley is offline     Reply With Quote
Old 10-16-2007, 03:37 AM   #5
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,220
Thanked: 7
Quote:
Originally Posted by matthewg42 View Post
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.

Last edited by Alien_Hominid; 10-16-2007 at 04:02 AM..
Alien_Hominid is offline     Reply With Quote
Old 10-16-2007, 03:58 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,712
Thanked: 21
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.

Last edited by bigearsbilly; 10-16-2007 at 04:01 AM..
bigearsbilly is offline     Reply With Quote
Old 10-16-2007, 04:02 AM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,220
Thanked: 7
Anyway: rm -r $(pwd) should work
Alien_Hominid is offline     Reply With Quote
Old 10-16-2007, 04:31 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,712
Thanked: 21
you should test first Alien

Code:
$ rm -r $(pwd) 
rm: cannot remove directory `/cygdrive/c/11': Device or resource busy
bigearsbilly is offline     Reply With Quote
Old 10-16-2007, 04:53 AM   #9
yongitz
Member
 
Registered: Nov 2005
Location: philippines
Distribution: ::Slackware:: ::Redhat:: ::Debian:: ::Ubuntu::
Posts: 116
Thanked: 0
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.

Last edited by yongitz; 10-16-2007 at 04:57 AM..
yongitz is offline     Reply With Quote
Old 10-16-2007, 08:08 AM   #10
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,220
Thanked: 7
Quote:
Originally Posted by bigearsbilly View Post
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.

Last edited by Alien_Hominid; 10-16-2007 at 08:12 AM..
Alien_Hominid is offline     Reply With Quote
Old 10-16-2007, 08:17 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,712
Thanked: 21
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
bigearsbilly is offline     Reply With Quote
Old 10-16-2007, 08:22 AM   #12
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,220
Thanked: 7
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.

Last edited by Alien_Hominid; 10-16-2007 at 08:25 AM..
Alien_Hominid is offline     Reply With Quote
Old 10-16-2007, 10:13 AM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 2,847
Thanked: 78
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.
PTrenholme is offline     Reply With Quote
Old 10-16-2007, 10:24 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: FreeBSD, Debian, Puppy
Posts: 2,712
Thanked: 21
er, am I the only one who's actually tried it?

bigearsbilly is offline     Reply With Quote
Old 10-16-2007, 11:45 AM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 2,847
Thanked: 78
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

Last edited by PTrenholme; 10-16-2007 at 11:47 AM..
PTrenholme is offline     Reply With Quote

Reply

Bookmarks


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
want to 'tar' a folder without some contents of folder shipon_97 Linux - Newbie 5 10-13-2007 05:21 AM
How to create a cron job to delete a folder's contents? s0n|k Linux - Newbie 1 10-05-2007 08:59 AM
Script to delete files from specified folder mrrx7 Solaris / OpenSolaris 4 08-27-2007 04:50 AM
Script for folder emptiness? samw5 Linux - Software 4 04-26-2006 12:03 PM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 11:54 AM


All times are GMT -5. The time now is 03:45 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
RSS2  LQ Podcast
RSS2  LQ Radio
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration