FedoraThis 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.
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.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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.
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.
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
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.
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.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.