2

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.

  • 1
    Get rid of the exit 0 and check for error messages. – ChrisF Feb 10 '11 at 23:31
  • And it DOES work when you just execute rm -rf myjunk.out from the command line? What is triggering execution of this script? – Pylsa Feb 10 '11 at 23:34
  • 2
    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 ? – Torian Feb 11 '11 at 01:48
  • @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 – CamelBlues Feb 11 '11 at 18:28

2 Answers2

5

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
Hieu
  • 611
0

The -f option inhibits error messages. The man page says:

Attempt to remove the files without prompting for confirmation, regardless of the file's permissions. If the file does not exist, do not display a diagnostic message or modify the exit status to reflect an error. The -f option overrides any previous -i options.

I've emphasised the part that is relevant; to get worthwhile warnings, you should remove the option flags from your rm command:

rm myjunk.out
Toby Speight
  • 4,967