开发者

Is there a way to get path/to/file with ls + awk, sed, grep or similar tools?

I'd like to recursively search a directory, and output:

Filename Date Path Size

I got everything but Path...which is a $$$$buster....

Here's my command so far:

ls -lThR {DIRECTORY_NAME_HERE} | awk '/^-/ {print $10 " " $6 " " $7 " " $8 " " $5}'

I wish there was a way to combine that command with:

find ./{DIRECTORY_NAME_HERE} -type f 

which just shows /path/to/filename itself...no other metadata afaik.

Any ideas...hopefully without needing a programming language?

EDIT: Here's the exact output I w开发者_运维问答as looking for assuming file is 5 bytes:

myfile.txt Dec 2 10:58 /path 5

UPDATE: Here's the command I wound up with:

find ./{DIRECTORY_NAME_HERE} -type f -ls | 
while read f1 blocks perms blocks owner group size mon day third file; 
do echo `basename $file` `ls -lrt $file | tr -s " " | cut -d" " -f6-8` `dirname $file` `ls -lrt $file | tr -s " " | cut -d" " -f-5`; done

If someone can improve it, that'd be great, but this works...


Have you tried find ./delete -type f -ls (note the -ls -- that's the key :-) )? You should then be able to pipe the results through awk to filter out the fields you want.

Edit... Another way you could do it is with a while loop, e.g.:

find ./delete -type f -ls | while read f1 blocks perms blocks owner group size mon day third file
do
    echo `basename $file` `dirname $file`
done

and add the bits you need into that.


You can also use the -printf feature of find to show just the right properties of a file that you want:

find {DIRECTORY_NAME_HERE} -type f -printf  '%f %Tb %Td %TH:%TM %h %s\n'

I get results like this:

config Nov 10 10:02 /etc/w3m 1185
mailcap Nov 10 10:02 /etc/w3m 44
hosts.allow Apr 29 05:25 /etc 580
rsyslog.conf Feb 24 10:26 /etc 1217
user-dirs.conf Apr 16 15:03 /etc/xdg 414
user-dirs.defaults Apr 16 15:03 /etc/xdg 418


I'd use Perl for this task:

#!/opt/local/bin/perl -w

use File::Find;
use POSIX qw(strftime);

find(\&wanted, ($DIRECTORY_NAME_HERE));
sub wanted {
  ($dev,$ino,$mode,$nlink,$uid,$gid,$rdev,$size,$atime,$mtime) = stat;
  printf("%s %s %s %d\n", $_, 
    strftime("%b %e %H:%M %Y", localtime($mtime)),
    $File::Find::dir,
    $size);
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜