Click to See Complete Forum and Search --> : A novice command question?
gopikrish
08-12-2004, 06:45 PM
How to list only the directories thats inside the current directory?
Suppose current directory is /usr and there are many files as well as /usr/A
/usr/B and usr/C directories. Now how to list only these A,B,C directories?
JohnT
08-12-2004, 08:13 PM
cd to the directory in question, and issue the commanddir
Alex Cavnar, aka alc6379
08-13-2004, 12:49 AM
or:
ls /usr/{A,B,C} would just list those directories listed in the curly brackets, if I recall correctly.
michaeln
08-13-2004, 01:06 AM
try
# ls -d */
gopikrish
08-13-2004, 01:48 AM
dir shows all files and dirs.
Yes thanks michael. it worked.
JohnT
08-13-2004, 03:03 AM
Originally posted by gopikrish
dir shows all files and dirs.
Yes thanks michael. it worked. Thats odd...on my box it only shows directories.
Ludootje
08-13-2004, 06:22 AM
'dir' isn't a command afaik, it's an alias to 'ls' which some distros set up. You could do "env | grep dir" to see what parameters you 'dir' command adds to ls. (At least, that's what it used to be - maybe there really is a dir command now on some distros)
psi42
08-13-2004, 09:57 AM
Originally posted by Ludootje
'dir' isn't a command afaik, it's an alias to 'ls' which some distros set up. You could do "env | grep dir" to see what parameters you 'dir' command adds to ls. (At least, that's what it used to be - maybe there really is a dir command now on some distros)
Actually, on slackware, it is:
maedhros@himring:~$ ls -al /usr/bin/dir
-rwxr-xr-x 1 root bin 72608 2004-03-15 18:08 /usr/bin/dir*
maedhros@himring:~$ ls -al /bin/ls
-rwxr-xr-x 1 root bin 72608 2004-03-15 18:08 /bin/ls*
maedhros@himring:~$ md5sum /usr/bin/dir
0892eaac68762dfc3dc4fa6b06e1a204 /usr/bin/dir
maedhros@himring:~$ md5sum /bin/ls
e86ea2c0825b1ca0b7ef20e7a83c3271 /bin/ls
...The md5sums do not match. What's the difference?
~psi42
Ludootje
08-13-2004, 10:30 AM
I had a look, apparently I have it too:
<ludo@gax ~> $ ls -lF /bin/dir /bin/ls
-rwxr-xr-x 1 root root 78648 Jul 9 09:21 /bin/dir*
-rwxr-xr-x 1 root root 78648 Jul 9 09:21 /bin/ls*
<ludo@gax ~> $ md5sum /bin/dir /bin/ls
feef14ecdca77d0d1bdf6434703502e1 /bin/dir
e2a228b5296e4fec3b7ecaf474b79003 /bin/ls
<ludo@gax ~> $ file /bin/dir /bin/ls
/bin/dir: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.4.1, dynamically linked (uses shared libs), stripped
/bin/ls: ELF 32-bit LSB executable, Intel 80386, version 1 (SYSV), for GNU/Linux 2.4.1, dynamically linked (uses shared libs), stripped
<ludo@gax ~> $ dir --version
dir (coreutils) 5.2.1
Written by Richard Stallman and David MacKenzie.
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
<ludo@gax ~> $ ls --version
ls (coreutils) 5.2.1
Written by Richard Stallman and David MacKenzie.
Copyright (C) 2004 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
I suppose it got added in the "new" coreutils package, and didn't exist when it was still called fileutils + shellutils + textutils... although coreutils is supposed to be a simple merge of those 3 packages. So it probably was there all along, this is just the first time I notice it.
BTW when I do "man dir", I get the man page for ls:
LS(1) LS(1)
NAME
ls, dir, vdir - list directory contents
SYNOPSIS
ls [options] [file...]
dir [file...]
vdir [file...]
The difference between both is very small I think.
Hmm just did some tests, the difference is at line 235. (both contain 237 lines)
All other lines are equal:
Check the number of lines:
<ludo@gax ~> $ wc -l /bin/dir /bin/ls
237 /bin/dir
237 /bin/ls
Check for a difference between the two last lines:
<ludo@gax ~> $ tail -n 2 /bin/dir > /tmp/dir
<ludo@gax ~> $ tail -n 2 /bin/ls > /tmp/ls
<ludo@gax ~> $ md5sum /tmp/ls /tmp/dir
17a679dbd54a6c0cef58a880456c9667 /tmp/ls
17a679dbd54a6c0cef58a880456c9667 /tmp/dir
Check for a difference between first 234 lines:
<ludo@gax ~> $ head -n 234 /bin/dir > /tmp/dir
<ludo@gax ~> $ head -n 234 /bin/ls > /tmp/ls
<ludo@gax ~> $ md5sum /tmp/ls /tmp/dir
8ca42e794cbbaa97f6ef7b712c9f6907 /tmp/ls
8ca42e794cbbaa97f6ef7b712c9f6907 /tmp/dir
I first did an octal dump (using 'od'), and a hexdump afterwards - I found the hexdump more clear. Here's the difference:
00120e0 0001 0000 ffff ffff 0001 0000 15c0 0805
00120e0 0001 0000 ffff ffff 0002 0000 15c0 0805
That's what accounts for the difference in the md5sum. I wish I knew what this difference *really* represents (in code, that is). I think it's nothing though, this stuff is probably from the compilation, as line 236 (remember, the difference is on line 235) contains version info about your system and the compiler which got used, etc.
If you care to see the difference as shown by 'cat', instead of in hex:
ì T
pì T
The first is /bin/dir, the second is /bin/ls
(Yes, I kinda got intrigued by it, and I'm a *tiny* bit bored, too:))
Bryon Speede
08-13-2004, 11:46 AM
I've always used:
ls -al|grep "^d"
you could add:
|awk '{ print $9 }'
if you just want directory names without the extra "long" information.
Never occurred to me to do the -d */ thing.
Seems the biggest difference is that the way I've always done it doesn't display links.
Ludootje
08-13-2004, 11:49 AM
hmm your solution (with awk) is pretty nice, I usually do an ls -F, then grep for /$, but the awk-way is pretty cool... gotta remember that one. thanks for the tip Bryon!
Ludootje
08-13-2004, 11:52 AM
anyone an idea for removing the linefeed (LF) at the end, and optionally replacing it by a space?
I tried to remove the \n using sed (sed -e 's/\n//') but it doesn't work apparently... "tr -d \r" doesn't do it either. suggestions are welcome :)
Shodan
12-14-2004, 05:00 PM
Ok
Now I have a question.
What if you just want to list the files in the directory and not any sub dirs.
Lets say I have files
foo1.txt
foo2.mpg
foo3.<whatever>
and directories
foo1/
foo2/
foo3/
Is there a way to list all of the files and not the dirs?
Thanks
JD
Bryon Speede
12-14-2004, 05:13 PM
ls -la|grep -v "^d"|awk '{ print $9 }'
Shodan
12-14-2004, 05:21 PM
Bryon,
Thanks for the speedie answer
however my system bawked at the ^
actually it said:
grep: invalid option --^
what was all that supposed to do anyway - in english that is ;)
I really appreciate the help
JD
Bryon Speede
12-15-2004, 11:03 AM
ls -la: long listing of all files including "hidden" files
grep -v "^d": match all except lines that begin with d (caret is regular expression speak for beginning of line)
awk '{ print $9 }': run awk script that prints the 9th space-delimited field (the filename).
Did you enclose the ^d in quotes? The quotes are important. Did you try with single quotes? Can you show exactly what you typed to get this error or did you cut and paste what I wrote above?