2

I'm trying to write a regular expression to match Unix file permissions returned by ls -l command. Here I found that all possible letters for the first field are d,c,l,p,s,b,D. Now, what are all possible letters for the remaining fields? This is how my regex looks so far:

/[-dclpsbD][-rwx]{9}/
forsajt
  • 121
  • 2
    Out of curiosity: what exactly are you trying to achieve? – peterph Feb 17 '15 at 18:12
  • Yes, why in the world would you want to use a regex for this and not field splitting? – terdon Feb 17 '15 at 18:14
  • I want to check if given text is the output of ls -l command, so I'd like to test it with explicit regex match for file permissions. – forsajt Feb 17 '15 at 18:17
  • At least [-rwxsStTlL], and optionally a + after all of them. – Mark Plotnick Feb 17 '15 at 18:20
  • Hi Mark, do you mean something like /[-dclpsbD][-rwxsStTlL]{9}\+?/ ? Thanks. – forsajt Feb 17 '15 at 18:25
  • That may do it. To be complete, read the man page for ls on all the operating systems you're interested in. I looked at Linux, FreeBSD, and Solaris. – Mark Plotnick Feb 17 '15 at 18:39
  • 1
    You can tighten this up ^[-dclpsbDCMnP?]([-r][-w][-xsS]){2}([-r][-w][-xtT])[+]?, but what are L and l? – ctrl-alt-delor Feb 17 '15 at 18:46
  • 1
    Please [edit] your question and explain what your actual objective is. Parsing ls is really not the best way to get permissions. You are almost certainly barking up the wrong tree here. – terdon Feb 17 '15 at 19:02
  • @richard L and l denote mandatory locking in Solaris. – Mark Plotnick Feb 17 '15 at 19:14
  • @MarkPlotnick - those will probably work, but other fs/ls implementations could add more. POSIX spec's [-dbclp] for the first field and [-sStTx] for 2-4, but it allows for Implementations may add other characters to this list for the third character position. Such additions shall, however, be written in lowercase if the file is executable or searchable, and in uppercase if it is not... forsajit: It will likely be easier to use ls -n than -l. – mikeserv Feb 17 '15 at 19:35
  • @terdon - the question seems complete enough to me. The question is straightforward and simply purposed. The asker can use the information gleaned in any way he/she sees fit - your own presumptions about his/her purpose are not, as I think, very relevant. – mikeserv Feb 17 '15 at 19:40
  • @forsajt - also, to handle multiple lines in filenames, don't use -R and use \ls -opts ./* for the command. You can then filter on / - checking only the head of those lines which contain a slash. That way you're in no danger of false positives - except in the case of symbolic links. If is acceptable though, -H would rule even that out. – mikeserv Feb 17 '15 at 19:48
  • 2
    I'm still wondering what is the real motive - "I want to check if given text is the output of ls -l command" still sounds like half way to the real reason. Which leads to feeling, that the right solution to the real problem solved here is something completely else. – peterph Feb 17 '15 at 21:39
  • This drwxr--r-x 1 richard richard 4096 Jan 25 22:09 Trash/ did not come from ls -lF, or did it. – ctrl-alt-delor Feb 17 '15 at 23:09
  • @MarkPlotnick: Don't forget that the eleventh character can also be a . (period) or @, so I guess the regex should end [+.@]?. – Scott - Слава Україні Jun 25 '15 at 19:39

1 Answers1

2

Instead ls use find -type:

File is of type:
b      block (buffered) special
c      character (unbuffered) special
d      directory
p      named pipe (FIFO)
f      regular file
l      symbolic  link
s      socket
D      door (Solaris)

and find -perm:

-perm mode
    File's permission bits are exactly mode (octal or symbolic). Since an exact
match is  required, if  you  want to use this form for symbolic modes, you may 
have to specify a rather complex mode string. For example -perm g=w will only 
match files which have mode 0020 (that is, ones for which group write permission
is the only permission set). It is more likely that you will want to use the `/'
or `-' forms, for example -perm -g=w, which matches any  file  with  group  write
permission.

-perm -mode
    All of the permission bits mode are set for the file. Symbolic modes are
accepted in this form, and this is usually the way in which would want to use 
them.  You must specify `u', `g'  or  `o' if you use a symbolic mode.

-perm /mode
    Any of the permission bits mode are set for the file.  Symbolic modes are 
accepted in this form. You must specify `u', `g' or `o' if you use a symbolic mode.
If no permission bits in mode are set, this test matches any file (the idea here
is to be consistent with the behaviour of -perm -000).

-perm +mode
    Deprecated, old way of searching for files with any of the permission bits in
mode set. You should use -perm /mode instead. Trying to use the `+' syntax with
symbolic modes will yield surprising results. For example, `+u+x' is a valid
symbolic mode (equivalent to +u,+x, i.e.  0111) and will therefore not be 
evaluated as -perm +mode but instead as the exact mode specifier -perm mode and so
it matches files with exact permissions 0111 instead of files with any execute bit
set. If you found this paragraph confusing, you're not alone - just use -perm 
/mode. This form of the -perm test is deprecated because the POSIX specification
requires the interpretation of a leading `+' as being part of a symbolic mode, and
so we switched to using `/' instead.
Costas
  • 14,916