Jump to content

ls -d


Guest BooYah
 Share

Recommended Posts

Guest BooYah

[booyah@localhost booyah]$ ls -d

./

[booyah@localhost booyah]$

 

I thought the -d was supposed to list all the directories?

 

I can get what I want, a list of directories in the current directory, by piping ls to grep, but shouldn't ls -d do that in a more efficient manner?

Link to comment
Share on other sites

well the man entry just says this for the -d switch

 

-d

   If an argument is a directory, list only its name (not its contents); often used with -l to get the status of a directory.

 

I'll poke around

Link to comment
Share on other sites

these aren't exactly easy on the keyboard, but you can do:

 

ls -l|grep "^d"

 

or

 

find * -maxdepth 0 -type d -exec ls -dal {};

 

i dunno why the -d option only shows the "." kinda a PITA if you ask me.

Link to comment
Share on other sites

well the man entry just says this for the -d switch

 

-d<!--QuoteEBegin--><!--QuoteEBegin-->    If [b]an argument is a directory[/b], list only its name (not its contents); often used with -l to get the status of a directory.

 

I'll poke around

 

you have to send it an arguement.

compare:

tyme@riesling tyme $ ls Downloads

22007.pdf                               atmelwlandriver

Game Stuff                              atmelwlandriver.2.1.2.2.tar.bz2

HP2_41k1.zip                            boot.img

Incomplete                              giza-2.jpg

LimeWireLinux.bin                       gizaavatar.jpg

OOo_1.1beta2_LinuxIntel_install.tar.gz  gnome-applets-2.2.2.tar

PB_10221.ROM                            icons

PB_10404.ROM                            modules.img

PG-BIOS.zip                             n9joy.tar.gz

TechRef_Guidev1.11.pdf                  office.pdf

WonderVE.pdf                            pyramid_khafre_ariel.jpg

aen_nickserv_v0.3a.pl                   root.img

aen_nickserv_v0.3a.pl.gz                xray-blue.boot

aen_nickserv_v0.3a.pl~

to

tyme@riesling tyme $ ls -d Downloads

Downloads

 

hope that shows you what actually does :)

Link to comment
Share on other sites

Best I could do is this:

 

find -type d -maxdepth 1 -mindepth 1  -printf '%fn'

 

Here's an example:

# pwd 

/home/omar

# ls

Desktop/            menu-icon.32.png

Documents/          menu-icon.48.png

evolution/          plugin131.trace

glxgears            rolling-r.gif

glxgears2           screenshot.png

glxgears3           sylpheed-crash-log-2003-06-24-21-29-17.txt

GNUstep/            testproject/

hda11/              texstar.ls

helloworld*         tmp/

helloworld.c        WebX-post.html

hs_err_pid3762.log  whichurpmf

images/             Windows XP2.jpg

latex-stuff/        Windows XP2.png

lfs/                WindowsXP.gif

Mail/               wvdial-pipe

map.gif             yinyang.png

menu-icon.16.png

# find -type d -maxdepth 1 -mindepth 1  -printf '%fn'

.qt

lfs

tmp

.kde

.sbd

Mail

.netscape

.designer

Documents

.gaim

.gftp

.java

.mcop

.xfce

.xmms

.gnome2_private

testproject

hda11

latex-stuff

.netscape6

.mozilla

.gconfd

.gnome2

.lgames

.acrobat

.gimp-1.2

.sigrot

.themes

.metacity

Desktop

.xvpics

.nautilus

GNUstep

.phoenix

images

.openoffice

.gnome-desktop

.thumbnails

.jpi_cache

.gnome_private

.adobe

.gconf

.gnome

.gnupg

.icewm

.icons

evolution

.sylpheed

.fullcircle

 

You can also do this:

find /etc -type d -maxdepth 1 -mindepth 1  -printf '%fn'

 

to list all the directories under /etc. but not recursive. If you want to make it list all the directories under /etc and all the directories under those directories, take out the -maxdepth 1

Link to comment
Share on other sites

Guest BooYah

Thanks guys. OK. So ls doesn't do what I want to do?

ls -d

Doesn't seem to do much of anything except verify the directory argument exists.

 

This what I do to get a listing of nonhidden directories in the current working directory

ls | grep /

 

tyme,

 

ezroller is right

ls -d ./*

lists all of the hidden directories.

 

ls *

gives me a recursive listing of everything starting from the working directory

Link to comment
Share on other sites

tyme,

 

ezroller is right

ls -d ./*

lists all of the hidden directories.

 

ls *

gives me a recursive listing of everything starting from the working directory

:shock:

 

i must have typed something wrong.... :oops:

 

addition: ok, so you want a list of the directories in the current working directory? (that's what i get from your original post)

 

if so, check this sh*t out:

ls -d */

:twisted: since any directory will technically have a / at the end of it's name, this does the trick 8)

Link to comment
Share on other sites

Guest BooYah

Dagnabbit! How'd I miss that one? Especially after ezroller's tip. I thought I had tried every combination under the sun before posting here.

 

Oddly it adds an extra / to each listing , but it does do what I want.

Link to comment
Share on other sites

besides the "ls -d */" which is already aliased by DEFAULT by mandrake as "lsd". Yes the same group of aliases that include "ll" and "la" (my face while reading this thread --> :shock: I can't believe you didn't know how to list directories :shock: :lol: ) There are also another cool ways:

 

One as Steve Scrimpshire suggested is "find":

level 1 directories:

~$ find . -type d -maxdepth 1

all of them:

~$ find . -type d

 

Another way could be using tree:

level 1 directories:

~$ tree -a -d -L 1

All the tree:

~$ tree -a -d

 

And finally a cool emulation of tree using find+sed instead:

~$ find . -type d -print | sed -e 's;[^/]*/;|____;g;s;____|;    |;g'

 

HTH

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

×
×
  • Create New...