Differences between revisions 16 and 32 (spanning 16 versions)
Revision 16 as of 2007-12-01 02:02:35
Size: 1887
Editor: GreyCat
Comment:
Revision 32 as of 2013-07-25 13:37:13
Size: 3946
Comment: corrections on stat and GNU find.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[Anchor(faq87)]]
== How can I get the permissions of a file without parsing ls -l output? ==
<<Anchor(faq87)>>
== How can I get a file's permissions (or other metadata) without parsing ls -l output? ==
There are several potential ways, most of which are system-specific. They also depend on precisely ''why'' you want the information; in most cases, there will be some other way to accomplish your [[XyProblem|real goal]]. You [[ParsingLs|don't want to parse ls's output]] if there's any possible way to avoid doing so.
Line 4: Line 5:
There are several potential ways, most of which are system-specific. They also depend on precisely ''why'' you want the permissions. Many of the cases where you might ask about permissions -- such as ''I want to find any files with the setuid bit set'' -- can be handled with [[UsingFind#permissions|the find(1) command]].
Line 6: Line 7:
The majority of the cases where you might ask this question -- such as ''I want to find any files with the setuid bit set'' -- can be answered by the information in [:UsingFind#permissions:]. As the page name implies, those answers are based on the `find(1)` command. For some questions, such as ''I want to make sure this file has 0644 permissions'', you don't actually need to ''check'' what the permissions are. You can just use `chmod 0644 myfile` and set them directly. And if you DO actually need to check what the permissions are instead of forcing them, then you can use `find`'s `-perm`.
Line 8: Line 9:
For some questions, such as ''I want to make sure this file has 0644 permissions'', you don't actually need to ''check'' what the permissions are. You can just use `chmod 0644 myfile` and set them directly. If you want to see whether you can read, write or execute a file, there are `test -r`, `-x` and `-w`.
Line 10: Line 11:
If your needs aren't met by any of those, then we can look at a few alternatives: If you want to see whether a file is zero bytes in size or not, you don't need to read the file's size into a variable. You can just use `test -s` instead.
Line 12: Line 13:
 * On GNU/Linux systems, and possibly others, there is a command called `stat(1)`. On older GNU/Linux systems, this command take no options -- just a filename -- and you will have to parse its output.
 {{{$ stat /
If you want to copy the modification time from one file to another, you can use `touch -r`. The `chown` command on some GNU/Linux systems has a `--reference` option that works the same way, letting you copy the owner and group from one file to another.

If your needs aren't met by any of those, and you really feel you ''must'' extract the metadata from a file into a variable, then we can look at a few alternatives:

 * On GNU/Linux systems, *BSD and possibly others, there is a command called `stat(1)`. On older GNU/Linux systems, this command takes no options -- just a filename -- and you will have to parse its output.
 {{{
 $ stat /
Line 20: Line 26:
 Change: Wed Feb 28 15:42:14 2007(00230.22:15:49)}}}  Change: Wed Feb 28 15:42:14 2007(00230.22:15:49)
 
}}}
Line 23: Line 30:
 * On newer GNU/Linux systems, the `stat` command takes arguments which allow you to specify which information you want:
 {{{$ stat -c %a /
 755}}}
 * On newer GNU/Linux systems:
 {{{
 
$ stat -c %a /
 755
 
}}}
Line 27: Line 36:

 With *BSDs (NetBSD, OpenBSD, FreeBSD and their derivatives like Apple OS/X), the syntax is different and you need to extract the permissions from the mode:

 {{{
 mode=$(stat -f %p -- "$filename")
 perm=$(printf %o "$((mode & 07777))")
 }}}
Line 30: Line 46:
 perl -e 'printf "%o\n", 07777 & (stat $ARGV[0])[2]' "$filename"}}}  perl -e 'printf "%o\n", 07777 & (stat $ARGV[0])[2]' "$filename"
 
}}}
Line 32: Line 49:

 * GNU `find` has a `-printf` switch that can print out any metadata of a file:
 {{{
 find "$filename" -prune -printf '%m\n'
 }}}
 That predates GNU `stat` by over a decade and can give metadata for several files in a directory as well. Beware though that for file named `-print`, `(`, `!`... or any of the other `find` predicates, you need to make sure you pass the file name as `./-print`, `./(`, `!` or any other relative or absolute path to the file that doesn't confuse `find`.

 * If your bash is compiled with [[BashLoadable|loadable builtin support]], you can build the `finfo` builtin (type `make` in the `examples/loadables/` subdirectory of your bash source tree), `enable` it, and then use:
 {{{
 $ finfo -o .bashrc
 644
 }}}
 Beware that the `finfo.c` distributed with bash up through 4.0 contains at least one bug (in the `-s` option), so the code has clearly not been tested much. Most precompiled bash packages do not include compiled examples, so this may be a difficult alternative for most users.

How can I get a file's permissions (or other metadata) without parsing ls -l output?

There are several potential ways, most of which are system-specific. They also depend on precisely why you want the information; in most cases, there will be some other way to accomplish your real goal. You don't want to parse ls's output if there's any possible way to avoid doing so.

Many of the cases where you might ask about permissions -- such as I want to find any files with the setuid bit set -- can be handled with the find(1) command.

For some questions, such as I want to make sure this file has 0644 permissions, you don't actually need to check what the permissions are. You can just use chmod 0644 myfile and set them directly. And if you DO actually need to check what the permissions are instead of forcing them, then you can use find's -perm.

If you want to see whether you can read, write or execute a file, there are test -r, -x and -w.

If you want to see whether a file is zero bytes in size or not, you don't need to read the file's size into a variable. You can just use test -s instead.

If you want to copy the modification time from one file to another, you can use touch -r. The chown command on some GNU/Linux systems has a --reference option that works the same way, letting you copy the owner and group from one file to another.

If your needs aren't met by any of those, and you really feel you must extract the metadata from a file into a variable, then we can look at a few alternatives:

  • On GNU/Linux systems, *BSD and possibly others, there is a command called stat(1). On older GNU/Linux systems, this command takes no options -- just a filename -- and you will have to parse its output.

     $ stat /
       File: "/"
       Size: 1024         Filetype: Directory
       Mode: (0755/drwxr-xr-x)         Uid: (    0/    root)  Gid: (    0/    root)
     Device:  8,0   Inode: 2         Links: 25   
     Access: Wed Oct 17 14:58:02 2007(00000.00:00:01)
     Modify: Wed Feb 28 15:42:14 2007(00230.22:15:49)
     Change: Wed Feb 28 15:42:14 2007(00230.22:15:49)

    In this case, one could extract the 0755 from the Mode: line, using awk or similar commands.

  • On newer GNU/Linux systems:
     $ stat -c %a /
     755
    That's obviously a lot easier to parse. With *BSDs (NetBSD, OpenBSD, FreeBSD and their derivatives like Apple OS/X), the syntax is different and you need to extract the permissions from the mode:
     mode=$(stat -f %p -- "$filename")
     perm=$(printf %o "$((mode & 07777))")
  • On systems with perl 5, you can use:
     perl -e 'printf "%o\n", 07777 & (stat $ARGV[0])[2]' "$filename"

    This returns the same octal string that the stat -c %a example does, but is far more portable. (And slower.)

  • GNU find has a -printf switch that can print out any metadata of a file:

     find "$filename" -prune -printf '%m\n'

    That predates GNU stat by over a decade and can give metadata for several files in a directory as well. Beware though that for file named -print, (, !... or any of the other find predicates, you need to make sure you pass the file name as ./-print, ./(, ! or any other relative or absolute path to the file that doesn't confuse find.

  • If your bash is compiled with loadable builtin support, you can build the finfo builtin (type make in the examples/loadables/ subdirectory of your bash source tree), enable it, and then use:

     $ finfo -o .bashrc
     644

    Beware that the finfo.c distributed with bash up through 4.0 contains at least one bug (in the -s option), so the code has clearly not been tested much. Most precompiled bash packages do not include compiled examples, so this may be a difficult alternative for most users.

BashFAQ/087 (last edited 2015-09-28 19:14:40 by GreyCat)