Search code examples
linuxbashshellcentosrhel

Why do I get 'permission denied' after using ./file2 in Linux?


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?


Solution

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