Why can't I delete a file with a Bash script?

17,849

First, make sure that you can delete myjunk.out without running your script; if not, check file attribute with lsattr.

Second, You don't need to providing exit 0;

Later, point a path to myjunk.out, such as:

rm -rf /path/to/myjunk.out
Share:
17,849

Related videos on Youtube

Dony Joe
Author by

Dony Joe

Updated on September 17, 2022

Comments

  • Dony Joe
    Dony Joe over 1 year

    I can't seem to run rm from a Bash script and remove a file.

    #!/bin/bash
    rm -rf myjunk.out
    exit 0;
    

    doesn't remove myjunk.out.

    • Pylsa
      Pylsa about 13 years
      And it DOES work when you just execute rm -rf myjunk.out from the command line? What is triggering execution of this script?
    • Torian
      Torian about 13 years
      Just for the record, -r is meant to descend on directories (Recursive), so if you intend to remove a file, -r is futile. What error do you get ? How do you run this script ? What are the permissions on this script ?
    • Dony Joe
      Dony Joe about 13 years
      @BloodPhilla it's triggered because myjunk.out is generated earlier in the script.... @Torian -r was in there because I was just trying stuff out
  • Keith
    Keith about 13 years
    Also remove the -r flag. it is unnecessary and dangerous.
  • Dony Joe
    Dony Joe about 13 years
    Actually, taking out the exit 0; allowed rm installed.out to execute properly. Thanks!
  • Emmanuel Devaux
    Emmanuel Devaux about 10 years
    What is the explanation of : Why the "exit 0" will make the rm not working
  • Chenmunka
    Chenmunka about 8 years
    This is more of a comment than an answer.