Search code examples
bashshellfindlscut

BASH file attribute gymnastics: How do I easily get a file with full paths and privileges?


Dear Masters of The Command Line,

I have a directory tree for which I want to generate a file that contains on two entries per line: full path for each file and the corresponding privileges of said file.

For example, one line might contain:

/v1.6.0.24/lib/mylib.jar -r-xr-xr-x

The best way to generate the left hand column there appears to be find. However, because ls doesn't seem to have a capability to either read a list of filenames or take stdin, it looks like I have to resort to a script that does this for me. ...Cumbersome.

I was sure I've seen people somehow get find to run a command against each file found but I must be daft this morning as I can't seem to figure it out!

Anyone?

In terms of reading said file there might be spaces in filenames, so it sure would be nice if there was a way to get some of the existing command-line tools to count fields right to left. For example, we have cut. However, cut is left-hand-first and won't take a negative number to mean start the numbering on the right (as seems the most obvious syntax to me). ... Without having to write a program to do it, are there any easy ways?

Thanks in advance, and especial thanks for explaining any examples you may provide!

Thanks, RT


Solution

  • GNU findutils 4.2.5+:

    find -printf "$PWD"'/%p %M\n'