94

I tried executing a script with this command:

./home/user/scripts/someScript

but it doesn't work:

No such file or directory

Any ideas?

BeastOfCaerbannog
  • 12,964
  • 10
  • 49
  • 77
UAdapter
  • 17,157
  • 38
  • 78
  • 102
  • 4
    Obviously there isn't such a file or directory. So you give wrong path. The dot there doesn't mean "execute". It's just part of the path. In shell you normally type either files to be executed or commands(which in fact are also files) :) – Pithikos Nov 02 '11 at 14:52
  • get rid of that starting period and try again. Alternatively, ~/scripts/someScript will also work. also keep `` qoutes in mind, and the exec programs ($man exec) – j0h Apr 26 '14 at 02:37

4 Answers4

110

The leading dot in your command means "relative to the current directory". Remove it and it'll refer to "the file someScript in the directory /home/user/scripts:

/home/user/scripts/someScript

If you get "Permission denied", it's either because you do not have sufficient permissions to access the file in the directory of other users or because the file is not executable. To make it executable, run:

chmod +x /home/user/scripts/someScript
Lekensteyn
  • 171,743
  • 65
  • 311
  • 401
55

If your script needs to access resources in the same folder that it is being run from, and you have it specified as relative paths, then your script will break.

I always add a cd $(dirname $0) to the head of my script so the folder containing the script will be the root folder.

naisanza
  • 859
  • 1
  • 9
  • 17
10

Remove the .

If you make the scrip executable with chmod 755 <nameofscript> to run it you only need to type the path to the script.

When you see ./script being used it telling the shell that the script is located on the same directory you are executing it. To use full path you type sh /home/user/scripts/someScript.

Bruno Pereira
  • 72,895
  • 33
  • 199
  • 223
  • `sh /path/to/file` is different from `/path/to/file`. `sh` runs `/bin/sh` which is symlinked to `/bin/dash`. – Lekensteyn Nov 02 '11 at 14:19
  • Just making something clear on the examples you see on the net, normally you see `sh ./somescript` which can also be typed as `sh /path/to/script/scriptitself'. – Bruno Pereira Nov 02 '11 at 14:37
6

I tried many ways and that's working well with me:

(cd ~/user/scripts/; ./someScript)

I get some help from this answer.

eleKai
  • 71
  • 1
  • 2