Perl oneliner to print access rights in octal format

perl -e 'printf "%04o\n", (stat shift)[2] & 0777;' file
This prints file access rights in octal - useful when "stat" is unavailable.
Sample Output
$ perl -e 'printf "%04o\n", (stat shift)[2] & 0777;' /etc/passwd

By: zlemini
2012-03-22 15:05:04

What Others Think

ls -l | sed -e 's/--x/1/g' -e 's/-w-/2/g' -e 's/-wx/3/g' -e 's/r--/4/g' -e 's/r-x/5/g' -e 's/rw-/6/g' -e 's/rwx/7/g' -e 's/---/0/g' prints them all. :)
Habitual · 488 weeks and 6 days ago
Any solution handles when sticky bits are set? Can think of a way but its far from elegant (amendment to code posted by Habitual).
cranie · 488 weeks and 2 days ago
perl -e'printf "%o\n",(stat shift)[2] & 07777' file # sticky bits.. doh :-)
cranie · 488 weeks and 2 days ago

What do you think?

Any thoughts on this command? Does it work on your machine? Can you do the same thing with only 14 characters?

You must be signed in to comment.

What's this? is the place to record those command-line gems that you return to again and again. That way others can gain from your CLI wisdom and you from theirs too. All commands can be commented on, discussed and voted up or down.

Share Your Commands

Stay in the loop…

Follow the Tweets.

Every new command is wrapped in a tweet and posted to Twitter. Following the stream is a great way of staying abreast of the latest commands. For the more discerning, there are Twitter accounts for commands that get a minimum of 3 and 10 votes - that way only the great commands get tweeted.


Subscribe to the feeds.

Use your favourite RSS aggregator to stay in touch with the latest commands. There are feeds mirroring the 3 Twitter streams as well as for virtually every other subset (users, tags, functions,…):

Subscribe to the feed for: