LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 03-27-2005, 11:32 PM   #1
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Rep: Reputation: 15
how to sort the output of ls


now when i use the ls command . it gives the output in the form that directories come first and files later on.

i want that files should come first and directories at the end.

how do i do this?

list=`ls|sort`

now i dont know how to use sort to do this?

any suggestions?
 
Old 03-28-2005, 12:13 AM   #2
uberNUT69
Member
 
Registered: Jan 2005
Location: Tasmania
Distribution: Xen Debian Lenny/Sid
Posts: 578

Rep: Reputation: 30
if you look at 'man ls', have a try of:
ls -r

or slightly nicer

ls -l | sort -r


does that help?
 
Old 03-28-2005, 12:15 AM   #3
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
yup buddy it worked.

thanks a lot
 
Old 03-28-2005, 12:22 AM   #4
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
now i only got one problem left.

handling spaces.

i have tried every thing to do it.

do u know of any way to handle spaces withint file names and directory names

the problem is that when i do a

file=`ls`

i dont have any problem getting the filenames

but when i try to process the variable $file what it does is that it handles every word as a separate file.

now any suggestions about this?
 
Old 03-28-2005, 01:01 AM   #5
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
use double quotes for example
file=`ls`
for i in "$file"; do echo "$i"; done

compare it with
file=`ls`
for i in $file; do echo $i; done
 
Old 03-28-2005, 01:08 AM   #6
uberNUT69
Member
 
Registered: Jan 2005
Location: Tasmania
Distribution: Xen Debian Lenny/Sid
Posts: 578

Rep: Reputation: 30
on a command line you use a \ to precede non-alpha(err .. whatever) characters

eg. if a file is called 'my- -file.txt'
rm -f my-\ -file.txt
wil delete it
 
Old 03-28-2005, 01:50 AM   #7
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
It looks like you have a different way of handling files in scripts then I do.

I usually use a loop such as

for file in <pattern>; do
mv "$file" "${file// /_}"
done

You have the output of ls in the $file variable. If you look at 'echo $file', all of the extra whitespace is removed, including the newlines. If you look at 'echo "$file"' the lines are retained. However if you try

for item in "$file"; do
echo ":$item:"
done

you will see that even though the files are printed like you want, the $item variable contains everything.
 
Old 03-28-2005, 02:01 AM   #8
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
buddy UberNut i couldnt understand what u said

but havea look at my problem

when i use the command ls like this

Code:
[jnsahibz@charlie jnsahibz]$ ls
CV.tar.gz        Favorites        assignment       fidelma.txt.txt  mail             workspace
Desktop          My Documents     bin              junaid.zip       public_html      zakir naik
now u see there is this directory MY Documents which is clearly shown as one directory and not as two.

but the problem starts from here

when i save this output into another variable. and then i try to pipe it into another command or pass it to another script instead of treating it as one directory this time it treats it as two separate directories.


now how do i get around this?
 
Old 03-28-2005, 02:22 AM   #9
jschiwal
LQ Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677Reputation: 677
Another method you can use is to use the find command with the -print0 option, and pipe the output to the 'xargs --null' command.

for example:
find ./ -type f -print0 -maxdepth 1 | xargs --null file

One advantage of this is using the -L or --max-lines option for very long results. Otherwise you might get a memory allocation error.
 
Old 03-28-2005, 02:43 AM   #10
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
oh well the trouble is that i am not supposed to use the find command.

this is a task. i have completed the whole task but i am having trouble handling the spaces.
 
Old 03-28-2005, 03:17 AM   #11
zeropash
Member
 
Registered: Apr 2003
Location: Bangalore,India
Distribution: FC2, RHES, RH9, FC3, FC1, Slackware 3.0
Posts: 208

Rep: Reputation: 30
huh let me try to tell this one more time.
say I have a script p

#!/bin/bash
echo "$1"

my dir listing is something like this
ls -l
total 8
-rw-rw-r-- 1 rprakash rprakash 0 Mar 28 12:18 asfd space
-rwxrwxr-x 1 rprakash rprakash 103 Mar 28 14:35 p
-rwxrwxr-x 1 rprakash rprakash 122 Mar 28 13:12 t

if I do something like this
for i in `ls`; do ./p $i; done
the output will be
asfd
space
p
t

but if I do something like
for i in "`ls`"; do ./p "$i"; done
the output will be
asfd space
p
t

isnt this what you wanted?
 
Old 03-28-2005, 03:25 AM   #12
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
ok let me try this one now.

i ll get back to u once i try it.
 
Old 03-28-2005, 03:40 AM   #13
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
hey can u just explain what this

./p is doing?
 
Old 03-28-2005, 04:26 AM   #14
uberNUT69
Member
 
Registered: Jan 2005
Location: Tasmania
Distribution: Xen Debian Lenny/Sid
Posts: 578

Rep: Reputation: 30
sorry bahadur,
I wasn't talking about scripting.

But to refer to your example,
I mean that 'My Documents'
can be referred to on the command line as 'My\ Documents'
 
Old 03-28-2005, 04:35 AM   #15
bahadur
Member
 
Registered: Apr 2004
Distribution: Linux Red Hat
Posts: 141

Original Poster
Rep: Reputation: 15
ok but i need to handle it within scripts
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
C++ Sort() bendeco13 Programming 2 11-02-2005 11:26 PM
sort grep output wijnands Linux - Newbie 4 10-09-2004 07:14 AM
Via AC'97 5.1 Optical Output or Audigy 4.1 Output Nza Fedora 3 06-01-2004 07:49 AM
sort pantera Programming 5 05-26-2004 07:36 PM
the sound gives output when using mic but no output when run a music file medo Debian 0 04-19-2004 07:17 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:51 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration