Directory Manipulation and Management

From Mintarc Forge

chmod - Change file mode bits

chmod modifies the permissions of files and directories.

Examples:

  • chmod 755 file.txt: Set read, write, execute for owner; read, execute for group and others
  • chmod u+x script.sh: Add execute permission for the owner
  • chmod go-w shared_file: Remove write permission for group and others
  • chmod -R 644 directory/: Recursively set permissions for files in a directory

ls - List directory contents

ls lists files and directories, including their permissions.

Examples:

  • ls -l: Long listing format, showing detailed permissions
  • ls -la: Show all files, including hidden ones, with permissions
  • ls -ld /path/to/directory: Display permissions for a specific directory

SUID (Set User ID):

chmod u+s file: Set SUID bit on a file ls -l /usr/bin/passwd: View SUID permission (shows as 's' in user execute position)

SGID (Set Group ID):

chmod g+s directory: Set SGID bit on a directory ls -ld /var/mail: View SGID permission (shows as 's' in group execute position)

Sticky Bit:

chmod +t /tmp: Set sticky bit on a directory ls -ld /tmp: View sticky bit (shows as 't' in others execute position)

chown - Change file owner and group

chown changes the user and/or group ownership of files and directories.

Examples:

  • chown user:group file.txt: Change both owner and group
  • chown :newgroup shared_dir: Change only the group ownership
  • chown -R owner:group directory/: Recursively change ownership

chgrp - Change group ownership

chgrp changes the group ownership of files and directories.

Examples:

  • chgrp developers project_file: Change group ownership to 'developers'
  • chgrp -R marketing /var/www/html: Recursively change group for a web directory

umask - Set file mode creation mask

umask sets the default permissions for newly created files and directories.

Examples:

  • umask: Display current umask value
  • umask 022: Set umask to remove write permissions for group and others
  • umask -S: Display umask in symbolic notation