LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Distributions > Fedora
User Name
Password
Fedora This forum is for the discussion of the Fedora Project.

Notices

Closed Thread
 
Search this Thread
Old 10-08-2012, 05:53 PM   #1
mohitnarula
LQ Newbie
 
Registered: Oct 2012
Posts: 24

Rep: Reputation: Disabled
Exclamation 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
 
Old 10-08-2012, 06:07 PM   #2
technicalthug
LQ Newbie
 
Registered: Sep 2012
Distribution: Gentoo, FreeBSD, CentOS
Posts: 18

Rep: Reputation: Disabled
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

Last edited by technicalthug; 10-08-2012 at 06:11 PM.
 
Old 10-09-2012, 01:14 AM   #3
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
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
Attached Images
File Type: png Screenshot from 2012-10-09 01:08:43.png (11.1 KB, 5 views)
 
Old 10-09-2012, 03:24 AM   #4
mohitnarula
LQ Newbie
 
Registered: Oct 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by technicalthug View Post
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.
 
1 members found this post helpful.
Old 10-09-2012, 03:26 AM   #5
mohitnarula
LQ Newbie
 
Registered: Oct 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by Rupadhya View Post
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.

Last edited by mohitnarula; 10-09-2012 at 03:29 AM.
 
1 members found this post helpful.
Old 10-09-2012, 03:42 AM   #6
mohitnarula
LQ Newbie
 
Registered: Oct 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Smile 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
 
1 members found this post helpful.
Old 10-09-2012, 08:40 AM   #7
Rupadhya
Member
 
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora 20
Posts: 167

Rep: Reputation: Disabled
Smile

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

- Raj
 
Old 10-09-2012, 11:12 AM   #8
David the H.
Bash Guru
 
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,823

Rep: Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946Reputation: 1946
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.

Last edited by David the H.; 10-09-2012 at 11:13 AM. Reason: word went missing
 
Old 10-09-2012, 10:29 PM   #9
technicalthug
LQ Newbie
 
Registered: Sep 2012
Distribution: Gentoo, FreeBSD, CentOS
Posts: 18

Rep: Reputation: Disabled
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" ?

Last edited by technicalthug; 10-09-2012 at 10:36 PM.
 
Old 10-10-2012, 03:49 AM   #10
mohitnarula
LQ Newbie
 
Registered: Oct 2012
Posts: 24

Original Poster
Rep: Reputation: Disabled
Quote:
Originally Posted by technicalthug View Post
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.
 
  


Closed Thread


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
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Using grep -v to sort out a log rhbegin Programming 7 05-11-2011 04:29 PM
grep+awk+sed+paste+sort in one script? mchriste Linux - Software 13 03-05-2009 01:57 PM
Is there a line limit with the sort utility? Trying to sort 130 million lines of text gruffy Linux - General 4 08-10-2006 08:40 PM
sort grep output wijnands Linux - Newbie 4 10-09-2004 07:14 AM


All times are GMT -5. The time now is 12:33 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