In Linux, every file and directory holds some permissions either it can be “read”, “write,” or “execute”.
In some cases, the file only has the “read” permission, which means that the user can only read that file and can not execute it. If the user executes that file, an error occurs in such a situation, such as “cannot open output file permission denied”.
In this guide, the causes and the possible solutions are described for the issue “cannot open output file permission denied”. The table of content of this guide is as follow:
- Reason: Unauthorized Access
- Solution: Access/Grant the File Permissions
- Alternative Solution: Change the Output File Ownership
Let’s get started with the first reason of the error.
Reason: Unauthorized Access
The error “cannot open output file permission denied” occurs because the currently logged-in user does not have the directory permissions. Suppose the “test” directory contains the “test.cpp” program having extension “.cpp” as shown in the screenshot:
$ ls -l test.cpp
Convert the “test.cpp” into the executable file “test” to run in the “etc” directory by using the below-mentioned “g++” command. Use the “-o” flag with the command, as it will compile the “test.cpp” file and create the desired executable file “test”:
$ g++ test.cpp -o /etc/test
The above command generates an error because the user does not have the “/etc/” directory permissions. The user only has permission to write the files available in the directory.
To resolve this error, the user must have to change the permissions of the “/etc/” directory
Solution: Access/Grant the Directory Permissions
The user can execute the “test.cpp” program in the “test” directory by changing its permissions. As the owner “itslinuxfoss” does not have the “write” permissions of the “test” directory as shown in the screenshot:
$ ls -l
To change the “test” directory permissions, follow the below-mentioned steps.
Step 1: Change the Directory Permissions
Change the “test” directory permissions to make the “test.cpp” writable and executable. For this purpose, use the “chmod(Change Directory)” command with the combination of “u+wx” and the target directory name in the terminal (Ctrl+Alt+T):
$ chmod u+wx test
The “test” directory now has “write” and “execute” permissions.
Step 2: Execute the Program
Execute the “test.cpp” program, and it will definitely execute now. The compiler “g++” compile that program and displays the output:
$ g++ test.cpp -o ~/test/test
The output verifies that the error “cannot open output file permission denied” has been fixed.
Alternative Solution: Change the Output File Ownership
Here is another solution to open the output file. To perform this task, change the ownership of the specified output file using the “chown” Linux command. Run the below command in the terminal to make the current user an “owner” of the directory:
$ sudo chown -R "$USER:" /path/to/the/directory
The above command contains the following components:
- chown: Linux command that is useful to change the ownership of file/directory.
- -R: This flag represents “recursive” as it changes the ownership of files and subdirectories located in a directory.
- $USER: The current user replaces the global variable.
- path/to/directory: Shows the specified directory path.
The current user is the directory owner and can open the desired output file.
That’s all about the reasons and fixes of the output file permissions error.
The “cannot open output file permission denied” can be resolved by “accessing the Directory Permissions”.The permissions can be changed using the “chmod(Change Directory)” command. This error occurs while executing the “C” program file to the targeted directory that does not have the “write” and “execute” permissions. This guide explains all the possible solutions to resolve the error “cannot open output file permission denied” in Linux.