LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to sort the output of ls (https://www.linuxquestions.org/questions/programming-9/how-to-sort-the-output-of-ls-306864/)

bahadur 03-27-2005 11:32 PM

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?

uberNUT69 03-28-2005 12:13 AM

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

or slightly nicer

ls -l | sort -r


does that help?

bahadur 03-28-2005 12:15 AM

yup buddy it worked.

thanks a lot

bahadur 03-28-2005 12:22 AM

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?

zeropash 03-28-2005 01:01 AM

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

uberNUT69 03-28-2005 01:08 AM

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

jschiwal 03-28-2005 01:50 AM

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.

bahadur 03-28-2005 02:01 AM

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?

jschiwal 03-28-2005 02:22 AM

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.

bahadur 03-28-2005 02:43 AM

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.

zeropash 03-28-2005 03:17 AM

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?

bahadur 03-28-2005 03:25 AM

ok let me try this one now.

i ll get back to u once i try it.

bahadur 03-28-2005 03:40 AM

hey can u just explain what this

./p is doing?

uberNUT69 03-28-2005 04:26 AM

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'

bahadur 03-28-2005 04:35 AM

ok but i need to handle it within scripts


All times are GMT -5. The time now is 03:41 PM.