List only the directories October 19, 2005
Posted by Carthik in commands, snippets, ubuntu.trackback
I had a trivial problem today where I had a huge list of files in a directory, and other directories within the directory. I was looking for a specific directory and wanted to get the files out of the way. I wanted a listing of the directories within the current directory and nothing more. Luckily, with a little experimentation I was able to figure out how to do this :
$ ls -l | grep “^d”
Neat.







Another one I find myself using a lot is: ls -ltr
That’s for directories such as /var/log that have lots of files in them and you want to know which ones have been updated most recently. It lists files, in detail, in reverse order of modification time. So once all the files have streamed through your screen, the ones at the bottom are the ones that have just been updated.
find . -type d -maxdepth 1 -mindepth 1
Not to mention the wonderful program tree.
tree -d -L 1
I have written small scripts to handle this since it frequently comes up for me too. Then last month somebody showed me this:
ls -d */
I second the vote for:
ls -d */
I just learned of it myself, also about a month ago. I think it was buried in a Slashdot comment.
does anyone know _how_ `ls -d */` works? after looking at the output of `ls -d` and `ls */` i’m totally confused.
tyler, the -d says “do not descend into sub-directories, it is the same as saying -d1.
The */ says list only items ending in / (which are directories).
If you want to see hidden directories, you could do a ls -d .*/
hmm…. While this seems to work – it is NOT as documented in Ubuntu info and man pages.
Both of those reference document the -d option as:
-d, –directory
list directory entries instead of contents, and do not
dereference symbolic links
Based on that definition ‘ls -d’ should have produced the desired results – but as we know it does not.
One should note that the wildcards are expanded by bash (or the shell you are using) not by ls.
ls -d will not list directories but the entries, by default no argument leads to the current directory entry that is “.”.
All other stuff like “ls -d */” is actually shell based and expands to something like “ls -d bla/ blub/ …./” and then just prints out those single entries.
Clearly “ls */” will then list the contents of all patterns matching it and thus the files in the directories matching this pattern (read: not the hidden dirs).
As proposed some time ago the “find” command will probably lead to the “best” results as its independent of bash globbing….
That’s fine, but i need a neat list of directory names, without prefixes or trailing slashes.
How to filter this out?
Try:
# for i in $(ls -d */); do echo ${i%%/}; done
[...] There are some other ways discussed here at the Ubuntu log. [...]
tree -fid is also nice. Useful in scripts.
Realizzare PCB è facilissimo con PCBFacile.
#10, i used this to have just directory names:
find . -type d -printf ‘%P\b’
find lets you format your output however you want.
This is one is good,
ls -d */ | xargs -l basename
i always do this ls -d */
then alias it using
lsd = `ls -d */`
so you just have to type “lsd”
well since the */ is interpreted by the shell anyhow why not use
echo */
to list directories and if you dont like the trailing “/”
echo */ ” ” | sed “s/\/ /\n/g”
?
this is neat vsutra, nice, how about folders whose names consists of a space and we would need to move them?
thanks in advance and regards
why the h311 isn’t there an option right in ls to do this? why do I have to pi55 around with for loops and crap like that to get a basic piece of information. List me the damn directories ONLY.
too much to ask?
To lejeczek:
Well since the default field separator includes space, we’ll need to change the code slightly to this:
IFS=$’\x0A’;for i in $(ls -1d */); do echo ${i%%/}; done
It should display out directories with spaces in it properly.
I’m not entirely sure whether I understand the second part of your question, but maybe this will help in manipulating the output:
IFS=$’\x0A’;for i in $(ls -1d */); do echo “\”${i%%/}\”"; done
To weenie:
ls -d */
thats all you need to do just to list down the directories, the rest of it is just to format the output.
find . -maxdepth 1 -mindepth 1 -type d -print0 | xargs -0 | sed ’s/.\//\n/g’
This will get the directories only even if the has spaces or other characters in the name and then strip out the leading ./ and replace it with a newline. drop the last | and stuff after it and add your favoritecommand to process the directories with:
find . -maxdepth 1 -mindepth 1 -type d -print0 | xargs -0 du -sh
show how much disk space each directory takes up.
this is fun
echo */*/*/
i need help.
i’m trying to backup multiple directories using tar. however i want those directories to keep their corresponding names.
e.g.
test1 = test1.tar
test2 = test2.tar
test3 = test3.tar and so on.
i tried to using the find command, to print the names, but after that i don’t know how to pipe it properly to tar.
please help
Hello,
I am using ls -d */ to get directories, but the list coming out contains not only directories but also files, behind which the commando simply puts /. The strange thing is, on my home directory it works fine, it only happens when I use it on another server (which need to be done cause my data is there).
Somebody has a suggestion?
Solved it, indirectly: the directories had short names, files longer. so i used the command ls -d /*/???/ /*/????/ /*/?????/
very strange tho
[...] What follows are a couple of ways of doing what I was trying to do, which I found in a post (and its comments) on the Ubuntu Blog, “List only the directories“: [...]
ls -d */ | xargs -l basename
This wokrs good.
BUT, if a directory has a space in the name , then it is not displaying full name.
kunle – April 9, 2007
this is fun
echo */*/*/
this is alot more fun:
echo /*/*/*/
hey there!
what about ths one:
ls -R | grep ./
GREAT list! Even with the numerous variations listed here, I was unable to find one to match my exact needs – I am sure there is probably an easier way to do this, as I am a bit of a noob, but I needed a recursive list of directories within a directory, with the spaces escaped. I ended up writing this:
find . -type d | sed ’s/ /\\ /g’
The odd thing is, when I run it within my script to assign the output to a variable, the spaces are no longer escaped, and the X’s are missing? Oh well, back to do further research…
ls -l | grep ^d | awk ‘{print $9}’ | grep -v ^\\.
[...] List only the directories « Ubuntu Blog listing of the directories within the current directory (tags: linux howto bash directories codesnips) Share and Enjoy: These icons link to social bookmarking sites where readers can share and discover new web pages. [...]
Print directory names one line at a time, without any other characters:
find . -maxdepth 1 -mindepth 1 -type d -printf %P\\n
Look ma, no pipes
Print directory names one line at a time, without any other characters:
find . -maxdepth 1 -mindepth 1 -type d -printf %P\\n
Look ma, no pipes
Nice for recursive directories permissions switching (in my case 700 -> 755):
find . -type d -mindepth 1 2>/dev/null | sed ’s/^/”/g; s/$/”/g’ | xargs chmod 755
Thanks for ls -d */
works fine !!
nice tips indeed, it works fine for me
I was using find . -type d but ls -d */ looks better
find . -maxdepth 1 -mindepth 1 -type d | grep -v ‘^\./\.’
aliased as ldir
works for me!
sample price auto insurance new driver nyc…
Aviv preposterous anchovy …
florida insurance flood zones…
chuckles masted impostors,nullifies badness!contraband:…
I’ve really been enjoying reading all that stuff of incredible ingenuity
Thanx folks for sharing
If you wish to display files only, try this:
ls -p | grep -v ‘/$’
Oooooookay, came across this thread when trying to chmod a directory (with files) I copied from a Windoze machine (dirs=755, files=644).
If anybody wants to do just that, try this:
find . -type d -print0 | xargs -0i chmod 755 {}
find . -type f -print0 | xargs -0i chmod 644 {}
Using -print0 and -0i (be careful, these are zeros), every file’s oder folder’s name may even contain special characters like quotes. Nice.
ls -l | grep “^d” | sed ’s/[ ][ ]*/ /g’ | cut -d ” ” -f 8
dont know if its valid but…
type cd and then press TAB key twice
And the opposite one
http://unstableme.blogspot.com/2008/12/list-only-file-and-not-directory-ls.html
ls -d */ is just what I need but ideally ls should have options like:
–dirs-only
–files-only
(maybe more for blocks and pipes?)
and an alias like lsdir (cf. lsusb, lspci)
lslatest for ls -latr (which I use a helluvalot)
There’s always bash aliases for those I guess.
Tada! Lists only top directories,in dictionary order,then pipes it into a text file:
find . -maxdepth 1 -mindepth 1 -type d -printf %P\\n | sort -d > filelist.txt