Umask command operation demonstration

Source: Internet
Author: User

Umask is a common command. It is used to set a permission mask when creating a file or directory, usually 0022:

shell> umask0022

Note: 0022 is equivalent to the first 0 in 022,0022 for sticky-bit. This article will not discuss it. If you are interested, check it yourself.

To facilitate the demonstration later, you must first create two files with 7-zip on Windows:

Foo.zip includes: Directory foo_dir, file foo_file1_bar.tar includes: Directory bar_dir, file bar_file)

Of course, you don't need Windows. I just want to shake the burden later.

Upload these two files to Linux, and then let me learn the director's voice and shout: Action!

Operation foo.zip

shell> unzip foo.zip

The permission to view directories and files is as follows:

foo_dir:755drwxr-xr-x)foo_file:644-rw-r–r–)

This is because the base permission of the directory is 777, and the base permission of the file is 666. Because the default umask permission is 022:

Directory: 777-022 = 755 file: 666-022 = 644

Note: Permission calculation is actually a binary bitwise and operation. For details, refer to All About the umask and Permissions.

Operate bar.tar

shell> tar xf bar.tar

This is a little different. We need to run the tar command twice.

For the first time: run the tar command as root and view the permissions of directories and files, respectively:

bar_dir:777drwxrwxrwx)bar_file:777-rwxrwxrwx)

The second time: run the tar command as a non-root user, and then view the Directory and file permissions, respectively:

bar_dir:755drwxr-xr-x)bar_file:755-rwxr-xr-x)

Note: before running the tar command, remember to delete the directories and files generated by the last run of the tar command to avoid any impact.

There are some differences between the two results. man tar can find some useful descriptions:

-p, –same-permissions, –preserve-permissionsextract all protection information –no-same-permissionsapply user’s umask when extracting files instead of recorded permissions

When using the tar command, for root, the default value is extract all protection information. For non-root users, the default value is apply user's umask when extracting files instead of recorded permissions.

We previously used Windows to create a file and then upload it to Linux. Therefore, the initial permission for the file and directory is 777. When we run the tar command as root, we retain this permission. When we run the tar command as a non-root user, although umask will be applied, tar is a bit special. It uses the permissions of directories and files as the benchmark permission, rather than the 777 directory) and 666 files), so the final permission is changed to 755777-022 ).

By the way, I will introduce how to easily modify the permissions of directories or files:

shell> find /path -type d | xargs chmod 755shell> find /path -type f | xargs chmod 644

This cram school class!

Original article: http://huoding.com/2010/12/11/26

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.