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? |
if you look at 'man ls', have a try of:
ls -r or slightly nicer ls -l | sort -r does that help? |
yup buddy it worked.
thanks a lot |
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? |
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 |
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 |
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. |
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 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? |
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. |
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. |
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? |
ok let me try this one now.
i ll get back to u once i try it. |
hey can u just explain what this
./p is doing? |
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' |
ok but i need to handle it within scripts
|
All times are GMT -5. The time now is 03:41 PM. |