-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?

2
  • 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. Commented Apr 17, 2017 at 0: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. Commented Apr 17, 2017 at 2:30

2 Answers 2

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...

Sign up to request clarification or add additional context in comments.

2 Comments

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.
@JonathanLeffler Thanks for your comment. You are perfectly right. I suggested /usr/local/bin for convenience, but using $HOME/bin is better. ;)
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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.