Why Can't I Delete a File with a Bash Script?

Why Can't I Delete a File with a Bash Script?

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.

4

2 Answers

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
3

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
1

Your Answer

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

Alexander Ross
Author

Alexander Ross

Alexander Ross has covered the video game industry for a decade, writing deep dives on game design, esports tournaments, VR developments, and gaming culture.