开发者

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 stdi开发者_运维知识库n, 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


GNU findutils 4.2.5+:

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


It can also be done with ls and awk:

ls -l -d $PWD/* | awk '{print $9 " " $1}' > my_files.txt


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.


bash 4

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜