题目:文件属性为-rw-r--r-- 对应权限为644,如何使用命令获取权限对应的数字??

举例如下:

[linuxidc@localhost ~]$ ll -l
-rw-r--r-- 1 linuxidc wheel 38 Oct 12 16:29 1.txt

使用stat命令可以查看
[linuxidc@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--r--) Uid: ( 503/ linuxidc) Gid: ( 10/ wheel)
Access: 2015-10-12 16:29:34.674990005 +0800
Modify: 2015-10-12 16:29:32.248990536 +0800
Change: 2015-10-12 16:29:32.248990536 +0800

取出对应的数字则需要使用正则sed awk 或cut ,head,tail命令;

方法1:使用正则或命令取
head,tail,cut

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

sed,cut

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

sed,awk

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

方法2:stat -c 命令

[linuxidc@localhost ~]$ stat -c %a 1.txt
644

注意:如何想到法二的思考过程,比答题更重要。当命令结果包含我们需要的内容的时候,我们要想到是否有具体的参数能够一步达到我们需要的结果。

man stat 查看帮助
-c --format=FORMAT
use the specified FORMAT instead of the default; output a new line after each use of FORMAT
使用特殊格式代替默认输出;
常用的参数有如下:
%a Access rights in octal        8进制显示访问权限,0644
%A Access rights in human readable form        以人类可读的形式输出,
%F File type        文件的类型
%g Group ID of owner        所属组gid的号码
%G Group name of owner        所属组的名称
%h Number of hard links        硬连接的数量
%i Inode number        inode的值
%n File name        文件名
%o I/O block size        IO块大小
%s Total size, in bytes        文件的总大小,字节显示;
%u User ID of owner        所属主的uid号码
%U User name of owner        所属主的名称
%x Time of last access        最后访问的时间
%X Time of last access as seconds since Epoch        最后访问时间的时间戳
%y Time of last modification        最后修改的时间
%Y Time of last modification as seconds since Epoch        最后修改时间的时间戳
%z Time of last change        最后更改的时间
%Z Time of last change as seconds since Epoch        最后更改的时间的时间戳

使用参数结果如下:
[linuxidc@localhost ~]$ ls -l 1.txt
-rw-r--r-- 1 linuxidc wheel 38 Oct 12 16:29 1.txt
[linuxidc@localhost ~]$ stat -c %a 1.txt
644
[linuxidc@localhost ~]$ stat -c %A 1.txt
-rw-r--r--
[linuxidc@localhost ~]$ stat -c %b 1.txt
8
[linuxidc@localhost ~]$ stat -c %B 1.txt
512
[linuxidc@localhost ~]$ stat -c %d 1.txt
64768
[linuxidc@localhost ~]$ stat -c %F 1.txt
regular file
[linuxidc@localhost ~]$ stat -c %g 1.txt
10
[linuxidc@localhost ~]$ stat -c %G 1.txt
wheel
[linuxidc@localhost ~]$ stat -c %u 1.txt
503
[linuxidc@localhost ~]$ stat -c %U 1.txt
linuxidc
[baby@localhost ~]$ stat -c %h 1.txt
1
[linuxidc@localhost ~]$ stat -c %i 1.txt
390954
[linuxidc@localhost ~]$ stat -c %n 1.txt
1.txt
[linuxidc@localhost ~]$ stat -c %o 1.txt
4096
[linuxidc@localhost ~]$ stat -c %s 1.txt
38

stat用法:获取文件对应权限的数字的更多相关文章

  1. seek和tell的用法--获取文件内容大小(字节)

    /*获取文件中存取的数据内容的大小(字节数) ellg() 和 tellp() 这两个成员函数不用传入参数,返回pos_type 类型的值(根据ANSI-C++ 标准) ,就是一个整数,代表当前get ...

  2. dirname的用法:获取文件的父级目录路径

    命令:dirname 获取文件的路径(到父级目录)用法:dirname file_name [root@bogon opt]# a=$(dirname /mnt/a/b/c/d/a.sh) [root ...

  3. 获取文件的缩略图Thumbnail和通过 AQS - Advanced Query Syntax 搜索本地文件

    演示如何获取文件的缩略图 FileSystem/ThumbnailAccess.xaml <Page x:Class="XamlDemo.FileSystem.ThumbnailAcc ...

  4. PHP获取文件扩展名的多种方法

    PHP获取文件扩展名的N种方法. 第1种方法: function get_extension($file) { substr(strrchr($file, '.'), 1): } 第2种方法: fun ...

  5. ASP php获取文件URL地址等方法

    $_SERVER["HTTP_REFERER"] rss中可用 echo next(explode("=", $_SERVER["QUERY_STRI ...

  6. PHP获取文件的绝对路径

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 ===========PH ...

  7. 【apache tika】apache tika获取文件内容(与FileUtils的对比)

    Tika支持多种功能: 文档类型检测 内容提取 元数据提取 语言检测 重要特点: 统一解析器接口:Tika封装在一个单一的解析器接口的第三方解析器库.由于这个特征,用户逸出从选择合适的解析器库的负担, ...

  8. JAVA 获取文件的MD5值大小以及常见的工具类

    /** * 获取文件的MD5值大小 * * @param file * 文件对象 * @return */ public static String getMD5(File file) { FileI ...

  9. node 的fs.state 获取文件信息

    1. fs.stat()可以获取文件的信息,用法如下: const fs = require('fs'); fs.stat('./book.js',(err,stats)=>{ if(err) ...

随机推荐

  1. 【Cocos2dx 3.3 Lua】SpriteBatchNode和SpriteFrameCache使用

    精灵帧缓存类 一.SpriteFrameCache     精灵帧缓冲类SpriteFrameCache用于存储精灵帧,SpriteFrameCache是一个单例模式,不属于某一个精灵,是所有精灵共享 ...

  2. [LeetCode] 292. Nim Game_Easy tag: Math

    You are playing the following Nim Game with your friend: There is a heap of stones on the table, eac ...

  3. .NET 互联网技术简介

    概述 技术更新太快,尤其是在互联网公司里,很多新的主流技术,我们还是必须要知道和熟练使用的.下面就给大家简单介绍,入门还是需要大家更努力的去深入学习. 目录 Git 入门 常用软件安装及VS插件工具 ...

  4. iOS开发--沙盒

    IOS中的沙盒机制(SandBox)是一种安全体系,它规定了应用程序只能在为该应用创建的文件夹内读取文件,不可以访问其他地方的内容.所有的非代码文件都保存在这个地方,比如图片.声音.属性列表和文本文件 ...

  5. sql可重复执行语句例子

    1.添加字段 SET @add_key_type_to_report = (SELECT IF( (SELECT count(1) FROM INFORMATION_SCHEMA.COLUMNS WH ...

  6. js文本框字数限制

    <!DOCTYPE html> <html> <head> <meta charset="utf-8" /> <title&g ...

  7. MySQL从删库到跑路_高级(四)——存储过程

    作者:天山老妖S 链接:http://blog.51cto.com/9291927 一.存储过程简介 1.存储过程简介 存储过程是一组具有特定功能的SQl语句集组成的可编程的函数,经编译创建并保存在数 ...

  8. 【转】eclipse反编译插件

    原文地址:http://bbs.csdn.net/topics/390263414 离线安装包下载地址一:http://feeling.sourceforge.net/downloads/org.sf ...

  9. Java面向对象---接口

    接口(英文:Interface),在JAVA编程语言中是一个抽象类型,是抽象方法的集合,接口通常以interface来声明.一个类通过继承接口的方式,从而来继承接口的抽象方法. 接口并不是类,编写接口 ...

  10. new Runnable中的 this

    package com.test; public class BB extends AA{ public String bb = "bbbbb"; public void gg() ...