Create a listing of all possible permissions and their octal representation.

touch /tmp/$$;for N in `seq -w 0 7777|grep -v [89]`; do chmod $N /tmp/$$; P=`ls -l /tmp/$$ | awk '{print $1}'`; echo $N $P; done;rm /tmp/$$

3
By: jyoder
2009-03-05 15:24:41

What Others Think

awesome
linuxrawkstar · 659 weeks ago
Interesting, but is there actually any practical application?
goodevilgenius · 659 weeks ago
Practical? Eh. I have a buddy that just can't seem to understand setting perms and how the different octal values work. So I wrote this for him. I was assuming that looking at the patterns would help explain what value impacted what permission, but instead he saved it to a file and just searches for the permission string he wants. I guess it works either way. :D
jyoder · 659 weeks ago
I'd never use $$ for temporary files, better use mktemp.
OJM · 659 weeks ago
There are several problems with this: * The use of /tmp/$$ isn't portable across shells, making it a Bash-ism. As already mentioned, use mktmep, or just choose an actual filename, like /tmp/FILE. If you insist on '$$', then quote it, as should be the first rule of all shell variables and filenames. * The use of backticks is a nightmare for maintenance and readability. Use $() instead. Unless you absolutely need portability to the archaic Unix shells, such as Bourne, $() is much more readable and functional. * The for loop is looping through numbers 0 to 7777, but our binary friends for permissions don't include permissions with the number '8' or '9' like 0999 or 3898, and so forth. Bit-shifting should be used here. * Further, the grep -v [89] is another Bash-ism, making this "one-liner" non-portable. * The endless scroll of nearly 7000 lines isn't teaching anything to anybody. Instead of the endless scrollback, the user should be asked for input, the input should be validated as a permission, then echoed back in octal to show the permission set. It's clean, and it actually teaches. * This is why shell-scripting sucks. Rather, use a more capable language, such as Perl, Python or Ruby. Shell scripts should only be used as simple solutions to simple problems.
atoponce · 658 weeks and 5 days ago
@stoponce: /tmp/$$ is portable. Even Sun's braindead /bin/sh (which is far from POSIX compliance) understands it. It doesn't understand $(), though. Looping from 0 to 7777 (decimal) and then removing all numbers that contain the digits 8 or 9 is a perfect way to generate octal numbers. There's no need for bit-shifting here. grep -v [89] is not a bashism, but it will fail if the current directory happens to contain a file named "8" or "9" (or both), whether bash or another shell. Therefore the pattern needs to be put in quotes.
inof · 646 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?

commandlinefu.com 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.

» http://twitter.com/commandlinefu
» http://twitter.com/commandlinefu3
» http://twitter.com/commandlinefu10

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: