LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   How to sort a list in decending order (http://www.linuxquestions.org/questions/linux-newbie-8/how-to-sort-a-list-in-decending-order-905573/)

drone01 09-29-2011 05:12 AM

How to sort a list in decending order
 
Use ls -l and grep to find all the files in /etc that were last modified in 2010. Sort this list in descending order of size and send the output to s7.

#I have spent hours looking for the answer to this.

snooly 09-29-2011 05:24 AM

If you look at "man ls", you will see that the -t option lets you sort by modification time, and -S is for sorting by file size. You could pipe the results from ls into grep, searching for the pattern 2010.

What is s7?

drone01 09-29-2011 05:27 AM

Quote:

Originally Posted by snooly (Post 4485406)
If you look at "man ls", you will see that the -t option lets you sort by modification time, and -S is for sorting by file size. You could pipe the results from ls into grep, searching for the pattern 2010.

What is s7?

s7 is the name of file to direct the output to. i'll try it now and get bacK asap

snooly 09-29-2011 05:28 AM

Quote:

Originally Posted by drone01 (Post 4485408)
s7 is the name of file to direct the output to. i'll try it now and get bacK asap

Okay it sounds like you know what you are doing. I predict success very soon!

drone01 09-29-2011 05:34 AM

Quote:

Originally Posted by drone01 (Post 4485408)
s7 is the name of file to direct the output to. i'll try it now and get bacK asap

i used: ls -lts /etc | grep '2010' | sort -n > s7

still didnt work

snooly 09-29-2011 05:35 AM

Quote:

Originally Posted by drone01 (Post 4485414)
i used: ls -lts /etc | grep '2010' | sort -n > s7

still didnt work

"didn't work" isn't enough information. you have to say what happened and in what way it didn't meet your expectations. Also you need to read the ls man page more carefully. -s is not the same as -S.

deep27ak 09-29-2011 05:41 AM

try this command

Quote:

#ls -lS /etc/
this will sort all the files in descending order

Best of Luck

drone01 09-29-2011 05:58 AM

[QUOTE=deep27ak;4485417]try this command



this will sort all the files in descending order

Best of Luck[/QUOTE



i have used: ls -lS /etc | grep '2010' | sort > s7 &
ls -lts /etc | grep '2010' | sort > s7

works the same magic Thank you very much guys for help. I just have to see the tutor to find out whats happening.

snooly 09-29-2011 06:07 AM

the ls -S command already sorted the files, so you don't need the last sort command.

ls -lts is just wrong for the problem you're trying to solve.

colucix 09-29-2011 06:10 AM

Most likely you have to specify which is the field to use for sorting (size is usually the 5th field in the output of ls). Take a look at option -k of the sort command. Anyway, you don't need to pipe the results to sort if you already use the -S option of ls, unless you use ls -lR to descend recursively into subdirectories (beware the question is "to find all the files in /etc").


All times are GMT -5. The time now is 10:00 PM.