access

file managementlinux
The access command is one of the most frequently used commands in Linux/Unix-like operating systems. access The access command checks whether the calling user or process has access permissions for a file or files. It returns 0 (success) if the user has the specified access rights, and non-zero (failure) otherwise.

Quick Reference

Command Name:

access

Category:

file management

Platform:

linux

Basic Usage:

access [options] [arguments]

Common Use Cases

  • 1

    File permission checking

    Check if a file can be accessed with current permissions

  • 2

    Script automation

    Test file accessibility before performing operations in scripts

  • 3

    Security validation

    Verify file access rights for security auditing purposes

  • 4

    Error handling

    Prevent script failures by checking file accessibility beforehand

Syntax

access [-acfmrwx] [-e] [-h] file ...

Options

Option Description
-e Check if file exists, regardless of access mode
-r Check if file is readable
-w Check if file is writable
-x Check if file is executable
-f Check if file exists and is a regular file
-a Show access information in verbose mode (implementation dependent)
-c Check the file's access by context (SELinux)
-m Check the file's access by mode (ACL)
-h Display help information and exit

Examples

How to Use These Examples

The examples below show common ways to use the access command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

Basic Examples:

Check if file exists and is readable
access -r /etc/passwd && echo "File exists and is readable"
Check if file exists and is writable
access -w /etc/shadow || echo "No write permission to this file"
Check if file exists and is executable
access -x /bin/bash && echo "File is executable"
Check if file exists (regardless of permissions)
access -e myfile.txt && echo "File exists"

Advanced Examples:

Check if all specified access modes are available
access -rw /var/log/syslog && echo "File is both readable and writable"
Check file existence in scripts
if access -f config.txt; then
echo "Config file found" else echo "Config file not found" fi
Check multiple permission types
access -rwx script.sh && ./script.sh
Check for execution permission in current directory
access -x . && echo "Current directory is searchable"

Try It Yourself

Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

Understanding Syntax

Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

Notes

Return Values:

The access command returns the following exit status codes:

  • 0 (success): All requested access permissions are granted
  • 1: File does not exist or one of the requested permissions is denied
  • 2: Invalid arguments

Access Modes:

The access command can check for the following types of access:

  • Read (-r): Can the file be opened for reading?
  • Write (-w): Can the file be modified or written to?
  • Execute (-x): Can the file be run as a program or script?
  • Existence (-e): Does the file exist (regardless of permissions)?
  • Regular file (-f): Does the file exist and is it a regular file (not a directory or device)?

Combining Access Modes:

You can combine multiple access modes to check for multiple permissions at once. For example, access -rw file.txt checks if the file is both readable and writable.

Use in Scripts:

The access command is particularly useful in shell scripts to check for file existence and permissions before attempting operations that might fail. It's often used in conditional statements.

Important Notes:

  • Access checks are performed using the real user ID and group ID of the process, not the effective IDs
  • The access command only checks the permission bits and ACLs, it does not verify if a file can actually be read/written (disk space, locks, etc.)
  • Some systems implement access as a shell built-in, which may have slightly different options
  • There is a race condition between checking for access and actually using the file - the permissions could change in between
  • The command is based on the POSIX access() system call

Alternatives:

In shell scripts, you can often use test operators like [ -r file ], [ -w file ], or [ -x file ] instead of the access command. These are built into most shells and provide similar functionality.

Common Use Cases

File permission checking

Check if a file can be accessed with current permissions

Script automation

Test file accessibility before performing operations in scripts

Security validation

Verify file access rights for security auditing purposes

Error handling

Prevent script failures by checking file accessibility beforehand

Cross-platform compatibility

Test file access across different filesystem types

Related Commands

These commands are frequently used alongside access or serve similar purposes:

Use Cases

1

File permission checking

Check if a file can be accessed with current permissions

2

Script automation

Test file accessibility before performing operations in scripts

3

Security validation

Verify file access rights for security auditing purposes

4

Error handling

Prevent script failures by checking file accessibility beforehand

5

Cross-platform compatibility

Test file access across different filesystem types

Learn By Doing

The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the access command works in different scenarios.

$ access
View All Commands