Find the answer to your Linux question:
Results 1 to 6 of 6
I need a script that can be executed in its directory to delete the directory and its contents. I tried using something like... $bash ./folder/del-script.sh Code: #!/bin/rmdir echo "If this ...
  1. #1
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261

    A script that can be executed in its directory to delete the directory and its ...

    I need a script that can be executed in its directory to delete the directory and its contents.

    I tried using something like...

    $bash ./folder/del-script.sh

    Code:
    #!/bin/rmdir
    echo "If this show, then it probably wasn't deleted."
    exit 0
    It didn't delete the script, the directory, nor its contents....
    What do I have to do to create a script like that?

  2. #2
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    This didn't work:
    Code:
    wally:~/monday/2/two$ cat 2.sh
    #!/bin/sh
    
    rm -rf .
    
    wally:~/monday/2/two$ ls -lR
    .:
    total 8
    -rwx------    1 wally    wally          21 Oct 15 15:24 2.sh
    drwx------    2 wally    wally        4096 Oct 15 15:23 zwei
    
    ./zwei:
    total 0
    wally:~/monday/2/two$ 2.sh
    rm: cannot remove `.' or `..'
    wally:~/monday/2/two$
    This did work:
    Code:
    wally:~/monday/2/two$ cat 2.sh
    #!/bin/sh
    
    xxx=`pwd`
    
    cd ..
    
    rm -rf $xxx
    
    wally:~/monday/2/two$ ls -lR
    .:
    total 8
    -rwx------    1 wally    wally          42 Oct 15 15:27 2.sh
    drwx------    2 wally    wally        4096 Oct 15 15:23 zwei
    
    ./zwei:
    total 0
    wally:~/monday/2/two$ 2.sh
    wally:~/monday/2/two$ ls -l
    total 0
    wally:~/monday/2/two$ cd ..
    wally:~/monday/2$ ls -l
    total 40
    -rwx------    1 wally    wally          42 Oct 15 15:27 2.sh
    -rw-------    1 wally    wally         415 Oct 15 15:26 t1
    -rw-------    1 wally    wally         432 Oct 15 15:28 t2
    wally:~/monday/2$
    Hope this helps.

  3. #3
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    It somewhat hurt more than it helped.

    It deleted the ./Desktop folder and whatever else what inside of the home folder.
    Not cool.

    Perhaps because I had made it one of the following:

    rm -rf ./
    rm -rf .
    rm -rf

    I had put the script into $HOME/testing/
    I executed it from $HOME, I think...
    I did that, because if I executed it inside of $HOME/testing/, then the folder wasn't destroyed... odd, but that's what happened.

    I can't remember which one of the three. I know two didn't work, so I used one of them... can't remember much when everything I just had was recently deleted and I didn't see it coming...

    Anyway, luckily, I was using an Ubuntu Live-CD, so things are alright.

    Perhaps giving more details about what I'm trying to do will help.

    Alright, so it's like this.

    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.

  4. #4
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    In your detailed example, in each case you will be setting the "current directory" to be the same directory as the one containing the script.

    In that case, my original solution should work quite well.

  5. #5
    Linux User Agent-X's Avatar
    Join Date
    May 2005
    Location
    Dimension X
    Posts
    261
    I see. I was being ignorant.
    I didn't use the xxx, because I figured it had something to do with passwords.
    Because I'm using a Live-CD, I ignored it.
    I'm not too sure what the xxx stuff was all about.

    Regardless, I used the cat print-out verbatim and turned it into a script.
    It did work, so thanks.

    Now, I just need to try it again to check for validity.

  6. #6
    Linux Engineer wje_lf's Avatar
    Join Date
    Sep 2007
    Location
    Mariposa
    Posts
    1,192
    Quoth Agent-X:
    I didn't use the xxx, because I figured it had something to do with passwords.
    I was going to use zzz, but I was afraid I'd fall asleep.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...