0

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 was 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...

joedevon
  • 2,649
  • 4
  • 28
  • 43
  • Please see the [reasons not to parse `ls`](http://mywiki.wooledge.org/ParsingLs). Your updated command has all those variables available to it via the `read` and you're *wasting* them by doing a bunch of redundant `ls`, `cut`, `tr`, etc. Look at Chris' comment below on making use of those variables. – Dennis Williamson Dec 04 '10 at 03:10
  • Yeah, I knew it was hacky & hated it. Was looking for better and actually responded to Chris that I was improperly escaping his solution. Your link is very interesting though and although what I came up with (not shown above yet) works, I have another solution via Anton & will try out yours too. Appreciate your blog post a lot! – joedevon Dec 04 '10 at 18:10

3 Answers3

3

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.

Chris J
  • 30,688
  • 6
  • 69
  • 111
  • No! Didn't know you could do that! Off to test...then I'll mark yours correct if it works. Thanks! – joedevon Dec 04 '10 at 00:19
  • Very close! Not sure if that'll do, I'm finding out....is there a way to get the file and the path separate? I'll edit the question to show the output I was looking for. – joedevon Dec 04 '10 at 00:25
  • BTW, feel very embarrassed because I looked at the find man file online and it didn't show -ls. On command line of course it does. – joedevon Dec 04 '10 at 00:31
  • See edited answer -- it's another way to split the output into fields. – Chris J Dec 04 '10 at 00:33
  • Looks good...As soon as I figure out how to get size and the full date in there :) (googling as we speak) – joedevon Dec 04 '10 at 00:41
  • @joedevon - you've already got that there: note with the `while read`, that's split the line into fields - so size and date are already split out into `$size`, `$mon`, `$day` and `$third`. Shouldn't be any need to google. – Chris J Dec 04 '10 at 01:20
  • That's what I thought initially but I had trouble getting access to it... I just tried again and it turns out I was escaping it improperly... Sweet! – joedevon Dec 04 '10 at 01:54
2

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
Anton I. Sipos
  • 3,493
  • 3
  • 27
  • 26
  • Hi Anton, I get an error on printf and it doesn't show in "man find". I'm on a Mac. – joedevon Dec 04 '10 at 01:52
  • My example was using GNU find on Ubuntu. There's another stackoverflow question about the OS X version of find: http://stackoverflow.com/questions/752818/why-does-macs-find-not-have-the-option-printf . In short, a friend recommends Homebrew on OS X, and a simple 'brew install findutils' should get you GNU find. 'brew install coreutils' is also useful. – Anton I. Sipos Dec 04 '10 at 04:04
1

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);
}
Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
  • Thanks Bill. Turns out I don't even have the find command let alone perl... I'll have to do a recursive ls and then use perl or something else locally to parse the file :( – joedevon Dec 21 '10 at 00:52