Musiz
Musiz

Reputation: 71

bash: No such file or directory when file is clearly present

I have a shell script located at /home/pi/scripts/take-snapshot.sh but, whenever I try to execute it, I get a error that the file is not present.

the following commands do not work (assuming in script directory):

/home/pi/scripts/take-snapshot.sh
./take-snapshot.sh
take-snapshot.sh
bash /home/pi/scripts/take-snapshot.sh

the following do work and will bring up the shell file (not a new file):

vi take-snapshot.sh
nano take-snapshot.sh

Upvotes: 7

Views: 67759

Answers (4)

tripleee
tripleee

Reputation: 189387

A common and quite confusing problem is that Linux will sometimes display "file not found" for a script which clearly exists, when the actual problem is that the interpreter specified in the shebang line on the first line of the script is the file which doesn't exist.

There are multiple scenarios where this can happen; several of these are covered by existing older answers.

  • The shebang line has a DOS carriage return, and the file which doesn't exist is (for example) /bin/sh^M (where the ^M notation symbolizes a control-M character, also known as CR, \r, hex 0x0D, octal 015, decimal 13, ␍, etc etc). See also Are shell scripts sensitive to encoding and line endings? The TLDR is to run dos2unix on the mangled script file, or perhaps tr -d '\015' <broken >fixed (and then probably chmod a+x fixed to make it executable). Going forward, don't use a Windows editor to write your scripts.

  • The shebang points to the wrong location; for example, bin/sh instead of the correct absolute path /bin/sh (if you are new to this, perhaps see also Difference between ./ and ~/), or /bin/bash when Bash is actually installed in e.g. /usr/local/bin/bash or /usr/bin/bash. At the interactive shell prompt, command -v bash will display the correct location (assuming the interpreter is installed on your system, and your PATH is correctly confgured).

  • The interpreter isn't installed at all. In stripped-down Busybox or Alpine environments, for example, Bash won't be installed by default, only /bin/sh. If the script doesn't actually require any Bash features, you might be able to simply change the shebang to /bin/sh instead; but in the general case, see Difference between sh and bash

    Similarly, Python 3 or Python 2 or TCL or Expect or Perl or what have you might not be installed. How exactly to fix this will depend on the environment; for Debian-based systems (including Ubuntu, etc), check https://packages.debian.org/ for the missing package. Other environments will typically have different tools, but probably similar facilities.

    In the absolutely worst case, you may need to port the required software to a previously unsupported platform, which is quite a significant undertaking; but for common software, chances are somebody else already did that, and ideally, provides prebuilt packages for your platform.

Upvotes: 1

Ben Weinstein-Raun
Ben Weinstein-Raun

Reputation: 46

I recently had this error because my binary was built dynamically on a very different system than I was trying to run it on (ubuntu vs nixos). I now know that this error can be triggered when an executable needs an "interpreter" (or, as in my case, a linker) to be in a specific location, different than its location on the running system. Building the binary on a system matching the target fixed the problem.

Upvotes: 2

adbdkb
adbdkb

Reputation: 2161

I have seen this error when the line endings are windows EOL characters. It doesn't give any other error, just the above "No file or directory".

Check the EOL character and convert it to linux EOL, if it is windows and try to run the script again.

Upvotes: 7

PMF
PMF

Reputation: 17185

The most likely cause is that your file is not executable. Bash is a bit confusing in that it reports the file as "not found", even though you only don't have permissions to execute it. Run ls -l and check the permissions. The leftmost column should show an "x" at least for the current user. It will usually look something like -rwxr-xr-x for a file you have created yourself.

Run chmod +x take-snapshot.sh to fix the permissions if they don't match.

Upvotes: 5

Related Questions