0

I read in Linux Command Line and Shell Scripting Bible by Richard Blum Christine Bresnahan that :

The umask value is subtracted from the full permission set for an object. The full permission for a file is mode 666, but for a directory it’s 777.

So if a file can't be have read, write, execute permissions at once, does a user have to change file everytime he needs to perform an action. What should a programmer do if he needs to test his code? And what about other file types? Thanks in advance.

  • you misunderstand. files can be executable. – cas Aug 25 at 9:37
  • Sure, but I think files can't have all 3 permissions at once . – Eye Patch Aug 25 at 9:43
  • you still misunderstand. they can. this is something you can easily test yourself: touch file ; chmod 777 file; ls -l file – cas Aug 25 at 9:46
  • Yes thank you. But what does the author mean by The full permission for a file is mode 666, but for a directory it’s 777. Can you please explain it to me. I'm a little confused now. – Eye Patch Aug 25 at 9:50
  • i haven't read that book, but it looks to me as if the author is talking specifically about how the umask setting affects permissions when a file or directory is created. – cas Aug 25 at 9:55
1

The umask value is subtracted from the full permission set for an object.

This isn't true. Or at least it's inaccurate and simplified to a common case.

  • First, the base value is not "the full set of permissions", but instead whatever the process creating the file sets as the file permissions. Granted, for regular files, this is usually 0666: the idea being that (say) a text editor shouldn't decide what the permissions of a file should be, but the user should be allowed to decide it via the umask.

    But a process creating the file doesn't need to use 0666 as the file permissions. For private files (think SSH keys), 0600 would be used, so that regardless of the umask, the file is never accessible by others than the owner. Also, for executable files, 0777 could be used so that the resulting file is executable.

    For directories, 0777 would be common, since the x bit is practically as necessary as the r bit for general use. For general data files, it isn't, so this is why the common case is 0666 for files, and 0777 for directories.

    The base permissions used could of course be something else, but those are likely to be the common cases.

  • Second, the value of the umask is not subtracted, but masked out. Subtraction implies a carry from one bit to the next up, and subtracting e.g. the 0007 from 0666 would result in 0657. That's not how the umask works, and that would not be useful.

Note that the umask is only used when a file is created, the Linux man page also calls it the "file mode creation mask". After that, chmod() can be used to change the permissions without being limited by the umask.

So if a file can't be have read, write, execute permissions at once,

Sure, they can. It's just not useful for nonexecutable files.

0

Because by default files are not executable, while directories are.

  • The "execute" flag on a directory lets you open files that are in it, so is necessary in most cases (the "read" flag lets you list the directory contents, but not open the files in it).
  • The "execute" flag in a regular file makes it executable, and this isn't generally wanted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.