-1

For example I have a file named file2 that echoes something.

So in the shell, after typing this

# ./file2

It shows

Permission denied

Where am I wrong here?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Rushabh Dudhe
  • 101
  • 2
  • 8
  • You're trying to execute a file that you don't have permission to. Either your don't have the proper privileges or the file cannot be executed. – Jacob G. Apr 17 '17 at 00:42
  • 1
    The duplicate is a duplicate of the question in your title - which is a different question from what you have in the question body. – Benjamin W. Apr 17 '17 at 02:30

2 Answers2

4

You are trying to execute a file and you do not have the right permissions for this. When you create a new Bash script with your text editor (let's say Vim), you should have the following permissions: -rw-r--r--. As a user, you can then read and write this file, but you cannot execute it with ./.

  1. If you want to execute a file without changing permissions, you can use the following command: bash myFile.sh.

  2. If you want to execute a file with ./, you will have to modify permissions. Something like that is OK: chmod +x myFile.sh.

  3. If you do not want to struggle with ./ and prefer to call myFile.sh from anywhere like other built-in commands, move the executable file in a directory that is in your PATH. /usr/local/bin should be a wise choice. Check your PATH with echo $PATH, just in case...

Badacadabra
  • 8,043
  • 7
  • 28
  • 49
  • 1
    I'd suggest `$HOME/bin` is a better choice (by far) for random commands. Only commands that might be useful to everyone on the machine belong in `/usr/local/bin`. – Jonathan Leffler Apr 17 '17 at 02:53
  • @JonathanLeffler Thanks for your comment. You are perfectly right. I suggested `/usr/local/bin` for convenience, but using `$HOME/bin` is better. ;) – Badacadabra Apr 17 '17 at 17:39
1

. refers to the current working directory and .. refers to the parent directory. So ./file2 means "execute the file named file2 in the current directory". Without the ./, the shell will search for file2 in all the directories of your PATH. If you want to execute it, add the execute bit with chmod +x ./file2 and try again. If you just want to view it, try less ./file2

Joseph Sheedy
  • 6,296
  • 4
  • 30
  • 31