How To Use ‘access’ command in Linux - Servo Node

How To Use ‘access’ command in Linux - Servo Node

access command in linux is basically used to check if the calling program has access to an specific file, or it checks if the file is in existence or not.

access command in Linux is basically used to check if the calling program has access to an specific file, or it checks if the file is in existence or not. This can be accomplished by utilisation of calling process’s UID and GID. In case if the specifiedpathname is symbolic link, it’s dereferenced. Below is how to use access command with syntax:

int access(const char *pathname, int mode); in the above syntax, const char *pathname refers to specified file’s path while the int mode is referred as flags which are mostly used as:

F_OK : Used for checking existence of file R_OK : Checks the file for read permissions W_OK : Checks the file for write permissions X_OK : Checks the file for execute permissions However, in case if the access command fails to access the file, it will return the value as -1, otherwise 0 indicating to success.

Example: In the example below, we have used F_OK flag to check if the file is existing or not. Obviously, if the file is found, it will return 0 else -1 as result.

clude<stdio.h> #include<unistd.h> #include<errno.h> #include<sys/types.h> #include<sys/stat.h> #include<fcntl.h>

extern int errno;

int main(int argc, const char *argv[]){ int fd = access("sample.txt", F_OK); if(fd == -1){ printf("Error Number : %d\n", errno); perror("Error Description:"); } else printf("No error\n"); return 0; }

use ‘access’ command in linux

What is Geek Coin

What is GeekCash, Geek Token

Best Visual Studio Code Themes of 2021

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

How to Delete Directories and Files in Linux using Command Line

In this remove or delete directories and files linux tutorial guide, you will learn how to remove empty directory and non empty directory linux using command line. And as well as how to remove/file files linux using command line.

Install Linux from USB Device or Boot into Live Mode Using Unetbootin and dd Command

We will be creating USB boot-able image in two different way: Unetbootin – is an open-source tool for creating bootable Live USB drives for Ubuntu, Fedora, and other Linux distributions.dd – is a command-line tool for converting and copying files.

Using Log Analysis with Command Line Tools to Explore Linux Log

Using Log Analysis with Command Line Tools to Explore Linux Log. Take a look at this guide to using log analysis with command line tools to dig into a Linux log.

5 Best CLI Tools to Search Plain-Text Data Using Regular Expressions

This guide takes a tour of some of the best command-line tools that are used for searching matching strings or patterns in text files. These tools are usually used alongside regular expressions – shortened as REGEX – which are unique strings for describing a search pattern.

gpm Command in Linux with Examples

A Computer Science portal for geeks. It contains well written, well thought and well explained computer science and programming articles, quizzes and practice/competitive programming/company interview Questions.