Skip to content

Instantly share code, notes, and snippets.

@realeroberto
Created February 16, 2018 16:51
Show Gist options
  • Save realeroberto/8b61441d97842544888d18106c4111da to your computer and use it in GitHub Desktop.
Save realeroberto/8b61441d97842544888d18106c4111da to your computer and use it in GitHub Desktop.
Convert a file mode string into the octal value
#!/bin/bash
# Convert a file mode string (e.g., -rw-rw-r--) into the octal value
function perm2octal()
{
local perm=${1:1} # remove first char (the file type)
res=$(echo "$perm" | sed 's/[^-]/1/g; s/-/0/g')
res=$(echo "ibase=2; obase=8; $res" | bc)
echo "$res"
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment