tags:

views:

120

answers:

4

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

+2  A: 

GNU findutils 4.2.5+:

find -printf "$PWD"'/%p %M\n'
Ignacio Vazquez-Abrams
A: 

It can also be done with ls and awk:

ls -l -d $PWD/* | awk '{print $9 " " $1}' > my_files.txt
mre
this is the worst way to parse file names
ghostdog74
@ghostdog74 Didn't say it was *efficient* or *elegant*. Just wanted to show that it can be done with plain old ls. No need to vote me down.
mre
Well, ghostdog, I gave you an up vote on this one; I like to know the alternatives!
Richard T
+1  A: 

stat -c %A file

will print file permissions for file

something like:

find . -exec echo -ne '{}\t\t' ';' -exec stat -c %A {} ';'

will give you a badly formatted version of what your after. It is made much trickier because you want everything aligned in tables. You might want to look into the 'column' command. TBH I would just relax my output requirements a little bit. Formatting output in SH is a pain in the ass.

pisswillis
A: 

bash 4

shopt -s globstar
for file in /path/**
do
    stat -c "%n %A" "$file"
done
ghostdog74