0

I'm trying to use find or grep onto the LS output.

for now, ls -l is printing tons of informations. But I only want the user associated to a filename. And the filename might be greped

Xcrowzz
  • 182
  • 1
  • 19

4 Answers4

6

Use find with the -printf flag:

find . -name "a*" -printf "%u %f\n"
find . -name "M*" -printf "%u %f\n"

From man find:

-printf format

%u File's user name, or numeric user ID if the user has no name

%f File's name with any leading directories removed (only the last element).

arco444
  • 22,002
  • 12
  • 63
  • 67
  • The `-printf` option is present on [linux](http://linuxcommand.org/man_pages/find1.html), but is missing on [Mac os](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/find.1.html). It is ok though, since OP hasn't included any `Mac os` related notes/tags. – Eugeniu Rosca Jun 22 '15 at 15:42
3

Most systems offer a stat command, which can easily produce whatever information you want about a file (or list of files). Unfortunately, the stat command is not standardized and the set of options vary considerably. For more information, read man 1 stat on your system.

On Linux, with GNU stat, you can use

stat -c%U file...

On BSD (including Mac OS X), you should be able to use

stat -f%Su file ...

(If you wanted the uid instead of the username, you would use -c%u or -f%u, respectively.)

rici
  • 234,347
  • 28
  • 237
  • 341
0

I found the answer by myself but my way seems way more tricky. I was using :

tr -s ' ' | cut -d ' ' -f3,9 | grep " $1.*"  

But yours seems good. The thing is that the

find . -name "M*" -printf "%u %f\n"

also shows .git files. The topic is solved I guess, thanks for your consideration !

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Xcrowzz
  • 182
  • 1
  • 19
  • 4
    do not post an answer. Instead, either accept an answer (by clicking on the check mark beside the answer to toggle it from hollow to green) or give some feedback in comments to the answers. You can probably omit the `.git` files with a similar approach to the one described in [How can I get `find` to ignore .svn directories?](http://stackoverflow.com/q/2314643/1983854) – fedorqui Jun 22 '15 at 15:56
-2

Consider adding a perl oneliner like this:

ll | grep user_u | perl -lane 'print "$F[2] $F[8]"'

-lane allow to slice the output using the space as separator, $F[2] and $F[8] are the slices of interest (first slice is $F[0])

Gvim-Louu
  • 13
  • 3
  • Whilst I like perl, I'd be suggesting instead - why not use `File::Find` and more specific. – Sobrique Jun 22 '15 at 15:09
  • I would like that you provide us with a oneliner using this module. My solution can be easily reuse for every formated output. – Gvim-Louu Jun 22 '15 at 15:19
  • You could always use `awk` instead of `perl`. Then you don't need to remember what the `-lane` options do. – Mr. Llama Jun 22 '15 at 15:52
  • 2
    Again, [`don't parse ls`](http://mywiki.wooledge.org/ParsingLs). Plus, suggesting use of alias (`ll`) is another no-no. AFAIK, aliases don't work in a non-interactive shell (read: shell scripts). Plus, you don't know what aliases would be defined to. I have set `alias ll='ls -alF'` Someone would have set it to `ls -l` – anishsane Jun 22 '15 at 16:12