Stat usage: obtains the number of permissions for a file.

Source: Internet
Author: User

Stat usage: obtains the number of permissions for a file.

Question: The file attribute is-rw-r -- the corresponding permission is 644. How can I use a command to obtain the number corresponding to the permission ??

Example:

[Bkjia @ localhost ~] $ Ll-l
-Rw-r -- 1 bkjia wheel 38 Oct 12 16:29 1.txt

Use the stat command to view
[Bkjia @ localhost ~] $ Stat 1.txt
File: '1.txt'
Size: 38 Blocks: 8 IO Block: 4096 regular file
Device: fd00h/64768d Inode: 390954 Links: 1
Access: (0644/-rw-r --) Uid: (503/bkjia) Gid: (10/wheel)
Access: 16:29:34. 674990005 + 0800
Modify: 16:29:32. 248990536 + 0800
Change: 16:29:32. 248990536 + 0800

To retrieve the corresponding number, use the regular sed awk or cut, head, and tail commands;

Method 1: use regular expressions or commands to obtain
Head, tail, cut

[Bkjia @ localhost ~] $ Stat 1.txt | head-n4 | tail-n1 | cut-d "/"-f1 | cut-d "("-f2
0644


Sed, cut

[Bkjia @ localhost ~] $ Stat 1.txt | sed-n '4p' | cut-d "/"-f1 | cut-d "("-f2
0644

Sed, awk

[Bkjia @ localhost ~] $ Stat 1.txt | sed-n '4p' | awk-F "/" '{print $1}' | awk-F "(" '{print $2 }'
0644

Method 2: stat-c command

[Bkjia @ localhost ~] $ Stat-c % a 1.txt
644

Note: Thinking about method 2 is more important than answering questions. When the command result contains the content we need, we need to consider whether there are specific parameters that can achieve the results we need in one step.

Man stat view help
-C -- format = FORMAT
Use the specified FORMAT instead of the default; output a new line after each use of FORMAT
Use special format instead of default output;
Common parameters are as follows:
% A Access rights in octal display Access permissions, 0644
% A Access rights in human readable form output in human readable form,
% F File type
The gid Number of the Group to which the owner belongs.
% G Group name of owner's Group name
% H Number of hard links hard connections
% I Inode number inode Value
% N File name
% O I/O block size IO block size
% S Total size, Total size of the in bytes file, bytes displayed;
% U User ID of owner's primary uid number
% U User name of owner's Master name
% X Time of last access Time
% X Time of last access as seconds since Epoch timestamp of last access Time
% Y Time of last modification last modified Time
% Y Time of last modification as seconds since Epoch timestamp of last modification Time
% Z Time of last change last modified Time
% Z Time of last change as seconds since Epoch timestamp of the last change Time

The result of using the parameters is as follows:
[Bkjia @ localhost ~] $ Ls-l 1.txt
-Rw-r -- 1 bkjia wheel 38 Oct 12 16:29 1.txt
[Bkjia @ localhost ~] $ Stat-c % a 1.txt
644
[Bkjia @ localhost ~] $ Stat-c % A 1.txt
-Rw-r --
[Bkjia @ localhost ~] $ Stat-c % B 1.txt
8
[Bkjia @ localhost ~] $ Stat-c % B 1.txt
512
[Bkjia @ localhost ~] $ Stat-c % d 1.txt
64768
[Bkjia @ localhost ~] $ Stat-c % F 1.txt
Regular file
[Bkjia @ localhost ~] $ Stat-c % g 1.txt
10
[Bkjia @ localhost ~] $ Stat-c % G 1.txt
Wheel
[Bkjia @ localhost ~] $ Stat-c % u 1.txt
503
[Bkjia @ localhost ~] $ Stat-c % U 1.txt
Bkjia
[Baby @ localhost ~] $ Stat-c % h 1.txt
1
[Bkjia @ localhost ~] $ Stat-c % I 1.txt
390954
[Bkjia @ localhost ~] $ Stat-c % n 1.txt
1. txt
[Bkjia @ localhost ~] $ Stat-c % o 1.txt
4096
[Bkjia @ localhost ~] $ Stat-c % s 1.txt
38

This article permanently updates the link address:

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.