Fixing “No such file or directory” Errors

Problem

You’ve set the execute permission as described in Forgetting to Set Execute Permissions, but when you run the script you get a “No such file or directory” error.

Solution

Try running the script using bash explicitly:

$ bash ./busted

If it works, you have some kind of permissions error, or a typo in your shebang line. If you get a bunch more errors, you probably have the wrong line endings. This can happen if you edit the file on Windows (perhaps via Samba), or if you’ve simply copied the file around.

To fix it, try the dos2unix program if you have it, or see Converting DOS Files to Linux Format. Note that if you use dos2unix it will probably create a new file and delete the old one, which will change the permissions and might also change the owner or group and affect hard links. If you’re not sure what any of that means, the key point is that you’ll probably have to chmod it again (Forgetting to Set Execute Permissions).

Discussion

If you really do have bad line endings (i.e., anything that isn’t ASCII 10 or hex 0a), the error you get depends on your shebang line. Here are some examples for a script named busted:

$ cat busted#!/bin/bash -
echo "Hello World!"

# This works
$ ./busted
Hello World!

# But if the file gets DOS line endings, we get:
$ ./busted
: invalid option
Usage: /bin/bash [GNU long option] [option] ...
[...]

# Different shebang line
$ cat ./busted
#!/usr/bin/env bash
echo "Hello World!"

$ ./busted
: No such file or directory ...

Get bash Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.