I was writing a quick little script today... well, at least it started out as a quick little script, but it needed to be able to read the directory names from an arbitrary place in my file structure. I thought, sure, the
After spending too long experimenting with various things I checked the net to see if anybody else knew the answer to what I was starting to think I was too stupid to see. It seems many other people have come up against this deficiency in the
but that has three failings. It prints the entire path, lists the directories with a trailing slash (the -d option), and doesn't see hidden dirs (those that begin with a dot). It is easy to use
and a little more fussing with
Another common solution is to use the
This works but puts all the output on one line, precedes each file with its path, the first line is always the name of the directory being listed, and the file list is all jumbled up. I can use the -printf option of file to fix the path problem and to put each name on its own line, getting rid of the first line is just a matter of piping through the tail command, and pipe through the sort command to organise the result.
That took a lot more hassling than I expected. Weird. You'd think that the ls command would have something that basic. The Amiga does:
Simple, easy to use and understand.
GNU
Additional:
I don't seem to be able to leave it alone. Here is a weird solution that I never would have thought of: use the bash shell's wildcard expansion to do it.
This has a couple of shortcomings, but as always,
We
I'm astounded that linux doesn't have an easy solution to this. Even crappy old MSDOS can do it.
Not very readable, but at least it does it.
Additional:
I have a commandline program called
Nice.
Found where I got it. http://mama.indstate.edu/users/ice/tree/
It was written by Steve Baker (ice@mama.indstate.edu) with HTML output capability Francesc Rocher (rocher@econ.udg.es) and character sets by Kyosuke Tokoro (NBG01720@nifty.ne.jp)
ls command is bound to have an option to restrict output to only directories, right? Wrong. This turned into quite a chase.After spending too long experimenting with various things I checked the net to see if anybody else knew the answer to what I was starting to think I was too stupid to see. It seems many other people have come up against this deficiency in the
ls command. There have been a lot of suggestions to get around it. The most common is: ls -d */but that has three failings. It prints the entire path, lists the directories with a trailing slash (the -d option), and doesn't see hidden dirs (those that begin with a dot). It is easy to use
sed to get rid of the trailing slash: ls -d1 */ | sed 's/\(.*\)./\1/g'and a little more fussing with
sed can get rid of the path, but I can't find any way to list the hidden dirs along with the normal ones.Another common solution is to use the
find command: find . -maxdepth 1 -type dThis works but puts all the output on one line, precedes each file with its path, the first line is always the name of the directory being listed, and the file list is all jumbled up. I can use the -printf option of file to fix the path problem and to put each name on its own line, getting rid of the first line is just a matter of piping through the tail command, and pipe through the sort command to organise the result.
find . -maxdepth 1 -type d -printf '%f\n' | tail -n+2 | sortThat took a lot more hassling than I expected. Weird. You'd think that the ls command would have something that basic. The Amiga does:
dir . dirsSimple, easy to use and understand.
GNU
dir command has a -d option supposedly to display only directories, but it doesn't seem to work properly.Additional:
I don't seem to be able to leave it alone. Here is a weird solution that I never would have thought of: use the bash shell's wildcard expansion to do it.
echo */This has a couple of shortcomings, but as always,
sed can help: cd "$address" ; echo */ | sed 's/\/ */\n/g'We
cd to the directory that we want to list from. The echo gets expanded to everything there that ends in a slash (all the directories), then sed looks for a slash followed by none or multiple spaces (can't just be a space because the very last slash doesn't have a space) and replaces them with a newline.I'm astounded that linux doesn't have an easy solution to this. Even crappy old MSDOS can do it.
DIR /A:D /B /O:NNot very readable, but at least it does it.
Additional:
I have a commandline program called
tree though I can't remember where I got it. I don't think it is standard on linux distros, but it has a lot of nice options, including the ability to print out a list of directories. It has one tiny failing, in that it begins the list with the name of the containing directory, but as before we can simply snip that off with tail: tree -ad -L 1 -i "$address" | tail -n+2Nice.
Found where I got it. http://mama.indstate.edu/users/ice/tree/
It was written by Steve Baker (ice@mama.indstate.edu) with HTML output capability Francesc Rocher (rocher@econ.udg.es) and character sets by Kyosuke Tokoro (NBG01720@nifty.ne.jp)
no subject
Date: 2012-05-27 03:13 am (UTC)As for whether happiness is a final value or a means to survival, that depends on how one chooses. Some will decide to make happiness their final goal; others will decide to seek it as a means to an end. I'm in a third camp: One should not pursue happiness as such. Rather, one should pursue certain other values and one's happiness will be one signal of one's success at that pursuit.
The main hope for those of us on the cusp is that the growth of knowledge is at least exponential. Then again, what with government meddling in the biological sciences, there's a damned good chance that important research will be stifled by religiously inspired "ethics". i have little hope that people will abandon that idiocy.
Ah, Ben Bova. He's OK but he writes too mainstream-like for me.