0

I have a script running in a folder. If a command fails I want to delete the folder the containing the script. Is that possible?

Edit: Based on comment, I took out what I tried.

  • 1
    That is "delete the directory you ran the script from", rather than "delete the directory containing the script". Which do you mean? What is "a command fail"ing in this context? – Michael Homer Apr 26 '17 at 2:57
  • The command failing is superfluous. I just have a script foo in a folder bar, is it possible to have a command in foo that will delete bar? – user1543042 Apr 26 '17 at 3:26
  • 1
    a BIG word of warning : the script deletes pwd, which is NOT the directory the script is in but the directory the USER is in when launching the script. if you did: (**DO NOT TRY THIS **) cd ; path/to/script , you would delete YOUR WHOLE HOMEDIRECTORY AND EVERYTHING UNDERNEATH! ... (especially bad on some OSes where root's homedir is "/" ... ). Instead in your script: mydir="$(cd -P "$(dirname "$0");pwd)" ; echo "the script '$0' is in: ${mydir} " and delete ${mydir} instead, once you're sure it is correctly reflecting the real script's directory. – Olivier Dulac Apr 26 '17 at 12:50
4

I give an answer as I'm worried anyone would try the OP's suggestion...

a BIG word of warning : the script shown in the question deletes the directory given by pwd, which is NOT the directory the script is in but the directory the USER is in when launching the script.

If one does: (**DO NOT TRY THIS **) cd ; /path/to/thatscript they would delete THE USER'S WHOLE HOMEDIRECTORY (as "cd" went back into it) AND EVERYTHING UNDERNEATH! ...

(This is especially bad on some OSes where root's homedir is "/" ... ).

Instead in your script you should:

mydir="$(cd -P "$(dirname "$0");pwd)"     
      #retrieve the script's absolute path, 
      #even if the script was called via ../relative/path/to/script
echo "the script '$0' is in: ${mydir} "
...
# and then (if you really want this.... but I think it's a bad idea!)
# rm -rf "${mydir:-/tmp/__UNDEFINED__}"  #deletes ${mydir}, if defined
# once you're sure it is correctly reflecting the real script's directory.
1

Yes,you can.

and this is what will happen:

[root@server ~]# mkdir new&&cd new
[root@server new]# echo -e "#! /bin/bash\nrm -rf" >remove.sh
[root@server new]# cat remove.sh
#!/bin/bash
rm -rf ~/new
[root@server new]# ls
remove.sh
[root@server new]# bash remove.sh
[root@server new]# ls

(empty here,all file and directory are deleted, but we are still inside this "new" folder before we change our directory)
[root@server new]# cd ..
[root@server ~]# ls new 
ls: cannot access new: No such file or directory
  • 2
    a BIG word of warning : the script deletes pwd, which is NOT the directory the script is in but the directory the USER is in when launching the script. if you did: (**DO NOT TRY THIS **) cd ; new/remove.sh , you would have deleted YOUR WHOLE HOMEDIRECTORY AND EVERYTHING UNDERNEATH! ... (especially bad on some OSes where root's homedir is "/" ... ) – Olivier Dulac Apr 26 '17 at 12:47
  • @OlivierDulac yes,i am aware of this,just trying to show the possibility of deleting the script-containing directory – FrontENG Apr 26 '17 at 13:52
  • 1
    +1 for showing that the new directory did not finally get deleted until its last open handle (your current bash session being cd-ed into new) closed. – Jesse Chisholm Jul 27 '17 at 20:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.