LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Fedora (https://www.linuxquestions.org/questions/fedora-35/)
-   -   ls grep and sort (https://www.linuxquestions.org/questions/fedora-35/ls-grep-and-sort-4175431222/)

mohitnarula 10-08-2012 05:53 PM

ls grep and sort
 
Hello everyone, my first question on this forum is:

Q. 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.

First I used this command to figure out the number of files which were changed in 2010:

ls -l /etc | grep 2010

This is the Output:

-rw-r--r--. 1 root root 662 Jun 28 2010 logrotate.conf
-rw-r--r--. 1 root root 801 Dec 22 2010 mke2fs.conf
-rw-r-----. 1 root named 1008 Jul 19 2010 named.conf
-rw-r--r--. 1 root named 487 Jul 19 2010 named.root.key
-rw-r--r--. 1 root root 99 Mar 13 2010 passwdqc.conf
-r--r-----. 1 root root 3338 Nov 30 2010 sudoers
-rw-r--r--. 1 root root 592 Dec 22 2010 usb_modeswitch.conf

And, then I used this command to sort the list:

ls -l /etc | grep 2010 | sort :

And the Output was

-r--r-----. 1 root root 3338 Nov 30 2010 sudoers
-rw-r-----. 1 root named 1008 Jul 19 2010 named.conf
-rw-r--r--. 1 root named 487 Jul 19 2010 named.root.key
-rw-r--r--. 1 root root 592 Dec 22 2010 usb_modeswitch.conf
-rw-r--r--. 1 root root 662 Jun 28 2010 logrotate.conf
-rw-r--r--. 1 root root 801 Dec 22 2010 mke2fs.conf
-rw-r--r--. 1 root root 99 Mar 13 2010 passwdqc.conf


Because I got the right output in descending order straight-away, I then copied it to a new file s7.

ls -l /etc | grep 2010 | sort > s7.

But, unluckily when I am checking it on linuxzoo.net, it is coming up as "FAILED" don't know why.

Any Suggestion, if I am not doing it right??

Thanks

technicalthug 10-08-2012 06:07 PM

try the option -S for ls

Code:

ls -lS
This will order by size :)

Something like

Code:

ls -laS /etc | grep 2010 > ls_output.txt

Rupadhya 10-09-2012 01:14 AM

1 Attachment(s)
I went to http://linuxzoo.net/ and tried to recreate your problem. It is working fine for me. On my local machine...
Code:

$ls -l /etc | grep 2011 | sort > s7
$more s7
-rw-r--r--.  1 root root      662 Aug 31  2011 logrotate.conf

I attached a screenshot from linuxzoo.net as well.

- Raj

mohitnarula 10-09-2012 03:24 AM

Quote:

Originally Posted by technicalthug (Post 4800721)
try the option -S for ls

Code:

ls -lS
This will order by size :)

Something like

Code:

ls -laS /etc | grep 2010 > ls_output.txt


Thanks for your reply, appreciate it.

I tried using your command but it ain't working too, dunno why.

mohitnarula 10-09-2012 03:26 AM

Quote:

Originally Posted by Rupadhya (Post 4800941)
I went to http://linuxzoo.net/ and tried to recreate your problem. It is working fine for me. On my local machine...
Code:

$ls -l /etc | grep 2011 | sort > s7
$more s7
-rw-r--r--.  1 root root      662 Aug 31  2011 logrotate.conf

I attached a screenshot from linuxzoo.net as well.

- Raj

Thanks to you to Raj, I am onto the same thing, I used this command before, getting the same output but if you look at it very closely, it ain't in descending order. And I believe that's why it is not working. But I did sort --help just now, to see if I can get anything out from there.

mohitnarula 10-09-2012 03:42 AM

Got the right command, from my professor!
 
So basically, the half of part of the command was right until

ls -l /etc | grep 2010 | sort

but then as the question says we need to sort it in Size order which is Column 5, so:

ls -l /etc | grep 2010 | sort -k5

and then we do it in numeric order:

ls -l /etc | grep 2010 | sort -k5 -n

and then that sorts in ascending order, so to reverse it and copy it in s7 we use:

ls -l /etc | grep 2010 | sort -k5 -nr > s7

Rupadhya 10-09-2012 08:40 AM

Great! Looks like you figured it out! Have a good day! :-)

- Raj

David the H. 10-09-2012 11:12 AM

If the assignment said to use ls and grep, then I guess you have to, but you really shouldn't.

Something like this is longer, but safer:

Code:

while IFS='' read -r -d '' fname; do

        read size date <<<"$( stat -c '%s %y' "$fname" )"
        [[ $date == 2010* ]] && echo "$size $fname"

done < <( find /etc -type f -newermt 12/31/2009 -print0 ) | sort -k1,1nr >s7

Note that I used features specific both to bash and the gnu version of find (and possibly stat), however. It would have to be adapted to other shells and systems. "-newermt", at least, could be left out, as I only used it to pre-limit the files tested.

technicalthug 10-09-2012 10:29 PM

Just for future reference, this is all built into the ls command.

Quote:

jason@wheezer:/$ ls --version
ls (GNU coreutils) 8.13
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David Mac
Code:

ls -lSr
Code:

-S    sort by file size
-r, --reverse
      reverse order while sorting

Are you able to show the output of "ls --version" ?

mohitnarula 10-10-2012 03:49 AM

Quote:

Originally Posted by technicalthug (Post 4801774)
Just for future reference, this is all built into the ls command.



Code:

ls -lSr
Code:

-S    sort by file size
-r, --reverse
      reverse order while sorting

Are you able to show the output of "ls --version" ?


Thanks for replying:
Here's the output of "ls --version" command:


[demo@host-7-89 ~]$ ls --version
ls (GNU coreutils) 8.10
Copyright (C) 2011 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Richard M. Stallman and David MacKenzie.


Well, I mean, I have just started learning Linux, from last 3 weeks only so I really don't know much about it.
Because I'm still a Beginner, we'll are being told to you use the sort command, to usually sort anything out.


All times are GMT -5. The time now is 10:01 AM.