| Fedora This forum is for the discussion of the Fedora Project. |
| Notices |
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
 |
GNU/Linux Basic Guide
This 255-page guide will provide you with the keys to understand the philosophy of free software, teach you how to use and handle it, and give you the tools required to move easily in the world of GNU/Linux. Many users and administrators will be taking their first steps with this GNU/Linux Basic guide and it will show you how to approach and solve the problems you encounter.
Click Here to receive this Complete Guide absolutely free. |
|
 |
10-08-2012, 05:53 PM
|
#1
|
|
LQ Newbie
Registered: Oct 2012
Posts: 24
Rep: 
|
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
|
|
|
|
10-08-2012, 06:07 PM
|
#2
|
|
LQ Newbie
Registered: Sep 2012
Distribution: Gentoo, FreeBSD, CentOS
Posts: 18
Rep: 
|
try the option -S for 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.
|
|
|
|
10-09-2012, 01:14 AM
|
#3
|
|
Member
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora
Posts: 164
Rep: 
|
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
|
|
|
|
10-09-2012, 03:24 AM
|
#4
|
|
LQ Newbie
Registered: Oct 2012
Posts: 24
Original Poster
Rep: 
|
Quote:
Originally Posted by technicalthug
try the option -S for 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.
|
10-09-2012, 03:26 AM
|
#5
|
|
LQ Newbie
Registered: Oct 2012
Posts: 24
Original Poster
Rep: 
|
Quote:
Originally Posted by Rupadhya
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.
|
10-09-2012, 03:42 AM
|
#6
|
|
LQ Newbie
Registered: Oct 2012
Posts: 24
Original Poster
Rep: 
|
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.
|
10-09-2012, 08:40 AM
|
#7
|
|
Member
Registered: Sep 2012
Location: Hoffman Estates, IL
Distribution: Fedora
Posts: 164
Rep: 
|
Great! Looks like you figured it out! Have a good day! :-)
- Raj
|
|
|
|
10-09-2012, 11:12 AM
|
#8
|
|
Bash Guru
Registered: Jun 2004
Location: Osaka, Japan
Distribution: Debian sid + kde 3.5 & 4.4
Posts: 6,577
|
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
|
|
|
|
10-09-2012, 10:29 PM
|
#9
|
|
LQ Newbie
Registered: Sep 2012
Distribution: Gentoo, FreeBSD, CentOS
Posts: 18
Rep: 
|
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:
-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.
|
|
|
|
10-10-2012, 03:49 AM
|
#10
|
|
LQ Newbie
Registered: Oct 2012
Posts: 24
Original Poster
Rep: 
|
Quote:
Originally Posted by technicalthug
Just for future reference, this is all built into the ls command.
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.
|
|
|
|
| Thread Tools |
Search this Thread |
|
|
|
Posting Rules
|
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
HTML code is Off
|
|
|
All times are GMT -5. The time now is 06:18 AM.
|
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|