LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Home Forums Tutorials Articles Register
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
  Search this Thread
Old 10-15-2007, 09:23 PM   #1
Cyberman
Member
 
Registered: Aug 2005
Distribution: Debian Stable
Posts: 218

Rep: Reputation: 17
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.
 
Old 10-15-2007, 09:52 PM   #2
matthewg42
Senior Member
 
Registered: Oct 2003
Location: UK
Distribution: Kubuntu 12.10 (using awesome wm though)
Posts: 3,530

Rep: Reputation: 65
Why would you want to do this? Smells malicious to me.
 
Old 10-15-2007, 10:54 PM   #3
student04
Member
 
Registered: Jan 2004
Location: USA
Distribution: macOS, OpenBSD
Posts: 669

Rep: Reputation: 34
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?
 
Old 10-16-2007, 01:21 AM   #4
gnashley
Amigo developer
 
Registered: Dec 2003
Location: Germany
Distribution: Slackware
Posts: 4,928

Rep: Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612Reputation: 612
( 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.
 
Old 10-16-2007, 02:37 AM   #5
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
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 03:02 AM.
 
Old 10-16-2007, 02:58 AM   #6
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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 03:01 AM.
 
Old 10-16-2007, 03:02 AM   #7
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
Anyway: rm -r $(pwd) should work
 
Old 10-16-2007, 03:31 AM   #8
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
you should test first Alien

Code:
$ rm -r $(pwd) 
rm: cannot remove directory `/cygdrive/c/11': Device or resource busy
 
Old 10-16-2007, 03:53 AM   #9
yongitz
Member
 
Registered: Nov 2005
Location: Davao City, Philippines
Distribution: RHEL, CentOS, Ubuntu, Mint
Posts: 139

Rep: Reputation: 20
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 03:57 AM.
 
Old 10-16-2007, 07:08 AM   #10
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
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 07:12 AM.
 
Old 10-16-2007, 07:17 AM   #11
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
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
 
Old 10-16-2007, 07:22 AM   #12
Alien_Hominid
Senior Member
 
Registered: Oct 2005
Location: Lithuania
Distribution: Hybrid
Posts: 2,247

Rep: Reputation: 53
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 07:25 AM.
 
Old 10-16-2007, 09:13 AM   #13
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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.
 
Old 10-16-2007, 09:24 AM   #14
bigearsbilly
Senior Member
 
Registered: Mar 2004
Location: england
Distribution: Mint, Armbian, NetBSD, Puppy, Raspbian
Posts: 3,515

Rep: Reputation: 239Reputation: 239Reputation: 239
er, am I the only one who's actually tried it?

 
Old 10-16-2007, 10:45 AM   #15
PTrenholme
Senior Member
 
Registered: Dec 2004
Location: Olympia, WA, USA
Distribution: Fedora, (K)Ubuntu
Posts: 4,187

Rep: Reputation: 354Reputation: 354Reputation: 354Reputation: 354
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 10:47 AM.
 
  


Reply



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 04:21 AM
How to create a cron job to delete a folder's contents? s0n|k Linux - Newbie 1 10-05-2007 07:59 AM
Script to delete files from specified folder mrrx7 Solaris / OpenSolaris 4 08-27-2007 03:50 AM
Script for folder emptiness? samw5 Linux - Software 4 04-26-2006 11:03 AM
need a script to delete all files from a folder and subfolders cccc Programming 1 03-04-2005 10:54 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:31 PM.

Main Menu
Advertisement
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
Twitter: @linuxquestions
Open Source Consulting | Domain Registration