The UNIX and Linux Forums  

Go Back   The UNIX and Linux Forums > Top Forums > Shell Programming and Scripting
Google UNIX.COM


Shell Programming and Scripting Post questions about KSH, CSH, SH, BASH, PERL, PHP, SED, AWK and OTHER shell scripts here.

More UNIX and Linux Forum Topics You Might Find Helpful
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

Reply
 
Submit Tools LinkBack Thread Tools Search this Thread Display Modes
  #1  
Old 07-22-2008
Registered User
 

Join Date: Jul 2008
Posts: 7
Thumbs down pipe to ls with filenames with spaces breaks!

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 "^.$"`
Reply With Quote
Forum Sponsor
  #2  
Old 07-22-2008
Registered User
 

Join Date: Jul 2008
Posts: 7
Red face Oh! I get the ./ behavior

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
Reply With Quote
  #3  
Old 07-22-2008
 

Join Date: May 2008
Location: Sydney, Australia
Posts: 917
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'
Reply With Quote
  #4  
Old 07-22-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
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
(Alternate sed invocation just to show There's More Than One Way To Do It.)

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
Reply With Quote
  #5  
Old 07-23-2008
Registered User
 

Join Date: Jul 2008
Posts: 7
Quote:
Originally Posted by era View Post
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.)
That's it exactly. ls does more flexible formatting, giving more columns with shorter filenames. If there was a pr -auto that worked liked that it would be cool

Patrick
Reply With Quote
  #6  
Old 07-24-2008
Registered User
 

Join Date: Jul 2008
Posts: 7
Thumbs up

Quote:
Originally Posted by Annihilannic View Post
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'
Just the thing! Thank you! This lets me do it more simply, and lets me collect args for the ls so that for example, sometimes I could do lsd, sometimes lsd -a. I've decided to build an arg string bit by bit so that later if some args will be better suited to the script, and some to the ls, then I can dispatch appropriate args appropriatelly Here's the most recent incarnation of lsd:

#! /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')
Reply With Quote
  #7  
Old 07-24-2008
era era is offline
Herder of Useless Cats
 

Join Date: Mar 2008
Location: /there/is/only/bin/sh
Posts: 3,650
As a minor quib, you should probably prefer "$@" over $*
Reply With Quote
Google The UNIX and Linux Forums
Reply

Tags
bash, find, ls with spaces, space in filename

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes




All times are GMT -7. The time now is 03:49 PM.


Powered by: vBulletin, Copyright ©2000 - 2006, Jelsoft Enterprises Limited.
The UNIX and Linux Forums Content Copyright ©1993-2008. All Rights Reserved.Ad Management by RedTyger Visit The Complex Event Processing Blog

Content Relevant URLs by vBSEO 3.2.0