A lightweight, POSIX-compliant shell script to set file permissions using the familiar 10-character mode string seen in ls -l output.
The standard chmod command uses either symbolic notation (u=rwx,g=rx,o=) or octal notation (755). Sometimes, it's more intuitive to copy the exact permission string you see in a long listing.
This tool allows you to do just that:
# Instead of this:
chmod u=rwx,g=rx,o= myfile
chmod 750 myfile
# You can do this:
chmodx -rwxr-x--- myfile- Intuitive Syntax: Use permission strings directly from
ls -l. - File Type Safety: Validates that the target is a file (
-) or directory (d) as specified in the mode, preventing accidental mistakes. - Verbose Mode: Use the
--verboseflag to see a confirmation of the change and the equivalentchmodcommand. - Robust & Portable: Written in pure POSIX
sh, making it compatible with any Unix-like system (Linux, macOS, BSD). - Comprehensive Error Checking: Validates input thoroughly and provides clear error messages.
- Download the
chmodxscript. - Make it executable:
chmod +x chmodx
- Move it to a directory in your
$PATH(e.g.,~/bin/or/usr/local/bin/).sudo mv chmodx /usr/local/bin/
chmodx [--verbose] MODE FILE...
MODE: A 10-character string.- Character 1: File type (
-for file,dfor directory). - Characters 2-10: Permissions for User, Group, and Other (each 3 chars). Each character must be
r(read),w(write),x(execute), or-(no permission).
- Character 1: File type (
FILE...: One or more files or directories to modify.
Set permissions for a file to rwxr-x--- (user: RWX, group: RX, other: none):
chmodx -rwxr-x--- my_script.shSet permissions for a directory to rwxr-xr-x and show the result:
chmodx --verbose drwxr-xr-x my_directory/
# Output:
# drwxr-xr-x 2 user group 4096 Jan 1 12:34 my_directory
# Reproduce with:
# chmod 755 'my_directory'The script parses the 10-character string, validates it, and converts the 9 permission characters into a 3-digit octal number (e.g., rwxr-x--- becomes 750). It then uses the standard chmod command with this octal number to set the permissions. It includes safety checks to ensure the target file type matches the specified mode.
This project is released under the MIT License. Feel free to use, modify, and distribute it.
Contributions are welcome! Please feel free to:
- Open an issue to report a bug or suggest a new feature.
- Submit a Pull Request with improvements.
- Please ensure changes maintain POSIX
shcompatibility. - Update tests and documentation as needed.
- Please ensure changes maintain POSIX