Then, we’ll look at this function through practical examples with code and images so you can better understand this function and use it quickly.
Access() Function to Check the Existence of Files
The access() function queries the existence of a file or directory and the associated read, write, and execute permissions. In the following, we will see the prototype of this function:
This function queries the existence and attributes of the file or directory that is specified by its absolute or relative path in the pathname argument. The path must be specified by a string or a pointer to it that contains the path, filename, and extension that you want to query.
The type of query is specified in the mode input argument by a flag or the result of an OR operation between several of them. The following is a table with the available queries in the access() function:
Flag | Description |
---|---|
F_OK | Check the existence of a file |
R_OK | Check if the file has read permissions |
W_OK | Check if the file has write permissions |
X_OK | Check if the file has executable permissions |
When querying the F_OK, the access() function returns a result that is equal to 0 if the file exists, or -1 if it does not. Similarly, it returns 0 if the file has the requested write, read, or access permissions. Otherwise, it returns -1.
This function is declared in the “unistd.h” header. So, to use the access() function, we need to include it in our code as follows:
How to Check the Existence of a File
In this example, we will see how to verify the existence of the “example.txt” file in the “Documents” directory. To do this, we create this file using the Linux file manager.
In the code that we will use for this example, we will include the “stdio.h” and “unistd.h” headers and open a main() function of type void. In it, we declare the “i” integer and the path pointer of type char to which we assign the string with the relative path of the file.
After declaring the variables, we call the access() function and pass the path string as the first input argument and the F_OK flag as the second argument.
The output argument is the “i” integer which we then use with the returned value in an if-else condition to print a message that indicates whether or not the specified file exists. Let’s see the code for this example:
In the following illustration, we will see the compilation and execution of this code. As can be seen in the figure, the access() function returns 0 which means that the “example.txt” file exists in the “Documents” directory.
Now, we specify the name of a non-existent file in the “path” string, compile it, and run the code.
As we can see in the figure, the access() function returns a result that is equal to -1 when a non-existent file is specified in the string path.
How to Check the Permissions of a File
In this example, we’ll look at how we can check the permissions of a file using the access() function. To do this, we use the code from the previous example and replace the F_OK flag with the W_OK flag in the mode input argument. We also change the messages that this code displays to match the query. Let’s see the following code for this example:
The following image shows the execution of this code. As we can see, the access() function has determined that the “example.txt” file has write permissions.
Now, we go to the Linux manager and enter the properties of the file. In the properties, we go to the “Permissions” menu where we set up the read-only access. After that, we run the compilation again.
As we can see in the figure, the access() function in this execution has determined that the file has no write permissions.
How to Check the Existence of a Directory
In the same way that it queries the existence of a file, the access() function can also query the existence of a directory.
The code for this is the same as in the previous example, except that instead of specifying the path of a file with its name and extension, we specify the directory path. Let’s look at the code to check the existence of the “Documents” directory:
The following image shows the execution of this code which queries the existence of the “Documents” directory:
Conclusion
In this Linux Ways article, we showed you how to check the existence of files and directories using the access() function. We looked at the syntax of this function, its calling method, and the ways to check the existence of files and directories as well as their read, write, and execute permissions. We then applied this function in practical examples where we can see this simple function in action by performing the queries of various types.