How to get octal file permissions on Linux/Unix command line

I am a new Linux command line user. How do I find file permissions in octal format such as 0644 from the bash command line running on a Linux or Unix operating systems? How can I get the octal permissions for a file? How do I get octal file permissions on Linux/Unix?

You need to use the stat command to view or get octal file permissions for given filename. By default the ls command will not display the permissions on a file in octal form. The permission in octal form is useful for many commands such as chmod command and other Linux or Unix sysadmin tasks. This quick tutorial shows how to use the stat command to view octal file permissions.
 

Tutorial details
Difficulty levelEasy
Root privilegesNo
RequirementsLinux or Unix terminal
CategoryFile Management
OS compatibilityBSD • Linux • macOS • Unix
Est. reading time4 minutes

How to get octal file permissions on Linux/Unix command line

To get file or file system status try the stat command. However, the syntax for stat is different on GNU/Linux and BSD/macOS Unix like systems. Let us see how to display file or file system status using the stat command.

Getting octal file permissions on Linux CLI

The syntax is as follows to get octal file permissions on Linux:
stat fileName
stat -c 'Format' file

The default output is as follows on a GNU/Linux. Let us see info for the /etc/passwd as follows:
stat /etc/passwd
Sample outputs:

  File: /etc/passwd
  Size: 2605      	Blocks: 8          IO Block: 4096   regular file
Device: fd01h/64769d	Inode: 15469225    Links: 1
Access: (0644/-rw-r--r--)  Uid: (    0/    root)   Gid: (    0/    root)
Access: 2017-11-05 19:06:44.879876898 +0530
Modify: 2017-10-16 00:15:53.236876717 +0530
Change: 2017-10-16 00:15:53.236876717 +0530
 Birth: -

There is no need to use the grep command or egrep command to filter info displayed by the stat command. Instead, to print octal file permissions on a GNU/Linux try the following syntax. For instance:
stat -c '%a' /etc/passwd
Sample outputs:

644

Want to store outputs in a shell variable? Try the following syntax:

1

2

filename="/etc/hosts"

result="$(stat -c '%a' "$filename")"

Then display result using the echo command or printf command as follows:

1

2

3

printf "%s\n" "$result"

## OR ##

echo "$result"

Another useful command that displays file permissions in both format:
stat -c '%A %a %n' /etc/passwd
Sample outputs:

-rw-r--r-- 644 /etc/passwd

Where format is as follows:

FormatDescription
%aaccess rights in octal (note ‘#’ and ‘0’ printf flags)
%Aaccess rights in human readable form
%bnumber of blocks allocated (see %B)
%Bthe size in bytes of each block reported by %b
%CSELinux security context string
%ddevice number in decimal
%Ddevice number in hex
%fraw mode in hex
%Ffile type
%ggroup ID of owner
%Ggroup name of owner
%hnumber of hard links
%iinode number
%mmount point
%nfile name
%Nquoted file name with dereference if symbolic link
%ooptimal I/O transfer size hint
%stotal size, in bytes
%tmajor device type in hex, for character/block device special files
%Tminor device type in hex, for character/block device special files
%uuser ID of owner
%Uuser name of owner
%wtime of file birth, human-readable; – if unknown
%Wtime of file birth, seconds since Epoch; 0 if unknown
%xtime of last access, human-readable
%Xtime of last access, seconds since Epoch
%ytime of last data modification, human-readable
%Ytime of last data modification, seconds since Epoch
%ztime of last status change, human-readable
%Ztime of last status change, seconds since Epoch

A note about macOS/BSD stat command

The syntax is as follows for BSD stat command to get octal file permissions on BSD or macOS:
stat fileName
stat -f "Format" fileName

The default output is as follows:
stat /etc/passwd
Sample outputs:

1518287693 66065 -rw-r--r-- 1 root wheel 4294967295 1724 "Oct 28 13:50:15 2017" "Oct 28 13:50:15 2017" "Oct 28 13:50:15 2017" "Oct 28 13:50:15 2017" 4096 9 0x800 /etc/passwd

To just see octal file permissions
stat -f "%OLp" /etc/passwd
Sample outputs:

644

To displays file permissions in both format:
stat -f "%Sp %OLp %N" /etc/passwd
Sample outputs:

-rw-r--r-- 644 /etc/passwd

Dealing with multiple files

Say you want to get info about the /etc/passwd, /etc/group, and /etc/shadow, then use bash for loop as follows on your machine (double click to select the code):

1

2

3

4

for f in /etc/passwd /etc/group /etc/shadow

do

  echo "File '$f' : $(stat -c "%a"  "$f")"

done

Here is what I see:

File '/etc/passwd' : 644
File '/etc/group' : 644
File '/etc/shadow' : 640

Conclusion

You just learned how to use the stat command to get octal file permissions from the command line. See stat command man page by typing the man command or help command. For example:
man 1 stat

#ifndef _LINUX_MODULE_PARAMS_H #define _LINUX_MODULE_PARAMS_H /* (C) Copyright 2001, 2002 Rusty Russell IBM Corporation */ #include <linux/init.h> #include <linux/stringify.h> #include <linux/kernel.h> /* You can override this manually, but generally this should match the module name. */ #ifdef MODULE #define MODULE_PARAM_PREFIX /* empty */ #else #define MODULE_PARAM_PREFIX KBUILD_MODNAME "." #endif /* Chosen so that structs with an unsigned long line up. */ #define MAX_PARAM_PREFIX_LEN (64 - sizeof(unsigned long)) /* This struct is here for syntactic coherency, it is not used */ #define __MODULE_INFO_DISABLED(name) \ struct __UNIQUE_ID(name) {} #ifdef CONFIG_MODULE_STRIPPED #define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO_DISABLED(name) #else #define __MODULE_INFO_STRIP(tag, name, info) __MODULE_INFO(tag, name, info) #endif #ifdef MODULE #define __MODULE_INFO(tag, name, info) \ static const char __UNIQUE_ID(name)[] \ __used __attribute__((section(".modinfo"), unused, aligned(1))) \ = __stringify(tag) "=" info #else /* !MODULE */ /* This struct is here for syntactic coherency, it is not used */ #define __MODULE_INFO(tag, name, info) __MODULE_INFO_DISABLED(name) #endif #define __MODULE_PARM_TYPE(name, _type) \ __MODULE_INFO(parmtype, name##type, #name ":" _type) /* One for each parameter, describing how to use it. Some files do multiple of these per line, so can't just use MODULE_INFO. */ #define MODULE_PARM_DESC(_parm, desc) \ __MODULE_INFO_STRIP(parm, _parm, #_parm ":" desc) struct kernel_param; /* * Flags available for kernel_param_ops * * NOARG - the parameter allows for no argument (foo instead of foo=1) */ enum { KERNEL_PARAM_OPS_FL_NOARG = (1 << 0) }; struct kernel_param_ops { /* How the ops should behave */ unsigned int flags; /* Returns 0, or -errno. arg is in kp->arg. */ int (*set)(const char *val, const struct kernel_param *kp); /* Returns length written or -errno. Buffer is 4k (ie. be short!) */ int (*get)(char *buffer, const struct kernel_param *kp); /* Optional function to free kp->arg when module unloaded. */ void (*free)(void *arg); }; /* * Flags available for kernel_param * * UNSAFE - the parameter is dangerous and setting it will taint the kernel */ enum { KERNEL_PARAM_FL_UNSAFE = (1 << 0) }; struct kernel_param { const char *name; struct module *mod; const struct kernel_param_ops *ops; const u16 perm; s8 level; u8 flags; union { void *arg; const struct kparam_string *str; const struct kparam_array *arr; }; }; extern const struct kernel_param __start___param[], __stop___param[]; /* Special one for strings we want to copy into */ struct kparam_string { unsigned int maxlen; char *string; }; /* Special one for arrays */ struct kparam_array { unsigned int max; unsigned int elemsize; unsigned int *num; const struct kernel_param_ops *ops; void *elem; }; /** * module_param - typesafe helper for a module/cmdline parameter * @value: the variable to alter, and exposed parameter name. * @type: the type of the parameter * @perm: visibility in sysfs. * * @value becomes the module parameter, or (prefixed by KBUILD_MODNAME and a * ".") the kernel commandline parameter. Note that - is changed to _, so * the user can use "foo-bar=1" even for variable "foo_bar". * * @perm is 0 if the the variable is not to appear in sysfs, or 0444 * for world-readable, 0644 for root-writable, etc. Note that if it * is writable, you may need to use kernel_param_lock() around * accesses (esp. charp, which can be kfreed when it changes). * * The @type is simply pasted to refer to a param_ops_##type and a * param_check_##type: for convenience many standard types are provided but * you can create your own by defining those variables. * * Standard types are: * byte, short, ushort, int, uint, long, ulong * charp: a character pointer * bool: a bool, values 0/1, y/n, Y/N. * invbool: the above, only sense-reversed (N = true). */ #define module_param(name, type, perm) \ module_param_named(name, name, type, perm) /** * module_param_unsafe - same as module_param but taints kernel */ #define module_param_unsafe(name, type, perm) \ module_param_named_unsafe(name, name, type, perm) /** * module_param_named - typesafe helper for a renamed module/cmdline parameter * @name: a valid C identifier which is the parameter name. * @value: the actual lvalue to alter. * @type: the type of the parameter * @perm: visibility in sysfs. * * Usually it's a good idea to have variable names and user-exposed names the * same, but that's harder if the variable must be non-static or is inside a * structure. This allows exposure under a different name. */ #define module_param_named(name, value, type, perm) \ param_check_##type(name, &(value)); \ module_param_cb(name, &param_ops_##type, &value, perm); \ __MODULE_PARM_TYPE(name, #type) /** * module_param_named_unsafe - same as module_param_named but taints kernel */ #define module_param_named_unsafe(name, value, type, perm) \ param_check_##type(name, &(value)); \ module_param_cb_unsafe(name, &param_ops_##type, &value, perm); \ __MODULE_PARM_TYPE(name, #type) /** * module_param_cb - general callback for a module/cmdline parameter * @name: a valid C identifier which is the parameter name. * @ops: the set & get operations for this parameter. * @perm: visibility in sysfs. * * The ops can have NULL set or get functions. */ #define module_param_cb(name, ops, arg, perm) \ __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, 0) #define module_param_cb_unsafe(name, ops, arg, perm) \ __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, -1, \ KERNEL_PARAM_FL_UNSAFE) /** * <level>_param_cb - general callback for a module/cmdline parameter * to be evaluated before certain initcall level * @name: a valid C identifier which is the parameter name. * @ops: the set & get operations for this parameter. * @perm: visibility in sysfs. * * The ops can have NULL set or get functions. */ #define __level_param_cb(name, ops, arg, perm, level) \ __module_param_call(MODULE_PARAM_PREFIX, name, ops, arg, perm, level, 0) #define core_param_cb(name, ops, arg, perm) \ __level_param_cb(name, ops, arg, perm, 1) #define postcore_param_cb(name, ops, arg, perm) \ __level_param_cb(name, ops, arg, perm, 2) #define arch_param_cb(name, ops, arg, perm) \ __level_param_cb(name, ops, arg, perm, 3) #define subsys_param_cb(name, ops, arg, perm) \ __level_param_cb(name, ops, arg, perm, 4) #define fs_param_cb(name, ops, arg, perm) \ __level_param_cb(name, ops, arg, perm, 5) #define device_param_cb(name, ops, arg, perm) \ __level_param_cb(name, ops, arg, perm, 6) #define late_param_cb(name, ops, arg, perm) \ __level_param_cb(name, ops, arg, perm, 7) /* On alpha, ia64 and ppc64 relocations to global data cannot go into read-only sections (which is part of respective UNIX ABI on these platforms). So 'const' makes no sense and even causes compile failures with some compilers. */ #if defined(CONFIG_ALPHA) || defined(CONFIG_IA64) || defined(CONFIG_PPC64) #define __moduleparam_const #else #define __moduleparam_const const #endif /* This is the fundamental function for registering boot/module parameters. */ #define __module_param_call(prefix, name, ops, arg, perm, level, flags) \ /* Default value instead of permissions? */ \ static const char __param_str_##name[] = prefix #name; \ static struct kernel_param __moduleparam_const __param_##name \ __used \ __attribute__ ((unused,__section__ ("__param"),aligned(sizeof(void *)))) \ = { __param_str_##name, THIS_MODULE, ops, \ VERIFY_OCTAL_PERMISSIONS(perm), level, flags, { arg } } /* Obsolete - use module_param_cb() */ #define module_param_call(name, set, get, arg, perm) \ static const struct kernel_param_ops __param_ops_##name = \ { .flags = 0, (void *)set, (void *)get }; \ __module_param_call(MODULE_PARAM_PREFIX, \ name, &__param_ops_##name, arg, \ (perm) + sizeof(__check_old_set_param(set))*0, -1, 0) /* We don't get oldget: it's often a new-style param_get_uint, etc. */ static inline int __check_old_set_param(int (*oldset)(const char *, struct kernel_param *)) { return 0; } #ifdef CONFIG_SYSFS extern void kernel_param_lock(struct module *mod); extern void kernel_param_unlock(struct module *mod); #else static inline void kernel_param_lock(struct module *mod) { } static inline void kernel_param_unlock(struct module *mod) { } #endif #ifndef MODULE /** * core_param - define a historical core kernel parameter. * @name: the name of the cmdline and sysfs parameter (often the same as var) * @var: the variable * @type: the type of the parameter * @perm: visibility in sysfs * * core_param is just like module_param(), but cannot be modular and * doesn't add a prefix (such as "printk."). This is for compatibility * with __setup(), and it makes sense as truly core parameters aren't * tied to the particular file they're in. */ #define core_param(name, var, type, perm) \ param_check_##type(name, &(var)); \ __module_param_call("", name, &param_ops_##type, &var, perm, -1, 0) /** * core_param_unsafe - same as core_param but taints kernel */ #define core_param_unsafe(name, var, type, perm) \ param_check_##type(name, &(var)); \ __module_param_call("", name, &param_ops_##type, &var, perm, \ -1, KERNEL_PARAM_FL_UNSAFE) #endif /* !MODULE */ /** * module_param_string - a char array parameter * @name: the name of the parameter * @string: the string variable * @len: the maximum length of the string, incl. terminator * @perm: visibility in sysfs. * * This actually copies the string when it's set (unlike type charp). * @len is usually just sizeof(string). */ #define module_param_string(name, string, len, perm) \ static const struct kparam_string __param_string_##name \ = { len, string }; \ __module_param_call(MODULE_PARAM_PREFIX, name, \ &param_ops_string, \ .str = &__param_string_##name, perm, -1, 0);\ __MODULE_PARM_TYPE(name, "string") /** * parameq - checks if two parameter names match * @name1: parameter name 1 * @name2: parameter name 2 * * Returns true if the two parameter names are equal. * Dashes (-) are considered equal to underscores (_). */ extern bool parameq(const char *name1, const char *name2); /** * parameqn - checks if two parameter names match * @name1: parameter name 1 * @name2: parameter name 2 * @n: the length to compare * * Similar to parameq(), except it compares @n characters. */ extern bool parameqn(const char *name1, const char *name2, size_t n); /* Called on module insert or kernel boot */ extern char *parse_args(const char *name, char *args, const struct kernel_param *params, unsigned num, s16 level_min, s16 level_max, void *arg, int (*unknown)(char *param, char *val, const char *doing, void *arg)); /* Called by module remove. */ #ifdef CONFIG_SYSFS extern void destroy_params(const struct kernel_param *params, unsigned num); #else static inline void destroy_params(const struct kernel_param *params, unsigned num) { } #endif /* !CONFIG_SYSFS */ /* All the helper functions */ /* The macros to do compile-time type checking stolen from Jakub Jelinek, who IIRC came up with this idea for the 2.4 module init code. */ #define __param_check(name, p, type) \ static inline type __always_unused *__check_##name(void) { return(p); } extern const struct kernel_param_ops param_ops_byte; extern int param_set_byte(const char *val, const struct kernel_param *kp); extern int param_get_byte(char *buffer, const struct kernel_param *kp); #define param_check_byte(name, p) __param_check(name, p, unsigned char) extern const struct kernel_param_ops param_ops_short; extern int param_set_short(const char *val, const struct kernel_param *kp); extern int param_get_short(char *buffer, const struct kernel_param *kp); #define param_check_short(name, p) __param_check(name, p, short) extern const struct kernel_param_ops param_ops_ushort; extern int param_set_ushort(const char *val, const struct kernel_param *kp); extern int param_get_ushort(char *buffer, const struct kernel_param *kp); #define param_check_ushort(name, p) __param_check(name, p, unsigned short) extern const struct kernel_param_ops param_ops_int; extern int param_set_int(const char *val, const struct kernel_param *kp); extern int param_get_int(char *buffer, const struct kernel_param *kp); #define param_check_int(name, p) __param_check(name, p, int) extern const struct kernel_param_ops param_ops_uint; extern int param_set_uint(const char *val, const struct kernel_param *kp); extern int param_get_uint(char *buffer, const struct kernel_param *kp); #define param_check_uint(name, p) __param_check(name, p, unsigned int) extern const struct kernel_param_ops param_ops_long; extern int param_set_long(const char *val, const struct kernel_param *kp); extern int param_get_long(char *buffer, const struct kernel_param *kp); #define param_check_long(name, p) __param_check(name, p, long) extern const struct kernel_param_ops param_ops_ulong; extern int param_set_ulong(const char *val, const struct kernel_param *kp); extern int param_get_ulong(char *buffer, const struct kernel_param *kp); #define param_check_ulong(name, p) __param_check(name, p, unsigned long) extern const struct kernel_param_ops param_ops_ullong; extern int param_set_ullong(const char *val, const struct kernel_param *kp); extern int param_get_ullong(char *buffer, const struct kernel_param *kp); #define param_check_ullong(name, p) __param_check(name, p, unsigned long long) extern const struct kernel_param_ops param_ops_charp; extern int param_set_charp(const char *val, const struct kernel_param *kp); extern int param_get_charp(char *buffer, const struct kernel_param *kp); extern void param_free_charp(void *arg); #define param_check_charp(name, p) __param_check(name, p, char *) /* We used to allow int as well as bool. We're taking that away! */ extern const struct kernel_param_ops param_ops_bool; extern int param_set_bool(const char *val, const struct kernel_param *kp); extern int param_get_bool(char *buffer, const struct kernel_param *kp); #define param_check_bool(name, p) __param_check(name, p, bool) extern const struct kernel_param_ops param_ops_bool_enable_only; extern int param_set_bool_enable_only(const char *val, const struct kernel_param *kp); /* getter is the same as for the regular bool */ #define param_check_bool_enable_only param_check_bool extern const struct kernel_param_ops param_ops_invbool; extern int param_set_invbool(const char *val, const struct kernel_param *kp); extern int param_get_invbool(char *buffer, const struct kernel_param *kp); #define param_check_invbool(name, p) __param_check(name, p, bool) /* An int, which can only be set like a bool (though it shows as an int). */ extern const struct kernel_param_ops param_ops_bint; extern int param_set_bint(const char *val, const struct kernel_param *kp); #define param_get_bint param_get_int #define param_check_bint param_check_int /** * module_param_array - a parameter which is an array of some type * @name: the name of the array variable * @type: the type, as per module_param() * @nump: optional pointer filled in with the number written * @perm: visibility in sysfs * * Input and output are as comma-separated values. Commas inside values * don't work properly (eg. an array of charp). * * ARRAY_SIZE(@name) is used to determine the number of elements in the * array, so the definition must be visible. */ #define module_param_array(name, type, nump, perm) \ module_param_array_named(name, name, type, nump, perm) /** * module_param_array_named - renamed parameter which is an array of some type * @name: a valid C identifier which is the parameter name * @array: the name of the array variable * @type: the type, as per module_param() * @nump: optional pointer filled in with the number written * @perm: visibility in sysfs * * This exposes a different name than the actual variable name. See * module_param_named() for why this might be necessary. */ #define module_param_array_named(name, array, type, nump, perm) \ param_check_##type(name, &(array)[0]); \ static const struct kparam_array __param_arr_##name \ = { .max = ARRAY_SIZE(array), .num = nump, \ .ops = &param_ops_##type, \ .elemsize = sizeof(array[0]), .elem = array }; \ __module_param_call(MODULE_PARAM_PREFIX, name, \ &param_array_ops, \ .arr = &__param_arr_##name, \ perm, -1, 0); \ __MODULE_PARM_TYPE(name, "array of " #type) extern const struct kernel_param_ops param_array_ops; extern const struct kernel_param_ops param_ops_string; extern int param_set_copystring(const char *val, const struct kernel_param *); extern int param_get_string(char *buffer, const struct kernel_param *kp); /* for exporting parameters in /sys/module/.../parameters */ struct module; #if defined(CONFIG_SYSFS) && defined(CONFIG_MODULES) extern int module_param_sysfs_setup(struct module *mod, const struct kernel_param *kparam, unsigned int num_params); extern void module_param_sysfs_remove(struct module *mod); #else static inline int module_param_sysfs_setup(struct module *mod, const struct kernel_param *kparam, unsigned int num_params) { return 0; } static inline void module_param_sysfs_remove(struct module *mod) { } #endif #endif /* _LINUX_MODULE_PARAMS_H */ 这段代码中module_param的作用
最新发布
12-20
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值