I'm trying to delete a directory after a series of events take place. That being said, I need to be able to place this in a file to execute once said events take place.

  • By the way, no I haven't tried much because I' unsure what it will do. – les Apr 4 '14 at 19:07
  • BTW - such a noob I was – les Dec 5 '14 at 5:35

You mean something simple like

rm -r -- "$dir"
  • What if you are currently within a subdirectory? Where would you end up (pwd)? – les Apr 4 '14 at 19:07
  • 1
    @les your cwd won't change, but future calls to lookup the cwd will return "no such file or directory". – jordanm Apr 4 '14 at 19:08
  • @les first change your directory; for example cd .. and then rm -r "$dir" – Mohammad Etemaddar Apr 4 '14 at 19:09
  • @les It wouldn't make sense to delete the directory which contains this script, would it? – Hauke Laging Apr 4 '14 at 19:14
  • @HaukeLaging it might :) – les Dec 5 '14 at 5:35

If I well understand what you ask, you need a script you can call after your events ?

Assuming you use bash, create a file (touch myFile.sh), make it executable (chmod +x myFile.sh), then edit :

# The location of the directorie to delete

# Testing if directory exist ([[ -d]]) and deleting the directory
# Using -r for delete a directory, -f to avoid human confirmation
[[ -d ${dir} ]] && rm -Rf ${dir}

Be carefull, no trash with this options -Rf, directory AND files inside will be deleted for ever. If you're not sure of what you're doing, replace the last line, for your tests, by :

[[ -d ${dir} ]] && mv ${dir} /tmp

Is it what you needed ?

Guess it help !

Enjoy !

  • Is there any confirmation on this? I've been past this, but will test it soon. – les Dec 5 '14 at 5:37

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.