[clug] list only directories

steve jenkin sjenkin at canb.auug.org.au
Tue Nov 19 23:54:10 MST 2013

Hal Ashburner wrote on 20/11/13 5:22 PM:

> Do you good and talented people all do something similar? Are you doing
> better? Have I missed a trick?


you've missed nothing.
'ls' doesn't let you get to a bunch of useful metadata in the inode, as
well being deficient in options and annoying 'user readable' variable
formatting (Grrrr).
This gave PERL a kick along as the SysAdmin's Swiss Army Chainsaw. It
allows scripts to retrieve and work with actual metadata, fully accessible.

==> this should be a robust solution to what you want to do.
[Andrew J, please correct/improve!]

find . -maxdepth 1 -type d -print0|xargs -0 ls -ld

- look in current dir
- no need for 'nofollow' (symlinks) or -xdev
- maxdepth is current dir
- type d, vs type f, l, ...
- print args with nul ('\0x0') chars, not newlines
- use xargs to do its thing reliably.

You'll all laugh, I only recently realised that:

find . -type f | tr '\n' '\0'

was equivalent to
find . -type f -print0

Useful when you capture a list of files & do some normal
editing/selection on them, then you can still use xargs.

[I've another problem with using xargs and 'mv'. You have to work hard
to put the new destination at the end of the line, after the args that
xargs inserts. Or I should reread the man pages :)]

In early V6/7 & BSD systems, filenames didn't have shell "word breaks"
or special characters.
find didn't need -print0, filenames didn't confuse the shell.

I can't recall the MAXPATH (?) and max dirent size, but they were small.
14 chrs in V6 [and 16-bit inodes in ufs] springs to mind.

Steve Jenkin, Info Tech, Systems and Design Specialist.
0412 786 915 (+61 412 786 915)
PO Box 48, Kippax ACT 2615, AUSTRALIA

sjenkin at canb.auug.org.au http://members.tip.net.au/~sjenkin

More information about the linux mailing list