PDA

View Full Version : A question about ls.


Sykora
26-08-2006, 08:17 AM
Is there any way to make the ls command display directories first, then the files? I tried the ls -X switch, and while this puts the directories first, it groups the rest of the fiels by extension, something I don't really want. I want the firectories to be in alphabetical order, followed by the rest of the files, also in alphabetical order Is there a way, by aliases or shell scripts? I'm a bit rusty on shell scripting so I thought I'd ask for help.

--

As a sidenote, I think the digit forums should open a new sub-forum for programming related questions.

romeo_8693
26-08-2006, 10:30 AM
i agree with syk,open a dedicated sub-forum for prog...

JGuru
26-08-2006, 12:00 PM
To list only directories, try this command:

$ ls -d */

To pause after a page listing, try this
$ ls -d */ |more

To move to the next page, press the 'TAB' key.

Sykora
26-08-2006, 12:28 PM
It seems I haven't made myself clear enough. I want to modify the ls command to list directories first, files later, all in alphabetic order, if possible, ignoring the dot in dotfiles when sorting them. ls -d */ is what I need for directories, but the problem comes in listing only files afterwards.

mediator
26-08-2006, 04:01 PM
Ahhhhhh............Shellscripting!!!!!!! YAY!
Why do u wanna modify ls?? Ur answer is a shellscript!!
Ok here's what u shud dooo!!

1. ********Save the following as "ls.sh"************************************************** ********

echo -e "\033[40m"
echo -e "\033[32m"
echo -e "\033[1m"
echo "************FILEZ*************"
while [ $1 ]
do
if [ -f $1 ]; then
echo $1
fi
shift
done

echo -e "\033[33m"
echo "***********Directories********"
ls -d */

echo -e "\033[0m"
echo -e "\033[30m"

************************END OF "ls.sh"************************************************** ****

2. command "chmod +x ls.sh"
3. Copy the file to any PATH idirectory ......or make a directory "bin" in ur home folder and add it to ur path and then copy "ls.sh" in "bin"!
4. Run as "ls.sh `ls`"

But first test as " sh entire_path/ls.sh `ls` "

Ur problem solved !!.... old buddy O pal!!

Sykora
26-08-2006, 04:49 PM
Can you explain exactly what you've written? I don't understand it, and it's not working. In a folder with only some files and no folders, it returns errors saying "no matches found; */". I'm also trying to make it accept the ls options ie long, all, nobackup, etc.

mediator
26-08-2006, 05:32 PM
Can you explain exactly what you've written? I don't understand it, and it's not working. In a folder with only some files and no folders, it returns errors saying "no matches found; */". I'm also trying to make it accept the ls options ie long, all, nobackup, etc.
Hey man....take it easy! dat was soooo ruuude!! I think u still remember our fight in the fight club!! That was restricted to the fight club only. Anyways.....whateva I have written can be undesrtood even by a newbie.
I help everyperson in this forum irrespective of his name or whateva. Neways I gave u the PERFECT solution. Its upto u to use it.
And about "no matches found"......I dunno what u r saying........I have various kinda of shell scripts I made in last 2 yrs. And this one I gave to a dozen of mah noobie friends. All said "PERFECT".
Neways.....wat can i say.......dont use it then!! And if works lemme know.

Sykora
26-08-2006, 05:36 PM
I use zsh. Could that make a difference?

mediator
26-08-2006, 05:59 PM
Yea it might...i dunno ! Anyways try bash !!

Sykora
26-08-2006, 06:31 PM
I figured it out myself, thanks anyway, mediator, for your help.

For those who want to know, my solution was this :

alias ls='ls -hl --color=always | grep --color=never ^d; ls -hl --color=always | grep --color=never ^-'
symlinks and other filetypes can be added as necessary.

I'm still waiting for an answer from the mods about a programming subforum... :)

mediator
26-08-2006, 09:31 PM
Ya ur solutions is better.......but is too lengthy. U can make shellscript of it too for simpler use. Also u r using aliasing here.....so u have to put that alias in path or u have to first write that whole code to initialize the shell everytime u open it. Its one and the same thing like I posted mah solution in either case u have to put it in path.

Neways.....Allow me to make ur solution shorter without disturbing the existing ls


ls -hl | sort

What say?
But if ur satisfied with ur solution then put it in a shellscript!!