Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a rather strict set of specs for a directory listing that I need:

  1. It must list the directories in the current directory
  2. It must list purely the basename (no "./", no trailing slash)
  3. It mustn't error if there are none (and mustn't print an error to std{out,err})
  4. It must be very lightweight
  5. It must work under tcsh

I've got something working like this:

find -maxdepth 1 -mindepth 1 -type d -printf '%f\n'

But I can't help feeling that using "find" is overkill. I tried to do something with ls -d */, but I couldn't figure out how to get it not error (or appear to not error).

Any suggestions?

share|improve this question
    
How do you need the output? ls-style or one per line? –  sudo Mar 26 '13 at 14:32
    
This is for a tcsh completion, which I think means either will work (I can't use the standard directory completion, for reasons I won't go into right now). –  spookypeanut Mar 26 '13 at 16:48

1 Answer 1

up vote 1 down vote accepted

Personally, I'd say the find command is the way to go.

The ls -d approach has two problems right now:

  • It shows an error on empty directories.
  • It prints trailing slashes.

Both are fixable. tr can take care of the slashes, and you need redirection to get rid of error messages:

( ls -d */ | tr -d / > /dev/tty ) > & /dev/null

See man tcsh for further information.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.