pipe to ls with filenames with spaces breaks!


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting pipe to ls with filenames with spaces breaks!
# 1  
Old 07-22-2008
PHP 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 "^.$"`
# 2  
Old 07-22-2008
Power 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
# 3  
Old 07-23-2008
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  
Old 07-23-2008
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-23-2008 at 02:40 AM.. Reason: Question about ls -d
# 5  
Old 07-23-2008
Quote:
Originally Posted by era
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 coolSmilie

Patrick
# 6  
Old 07-24-2008
MySQL

Quote:
Originally Posted by Annihilannic
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 appropriatellySmilie 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')
# 7  
Old 07-25-2008
As a minor quib, you should probably prefer "$@" over $*
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Handling filenames with spaces

I'm trying to handle some files with spaces in their name using "" or \ . Like "file 1" or file\ 1. My current confusion can be expressed by the following shell script: #!/bin/bash touch "file 1" "file 2" echo -n "ls: " ; ls echo --- for file in "file 1" "file 2" ; do echo $file... (9 Replies)
Discussion started by: Ralph
9 Replies

2. UNIX for Beginners Questions & Answers

Remove line breaks and extra spaces

Hi, I want to remove all extra spaces, line breaks . Need a new line entry only for term starting"array" For eg: my input is array(), array(), array(), and my expected output is array(), array(), array(), Is it possible using awk? (5 Replies)
Discussion started by: rsi.245
5 Replies

3. Shell Programming and Scripting

Delete line breaks and extra spaces between patterns

Hi, if in between strings "<section" and "</section>" across multiple lines there occurs the string "ole-present", delete all line breaks and replace any tabs or multiple spaces with a single space. Looking for an AWK or SED solution. Thank you. <section ... status = "ole-present" ...... (2 Replies)
Discussion started by: pioavi
2 Replies

4. Shell Programming and Scripting

Remove spaces in filenames

Hi, I have files like below, In files coming as spaces. Before transfering those files into ftp server. I want to remove the spaces and then can transfer the files into unix server. e.g: filenames are 1) SHmail _profile001_20120908.txt 2) SHmail_profile001 _20120908.txt 3) sh... (3 Replies)
Discussion started by: kirankumar
3 Replies

5. Shell Programming and Scripting

awk and spaces in filenames

Hey there, this is my first post and I'll try to explain my situation as best I can.Here is a sample of the input file: ADO Sample.h,v ADO Sample 2010-05-21 lyonsb /repository/patents/TSCommon/OpenSource/Dundass/ug6mfc/DataSources/Ado/ADO Sample ADO SampleDoc.h,v ADO SampleDoc 2010-05-21... (3 Replies)
Discussion started by: rodan90
3 Replies

6. Shell Programming and Scripting

Moving filenames containing spaces

I want to ftp all the sh files in the directory. Also if any of the file name contains spaces in them, it should be converted to underscores before it is ftped. I wrote the following code below: FILESSH=$(ls /mysh/*.sh) --- FILESH being used here for some other task --- echo "$FILESSH" |... (3 Replies)
Discussion started by: amicon007
3 Replies

7. Shell Programming and Scripting

spaces in filenames

Hi I hope someone will be able to resolve this little teaser! I am running a script for file in `ls directory` do echo "$file" ...other code here.... done this works fine unless we receive a file with a name which has a space in it ie "filena me" (I know its not good... (8 Replies)
Discussion started by: Bab00shka
8 Replies

8. Shell Programming and Scripting

spaces in filenames, for do

Hi All, I see similar problems in past threads but so far no answers have worked for me. I am trying to write a script which parses a txt file that contains one filename per line, then finds those files on the local disk and copies them to a specified directory. What I have: ... (4 Replies)
Discussion started by: naviztirf
4 Replies

9. Shell Programming and Scripting

Unix filenames and spaces

I have files on my unix boxes that users have created with spaces. Example: /tmp/project plan ls -l "/tmp/project plan" works fine. $/tmp>ls -l "/tmp/project plan" -rw-r--r-- 1 root other 0 Jan 31 12:32 /tmp/project plan I created a file called test and put just the... (2 Replies)
Discussion started by: x96riley3
2 Replies

10. Shell Programming and Scripting

spaces in filenames

I have a problem with the script below #!/bin/sh for vo in `find -maxdepth 1 -type f -regex "^\./*$"` do ls -l "$vo" some other commands done It works fine until `find ...` returns files with spaces. I've tryed to change IFS but haven't succeed Any solutions? (4 Replies)
Discussion started by: Hitori
4 Replies
Login or Register to Ask a Question