Piping grep and ls

robert_garcia718

New Member
Joined
Apr 15, 2020
Messages
4
Reaction score
2
Credits
0
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.
 


Works here:
Code:
ls -l
total 11526784
-rw-rw-r-- 1 john john 1193279488 Feb 21 23:50  antiX-19.1_386-full.iso
-rw-rw-r-- 1 john john  341835776 Nov  4 20:20  gparted-live-1.0.0-5-i686-pae.iso
-rw-rw-r-- 1 john john 1955266560 Jul 28  2019  kubuntu-18.04.2-desktop-i386.iso
-rw-rw-r-- 1 john john 1729101824 Jan 18  2018  linuxmint-18.2-mate-32bit.iso
-rw-rw-r-- 1 john john 1970274304 Nov 11  2018  linuxmint-19-mate-32bit.iso
-rw-rw-r-- 1 john john        774 Jan 16  2018 'Mint 18.2 SHA 256.txt'
-rw-rw-r-- 1 john john 1412431872 Jan  1  2019  Peppermint-9-20181222-i386.iso
-rw-rw-r-- 1 john john  454033408 Sep 28  2019  q4os-3.8-i386-instcd.r5.iso
-rw-rw-r-- 1 john john  704643072 Sep  5  2017  rescatux-0.41b1.iso
-rw-rw-r-- 1 john john   20404224 Sep  5  2017  super_grub2_disk_hybrid_2.02s9.iso
-rw-rw-r-- 1 john john 2022113280 Nov  3  2018  ubuntu-mate-18.04.1-desktop-i386.iso
Code:
s -l|grep Feb
-rw-rw-r-- 1 john john 1193279488 Feb 21 23:50 antiX-19.1_386-full.iso
Code:
ls -l|grep Sep
-rw-rw-r-- 1 john john  454033408 Sep 28  2019 q4os-3.8-i386-instcd.r5.iso
-rw-rw-r-- 1 john john  704643072 Sep  5  2017 rescatux-0.41b1.iso
-rw-rw-r-- 1 john john   20404224 Sep  5  2017 super_grub2_disk_hybrid_2.02s9.iso
Code:
ls -l|grep "Sep"
-rw-rw-r-- 1 john john  454033408 Sep 28  2019 q4os-3.8-i386-instcd.r5.iso
-rw-rw-r-- 1 john john  704643072 Sep  5  2017 rescatux-0.41b1.iso
-rw-rw-r-- 1 john john   20404224 Sep  5  2017 super_grub2_disk_hybrid_2.02s9.iso

What's your distro?
 
It should have worked either way, as shown in the last two outputs from my Kubuntu, but glad it's working for you now!
 
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


No go.
 
Try sending the output of grep into sort:
Code:
ls -l /etc | grep Jun > sort -k4 -k8

It's been a (LOOOOOONG) while since I dabbled in scripting stuff like that. But, what heck ....
 
Right, but are the columns right? The columns I am asking to be sorted are not being sorted at all. Also, I have to send the output of the sort to a file labeled s7, can I send output twice?
 
I've never done much scripting, but have you tried it? It's not like you're going start thermonuclear war
:D:D
 
In looking at man, have you tried using the --keydef=4,8 instead? It appears, from your output, to be sorting on the -k4, but ignoring the -k8.
 

Members online


Latest posts

Top