Differences between revisions 12 and 28 (spanning 16 versions)
Revision 12 as of 2007-11-30 23:32:59
Size: 3477
Editor: s20
Comment: Hi!, <a href="http://www.aircargotracking.cn/cargo/map.html">cargo </a>[url="http://www.aircargotracking.cn/cargo/map.html"]cargo [/url]http://www.aircargotracking.cn/cargo/map.html cargo :-[[, <a hr
Revision 28 as of 2011-06-21 02:34:53
Size: 3497
Editor: 64
Comment: spelling/grammar mistake: (this command take*s* no options) -m
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
Hi!, <a href="http://www.aircargotracking.cn/cargo/map.html">cargo
</a>[url="http://www.aircargotracking.cn/cargo/map.html"]cargo
[/url]http://www.aircargotracking.cn/cargo/map.html cargo
 :-[[, <a href="http://www.cargovanrental.cn/boiler/map.html">boiler
</a>[url="http://www.cargovanrental.cn/boiler/map.html"]boiler
[/url]http://www.cargovanrental.cn/boiler/map.html boiler
 369015, <a href="http://www.aircargotracking.cn/cargo/map.html">cargo
</a>[url="http://www.aircargotracking.cn/cargo/map.html"]cargo
[/url]http://www.aircargotracking.cn/cargo/map.html cargo
 =D, <a href="http://www.hotwaterboiler.cn/cigar/map.html">cigar</a>[url="http://www.hotwaterboiler.cn/cigar/map.html"]cigar[/url]http://www.hotwaterboiler.cn/cigar/map.html cigar %-[, <a href="http://www.cargovanrental.cn/boiler/map.html">boiler
</a>[url="http://www.cargovanrental.cn/boiler/map.html"]boiler
[/url]http://www.cargovanrental.cn/boiler/map.html boiler
 vdrxyk, <a href="http://www.lowpressureboiler.cn/crane/map.html">crane
</a>[url="http://www.lowpressureboiler.cn/crane/map.html"]crane
[/url]http://www.lowpressureboiler.cn/crane/map.html crane
 :[[[, <a href="http://www.lowpressureboiler.cn/crane/map.html">crane
</a>[url="http://www.lowpressureboiler.cn/crane/map.html"]crane
[/url]http://www.lowpressureboiler.cn/crane/map.html crane
 kxkt, <a href="http://www.aircargotracking.cn/cargo/map.html">cargo
</a>[url="http://www.aircargotracking.cn/cargo/map.html"]cargo
[/url]http://www.aircargotracking.cn/cargo/map.html cargo
 6178, <a href="http://www.craneoperatorjobs.cn/filters/map.html">filters
</a>[url="http://www.craneoperatorjobs.cn/filters/map.html"]filters
[/url]http://www.craneoperatorjobs.cn/filters/map.html filters
 %-DDD, <a href="http://www.aircargotracking.cn/cargo/map.html">cargo
</a>[url="http://www.aircargotracking.cn/cargo/map.html"]cargo
[/url]http://www.aircargotracking.cn/cargo/map.html cargo
 8), <a href="http://www.asiananal.cn/anal/map.html">anal
</a>[url="http://www.asiananal.cn/anal/map.html"]anal
[/url]http://www.asiananal.cn/anal/map.html anal
 318, <a href="http://www.grannysluts.cn/map.html">mature
</a>[url="http://www.grannysluts.cn/map.html"]mature
[/url]http://www.grannysluts.cn/map.html mature
 8571, <a href="http://www.grannyblowjob.cn/map.html">mature
</a>[url="http://www.grannyblowjob.cn/map.html"]mature
[/url]http://www.grannyblowjob.cn/map.html mature
 neu, <a href="http://www.bdsmslave.cn/map.html">bdsm
</a>[url="http://www.bdsmslave.cn/map.html"]bdsm
[/url]http://www.bdsmslave.cn/map.html bdsm
 qzmtzr, <a href="http://www.anallesbian.cn/anal/map.html">anal
</a>[url="http://www.anallesbian.cn/anal/map.html"]anal
[/url]http://www.anallesbian.cn/anal/map.html anal
 usay, <a href="http://www.bdsmslave.cn/map.html">bdsm
</a>[url="http://www.bdsmslave.cn/map.html"]bdsm
[/url]http://www.bdsmslave.cn/map.html bdsm
 397757, <a href="http://www.analtoy.cn/anal/map.html">anal
</a>[url="http://www.analtoy.cn/anal/map.html"]anal
[/url]http://www.analtoy.cn/anal/map.html anal
 8-[[[, <a href="http://www.anallesbian.cn/anal/map.html">anal
</a>[url="http://www.anallesbian.cn/anal/map.html"]anal
[/url]http://www.anallesbian.cn/anal/map.html anal
 2535, <a href="http://www.gayteenboy.cn/map.html">gays
</a>[url="http://www.gayteenboy.cn/map.html"]gays
[/url]http://www.gayteenboy.cn/map.html gays
 052,
----
CategoryHomepage
<<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.

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]].

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`.

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 and FreeBSD systems, the `stat` command takes arguments which allow you to specify which information you want:
 {{{
 $ stat -c %a /
 755
 }}}
 That's obviously a lot easier to parse. (NetBSD and OpenBSD use `-f` instead of `-c`.)

 * 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.)

 * Sufficiently recent GNU `find` has a `-printf` switch that can print out metadata instead of filenames:
 {{{
 find "$filename" -maxdepth 0 -printf %m
 }}}
 (Very, very non-portable. But if you happen to need a whole bunch of metadata, recursively....)

 * 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.

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 and FreeBSD systems, the stat command takes arguments which allow you to specify which information you want:

     $ stat -c %a /
     755

    That's obviously a lot easier to parse. (NetBSD and OpenBSD use -f instead of -c.)

  • 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.)

  • Sufficiently recent GNU find has a -printf switch that can print out metadata instead of filenames:

     find "$filename" -maxdepth 0 -printf %m
    (Very, very non-portable. But if you happen to need a whole bunch of metadata, recursively....)
  • 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)