Linux - NewbieThis Linux forum is for members that are new to Linux.
Just starting out and have a question?
If it is not in the man pages or the how-to's this is the place!
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, folks. I was hoping I could get some help because I am completely stumped! I have tried different combinations and have not been able to figure it out. The question is:
Use ls -l and grep to find all the files in the directory /etc that were last modified in March.
I have tried a few of the following:
1. ls -l /etc | grep "Mar"
2. ls -l -f /etc | grep "Mar"
3. grep -l "Mar" /etc | ls -l
4. ls -latr /etc | grep "Mar"
5. ls -fl /etc | grep "Mar"
And so on, ad nauseam. I do not get the results I am looking for, and I cannot figure it out. I would appreciate a nudge in the right direction. Thanks.
Hello, folks. I was hoping I could get some help because I am completely stumped! I have tried different combinations and have not been able to figure it out. The question is:
Use ls -l and grep to find all the files in the directory /etc that were last modified in March.
I have tried a few of the following:
1. ls -l /etc | grep "Mar"
2. ls -l -f /etc | grep "Mar"
3. grep -l "Mar" /etc | ls -l
4. ls -latr /etc | grep "Mar"
5. ls -fl /etc | grep "Mar"
And so on, ad nauseam. I do not get the results I am looking for, and I cannot figure it out. I would appreciate a nudge in the right direction. Thanks.
...and...
Quote:
Originally Posted by robert_garcia718
I tried the l -ltr. That did not yield the results I needed either.
You don't tell us what you ARE getting, though, and on what version/distro of Linux.
Post the results of just doing "ls -l /etc" (just a few files will do), and post what you are getting as the results of the command(s) you've posted.
This list looks correct. Can you be more specific? What do you mean by "it fails"? Which command fails? What error messages are printed, what incorrect result do you obtain?
This is what I get. When I redirect output to a file, and test to see if it is correct, it fails, no matter what I do.
Please post the exact command you run and describe how it fails including any error messages.
If I run
Quote:
ls -l /etc | grep 'Mar' > etcmar.txt
It works as expected. However if I run it in a directory which I don't have permission to write to, such as /etc, it fails with a permission denied error message.
I am doing the linuxzoo.net tutorial. This is the original question:
Question 9: grep and ls
Use ls -l and grep to find all the files in the directory /etc that were last modified in March (hint: try looking for the case sensitive string "Mar"). Send this list to s6.
Tests: Complete
ls and grep FAILED
There is a button to test the output, and when I test it, it fails. I am trying to learn how to use Linux. Sorry, I am shamefully new.
Linux Centos 7 and I am doing the Linuxzoo.net tutorial on pipes, Question 9. This is what I get. When I redirect output to a file, and test to see if it is correct, it fails, no matter what I do.
Code:
-rw-r--r--. 1 root root 12288 Mar 9 2017 aliases.db
drwxr-xr-x. 3 root root 51 Mar 9 2017 udev
drwx------. 2 root root 4096 Mar 9 2017 grub.d
Thanks, but I asked to post just a FEW files...and when posting such things, use CODE tags to make it easier to read.
As others have said, it works for us, but you still haven't told us what YOU are seeing. We can't diagnose anything with "doesn't work" or "it fails". As has been stated here before, doing
Code:
ls -l /etc | grep "Mar"
..works fine, and will return any files with "Mar" in it. But since you're after modification date, you may want to look for "Mar " (note the space after Mar), because any files with the string Mar in them will ALSO come back.
grep looks everywhere in the line.
If there were a filename "Mary" it would be a false positive.
You can improve it a bit with
Code:
grep -w Mar
or
Code:
grep " Mar "
Further, the month name "Mar" for the date is locale dependent. You can enforce a US locale with
e.g.
Code:
LC_ALL=en_US ls -l /etc
Ensure that you specify a locale that is present/installed, with
Code:
locale -a
(Last, if the current month is "Mar" or close to it ("Feb" or "Apr") it might not work because ls -l switches to date/time presentation.)
EDIT: just seeing, in the US locales the month name is always present, regardless what presentation.
Last edited by MadeInGermany; 04-16-2020 at 07:28 AM.
The next question is stumping me as well. I can't seem to sort the results properly. The question is:
Question 10: ls grep and sort
Use ls -l and grep and sort to find all the files in /etc that were last modified in Jun. Sort this list in descending order of size and then alphabetically by name (so 2 files with the same size will appear in alphabetic order). Send the output to s7. Sorting using other techniques will probably not get the same answer...
Tests: Complete
Jun files in order FAILED
I typed this:
ls -l /etc | grep Jun | sort -k4 -k8 > s7
And got this:
[demo@host-2-65 ~]$ ls -l /etc | grep Jun | sort -k4 -k8
-rw-r-----. 1 root named 931 Jun 21 2007 named.rfc1912.zones
-rw-r--r--. 1 root root 0 Jun 10 2014 wvdial.conf
-rw-r--r--. 1 root root 0 Jun 7 2013 exports
-rw-r--r--. 1 root root 0 Jun 7 2013 motd
-rw-r--r--. 1 root root 1362 Jun 10 2014 pbm2ppa.conf
-rw-r--r--. 1 root root 1518 Jun 7 2013 aliases
-rw-r--r--. 1 root root 158 Jun 7 2013 hosts
-rw-r--r--. 1 root root 1787 Jun 10 2014 request-key.conf
-rw-r--r--. 1 root root 20 Jun 24 2014 fprintd.conf
-rw-r--r--. 1 root root 233 Jun 7 2013 printcap
-rw-r--r--. 1 root root 2620 Jun 10 2014 mtools.conf
-rw-r--r--. 1 root root 2872 Jun 10 2014 pinforc
-rw-r--r--. 1 root root 370 Jun 7 2013 hosts.allow
-rw-r--r--. 1 root root 451 Jun 9 2014 crontab
-rw-r--r--. 1 root root 460 Jun 7 2013 hosts.deny
-rw-r--r--. 1 root root 4760 Jun 9 2014 enscript.cfg
-rw-r--r--. 1 root root 5171 Jun 9 2014 man_db.conf
-rw-r--r--. 1 root root 6300 Jun 10 2014 pnm2ppa.conf
-rw-r--r--. 1 root root 6545 Jun 7 2013 protocols
-rw-r--r--. 1 root root 670293 Jun 7 2013 services
drwxr-xr-x. 2 root root 6 Jun 10 2014 popt.d
drwxr-xr-x. 2 root root 6 Jun 11 2014 terminfo
drwxr-xr-x. 2 root root 6 Jun 9 2014 cron.monthly
drwxr-xr-x. 2 root root 6 Jun 9 2014 cron.weekly
-rw-r--r--. 1 root root 841 Jun 7 2013 csh.login
-rw-r--r--. 1 root root 8892 Jun 10 2014 nanorc
-rw-r--r--. 1 root root 942 Jun 7 2013 inputrc
-rw-r--r--. 1 root root 9 Jun 7 2013 host.conf
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.