![]() |
|
|
|
|
|||||||
| Forums | Portal | Register | Forum Rules | FAQ | Contribute | Members List | Arcade | Search | Today's Posts | Mark Forums Read |
| Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here. |
|
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| spaces in filenames | Bab00shka | Shell Programming and Scripting | 8 | 09-30-2008 11:13 AM |
| spaces in filenames, for do | naviztirf | Shell Programming and Scripting | 4 | 10-17-2007 02:08 PM |
| Unix filenames and spaces | x96riley3 | Shell Programming and Scripting | 2 | 01-31-2007 04:07 PM |
| how to handle spaces in filenames | rayne | Shell Programming and Scripting | 4 | 11-19-2006 10:37 AM |
| spaces in filenames | Hitori | Shell Programming and Scripting | 4 | 07-04-2006 01:35 PM |
|
|
Submit Tools | LinkBack | Thread Tools | Search this Thread | Display Modes |
|
#1
|
|||
|
|||
|
I wanted an alias or program, lsd, that would show just the directories in a directory. My first take was this alias.
alias lsd='ls -d `find . -maxdepth 1 -type d -print | grep -v "^.$" | cut -c 3- `' It worked fine until I got to directory names with spaces, so I moved it into a script so I could look at it, and as a first take, thought I'd use sed to replace any spaces with "\ " like you would from a command line and that was a disaster. I finally got it to work, and I'll include the short script below, but does someone know of a better, shorter way of doing it? (The section on args is there so later I can override the depth if I want to. One strange thing is that the grep is to get rid of the ./ dir which is always there, and I don't really need to see it. Great! But if there's no directories in a directory, it lists the ./ anyway! I kind of like it, but I'd like to understand how or why! #! /bin/bash export IFS=" " mygetdir='.' for arg in $* ; do case $arg in *) mygetdir=$arg ;; esac done cd >& /dev/null $mygetdir echo ~~~ $mygetdir ~~~ ls -F -d `find -L . -maxdepth 1 -type d -print | sed 's/.\///' | grep -v "^.$"` |
| Forum Sponsor | ||
|
|
|
#2
|
|||
|
|||
|
If the only directory is ./ then the pipeline has no output, and the default directory for ls with no arguments is ./ -- so -- accidentally I got behavior I want. I'd still like to have a shorter more elegant version though.
Thanks, Patrick |
|
#3
|
|||
|
|||
|
Why not use the fact that ls -F suffixes directories with a "/" to filter them out, something like this perhaps:
Code:
ls -F "$@" | sed -n 's/\/$//p' |
|
#4
|
|||
|
|||
|
You can explicitly exclude the current (dot) directory with something like
Code:
find . -maxdepth 1 -name . -o -type d -print | sed -e 's%^./%%' | tr '\012' '\000' | xargs -r0 ls -d The use of tr to replace newlines with ASCII NUL is so I can use xargs -0 in order to cope with spaces in file names. It still won't work right if you have directory names with newlines in them, but I guess that's an acceptable restriction in practice. Why do you want to pass this to ls -d, though? It will wrap the output into columns which is kind of neat, but using ls just for this side effect is kind of obscure. (Maybe something like pr -bt4 would be more transparent, although ls is more flexible, as it does some dynamic formatting of the column widths depending on the lengths of the entries.) Last edited by era; 07-22-2008 at 10:40 PM. Reason: Question about ls -d |
|
#5
|
|||
|
|||
|
Quote:
Patrick |
|
#6
|
|||
|
|||
|
Quote:
#! /bin/bash export IFS=" " mylsargs="-F" mygetdir='.' for arg in $* ; do case $arg in -a) mylsargs="$mylsargs -a" ;; *) mygetdir=$arg ;; esac done cd $mygetdir if [ "$mygetdir" != "." ]; then echo ~~~ $mygetdir ~~~ fi ls -F -d $(eval ls $mylsargs . | sed -n 's/\/$//p') |
|
#7
|
|||
|
|||
|
As a minor quib, you should probably prefer "$@" over $*
|
|||
| Google The UNIX and Linux Forums |
| Tags |
| bash, find, ls with spaces, space in filename |
| Thread Tools | Search this Thread |
| Display Modes | |
|
|