LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Software
User Name
Password
Linux - Software This forum is for Software issues.
Having a problem installing a new program? Want to know which application is best for the job? Post your question in this forum.

Notices

Reply
 
Search this Thread
Old 06-21-2004, 03:05 AM   #1
Beppe83
Member
 
Registered: Mar 2004
Posts: 41

Rep: Reputation: 15
Sorting


Hi all.

How is possible to apply a sorting to the output of an "ls" command?

I need to sort by biggest dimension and smallest dimension.
 
Old 06-21-2004, 04:23 AM   #2
cck23
Member
 
Registered: Jun 2004
Location: Surrey, UK
Distribution: Mandrake 10 Download
Posts: 73

Rep: Reputation: 15
There are many ways to do a sort so it might help if you gave a specific example so we can help with a specific solution.

Otherwise you might want to try the following:

ls -l | sort -k 'n'

eg.

ls -l | sort 5

would sort by the file size

Let us know if that helps.

CK
 
Old 06-21-2004, 04:36 AM   #3
Beppe83
Member
 
Registered: Mar 2004
Posts: 41

Original Poster
Rep: Reputation: 15
We used "tail" and "head" command to display only the biggest "n" files or the smallest; the head works fine, the tail no.
 
Old 06-21-2004, 05:10 AM   #4
cck23
Member
 
Registered: Jun 2004
Location: Surrey, UK
Distribution: Mandrake 10 Download
Posts: 73

Rep: Reputation: 15
Would it be possible for you to supply an example of what you are typing?

Cheers

CK
 
Old 06-21-2004, 07:16 AM   #5
Beppe83
Member
 
Registered: Mar 2004
Posts: 41

Original Poster
Rep: Reputation: 15
ls - l --sort=size | head -n 5

ls - l --sort=size | tail -n 5
 
Old 06-21-2004, 07:39 AM   #6
cck23
Member
 
Registered: Jun 2004
Location: Surrey, UK
Distribution: Mandrake 10 Download
Posts: 73

Rep: Reputation: 15
right...

I've tried this and it worked for me, so.... for you:

- when you say it didn't work, what error or output do you get?
- does it fail for any ls or just some directories?
- to find out if there is a general problem with 'tail', you could cat a test file through "tail -n 5" and see if that works
- if tail does not work, try sed

eg.

ls -l sort=size | sed -n '3,$p'
ls -l sort=size | sed -n '45,51p'

tip: if you want to know last five lines they quickly pipe through "wc -l" to find out how many lines in output and then set the sed command as appropriate. Sorry this is messy but I am not sed expert and that is what i would do, no doubt sed experts out there will tell us how to use sed to get last x lines?



CK
 
Old 06-21-2004, 09:05 AM   #7
Beppe83
Member
 
Registered: Mar 2004
Posts: 41

Original Poster
Rep: Reputation: 15
Ok probably I expalined badly the problem.

I'm making a script that works on folder and directories and shows them with some sorting criteria; now the point is: I have 2 options: see files with biggest dimension or see files with smallest dimension. So how it works: simple: I always use "ls -l --sort=size" but one time I use "| head -n 10" and the other one "| tail -n 10".

When I use the "tail" command it doesn't display the right files...but again the biggest...
 
Old 06-21-2004, 09:10 AM   #8
cck23
Member
 
Registered: Jun 2004
Location: Surrey, UK
Distribution: Mandrake 10 Download
Posts: 73

Rep: Reputation: 15
Ok - in that case, so we know if it's a problem with the shell, the tail command or your script. Does that "tailing" work if you do it from the command line prompt?

I would guess that it's more likely that you have an error with your script than a problem with the tail command, but without seeing your script, it's hard to say. Let us know if the tail works as it should from plain command line....


good luck

CK
 
  


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
Sorting data. unreal128 Linux - Software 1 09-20-2005 06:31 PM
sorting a map (C++) Thinking Programming 6 09-13-2005 04:52 PM
Which sorting algorithm? nodger Programming 6 01-28-2005 06:13 PM
sorting and stuff arnulfo Programming 13 01-01-2005 04:37 PM
Sorting question AMMullan Linux - General 12 01-19-2004 03:09 PM


All times are GMT -5. The time now is 09:19 PM.

Main Menu
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
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration