File Permissions Linux command.
File Permissions Linux command.
Command: chmod
Definition:
The chmod command in Linux is used to change the permissions of a file or directory. It allows users to
define who can read, write, or execute a file. Permissions can be set for the file owner, group, and
others.
Pattern to Use:
chmod [options] mode file
Options:
4. --reference=RFILE — Use the permissions of RFILE as the reference for the change.
Mode:
Permissions are set using either symbolic mode or numeric (octal) mode.
Symbolic Mode:
o r — Read permission.
o w — Write permission.
o x — Execute permission.
o + — Add permission.
o - — Remove permission.
u — User (owner).
g — Group.
o — Others.
o 0 — No permission.
o 1 — Execute permission.
o 2 — Write permission.
o 4 — Read permission.
Examples:
1. Add execute permission for the user: Command: chmod u+x file.txt
This adds the execute permission for the file owner (user).
3. Set read, write, and execute permissions for the owner, and read and execute permissions for
the group and others (numeric mode): Command: chmod 755 file.txt
This sets the permissions as rwxr-xr-x.
5. Set read and write permissions for the owner and the group, and only read for others:
Command: chmod 664 file.txt
This gives rw-rw-r-- permissions.
6. Apply changes recursively to directories and their contents: Command: chmod -R 755
/path/to/dir
This applies rwxr-xr-x permissions to all files and directories inside /path/to/dir.
Command: chown
Definition:
The chown command in Linux is used to change the ownership of a file or directory. You can specify a
new owner and/or group for the file, allowing you to manage file permissions more effectively.
Pattern to Use:
chown [options] owner[:group] file
Options:
5. --reference=RFILE — Use the ownership of RFILE as the reference for the change.
owner — New owner of the file (can be a user name or user ID).
group — New group of the file (can be a group name or group ID).
o If only the group is to be changed, the owner can be left empty (:group).
Examples:
2. Change both owner and group of a file: Command: chown user1:group1 file.txt
This changes the ownership of file.txt to user1 and the group to group1.
3. Change ownership recursively for all files in a directory: Command: chown -R user1:group1
/path/to/dir
This changes the ownership of dir and all its contents to user1 and group1.
8. Change the owner and group of all files in a directory: Command: chown -R user1:group1
/home/user/*
This recursively changes the owner and group for all files in /home/user.
9. Change owner and group of a file to the current user and group: Command: chown $USER:
$USER file.txt
This changes the owner and group of file.txt to the current user and group.
10. Change ownership of a directory without affecting files inside: Command: chown
user1:group1 /path/to/dir/
This changes only the ownership of the directory itself, not the files inside.
Command: setfacl
Definition:
The setfacl command in Linux is used to set Access Control Lists (ACLs) on files and directories. ACLs
provide a more flexible permission mechanism than the traditional owner-group-other model, allowing
you to grant or deny specific permissions to individual users or groups.
Pattern to Use:
setfacl [options] acl_specification file
Options:
6. -d — Set default ACLs for directories (applied to newly created files/directories inside).
Examples:
Command: getfacl
Definition:
The getfacl command in Linux is used to display the Access Control List (ACL) of files and directories. It
shows both the standard permissions and the additional ACL entries for a file or directory.
Pattern to Use:
getfacl [options] file
Options:
Examples:
Command: umask
Definition:
The umask command in Linux is used to set or display the default file permission mask for newly created
files and directories. It determines which permission bits will be disabled by default when files or
directories are created.
Pattern to Use:
umask [options] [mask]
Options:
3. Mask value (octal) — Specifies the new umask value to be applied (e.g., umask 022).
Examples: