LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This 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


Reply
  Search this Thread
Old 04-15-2020, 03:19 PM   #1
robert_garcia718
LQ Newbie
 
Registered: Apr 2020
Posts: 6

Rep: Reputation: Disabled
piping grep and ls


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.
 
Old 04-15-2020, 04:06 PM   #2
ehartman
Senior Member
 
Registered: Jul 2007
Location: Delft, The Netherlands
Distribution: Slackware
Posts: 1,674

Rep: Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888Reputation: 888
Quote:
Originally Posted by robert_garcia718 View Post
1. ls -l /etc | grep "Mar"
That should be the one if YOUR ls reports the month as text (mine doesn't):
Quote:
$ . ls -l bin
-rwxr-xr-x 1 hartman beheer 287 2016-03-24 12:00 slcop
-rwxr-xr-x 1 hartman beheer 602 2017-01-01 12:00 sldif
-rwxr-xr-x 1 hartman beheer 737 2012-01-01 12:00 slget
-rwxr-xr-x 1 hartman beheer 532 2020-02-23 17:00 sllog
(partial listing, just to show the format)

So for me the grep would have to test for "[0-9]-03-" (as you do not want a - as the first char, grep will consider it to be an option).

Do a 'ls -l /etc' first to see what the default output is in your system.
 
Old 04-15-2020, 04:12 PM   #3
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
Code:
ls -ltr /etc
Will put the most recent files at the bottom of the list, so those changed last month will be right there.
 
Old 04-15-2020, 04:38 PM   #4
robert_garcia718
LQ Newbie
 
Registered: Apr 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
I tried the l -ltr. That did not yield the results I needed either.
 
Old 04-15-2020, 04:57 PM   #5
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by robert_garcia718 View Post
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.
 
Old 04-15-2020, 05:22 PM   #6
robert_garcia718
LQ Newbie
 
Registered: Apr 2020
Posts: 6

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

-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
-rw-r--r--. 1 root root 78366 Mar 9 2017 ld.so.cache
drwxr-xr-x. 2 root root 4096 Mar 9 2017 alternatives
drwxr-xr-x. 2 root root 4096 Mar 9 2017 bash_completion.d
drwxr-xr-x. 2 root root 54 Mar 9 2017 cron.daily
drwxr-xr-x. 2 root root 21 Mar 9 2017 cron.hourly
drwxr-xr-x. 6 root root 95 Mar 9 2017 yum
drwxr-xr-x. 2 root root 51 Mar 9 2017 cron.d
drwx------. 4 root root 4096 Mar 9 2017 libvirt
drwxr-xr-x. 2 root root 4096 Mar 9 2017 logrotate.d
drwxr-xr-x. 2 root root 6 Mar 9 2017 tmpfiles.d
drwxr-xr-x. 2 root root 24 Mar 9 2017 rsyslog.d
drwxr-xr-x. 2 root root 6 Mar 9 2017 glusterfs
-rw-r--r--. 1 root root 813 Mar 9 2017 hba.conf
drwxr-xr-x. 2 root root 6 Mar 9 2017 gtk-2.0
drwxr-xr-x. 2 root root 22 Mar 9 2017 bluetooth
drwxr-xr-x. 2 root root 4096 Mar 9 2017 ld.so.conf.d
drwxr-xr-x. 4 root root 4096 Mar 9 2017 vmware-tools
drwxr-xr-x. 3 root root 19 Mar 9 2017 gss
drwxr-xr-x. 2 root root 88 Mar 9 2017 PackageKit
drwx------. 3 root root 93 Mar 9 2017 ipsec.d
drwxr-xr-x. 2 root root 4096 Mar 9 2017 ssh
drwxr-xr-x. 3 root root 32 Mar 9 2017 scl
drwxr-xr-x. 4 root root 94 Mar 9 2017 avahi
drwxr-xr-x. 3 root root 71 Mar 9 2017 smartmontools
drwxr-xr-x. 2 root root 4096 Mar 9 2017 pam.d
drwxr-xr-x. 3 root root 48 Mar 9 2017 qemu-ga
drwxr-xr-x. 5 root root 86 Mar 9 2017 httpd
drwxr-xr-x. 2 root root 45 Mar 9 2017 rwtab.d
drwxr-xr-x. 2 root root 4096 Mar 9 2017 profile.d
drwxr-xr-x. 2 root root 25 Mar 9 2017 cifs-utils
drwxr-xr-x. 2 root root 74 Mar 9 2017 request-key.d
lrwxrwxrwx. 1 root root 22 Mar 9 2017 grub2.cfg -> ../boot/grub2/grub.cfg
drwxr-xr-x. 2 root root 64 Mar 9 2017 my.cnf.d
drwxr-xr-x. 2 root root 41 Mar 9 2017 default
drwxr-xr-x. 2 root root 67 Mar 9 2017 tuned
drwxr-xr-x. 2 root root 84 Mar 9 2017 modprobe.d
drwxr-xr-x. 3 root root 17 Mar 9 2017 firefox
drwxr-xr-x. 5 root lp 4096 Mar 9 2017 cups
drwxr-xr-x. 6 root root 101 Mar 9 2017 gdm
drwxr-xr-x. 2 root root 33 Mar 9 2017 cupshelpers
drwxr-xr-x. 2 root root 49 Mar 9 2017 qemu-kvm
drwxr-xr-x. 6 root root 97 Mar 9 2017 X11
drwxr-xr-x. 2 root root 12288 Mar 9 2017 brltty
drwxr-xr-x. 2 root root 31 Mar 9 2017 at-spi2
drwxr-xr-x. 2 root root 75 Mar 9 2017 pulse
-rw-r--r--. 1 root root 1016 Mar 9 2017 group-
----------. 1 root root 822 Mar 9 2017 gshadow-
drwxr-x---. 5 root root 4096 Mar 9 2017 firewalld
drwxr-xr-x. 2 root root 21 Mar 9 2017 fcoe
drwxr-xr-x. 8 root root 4096 Mar 9 2017 NetworkManager
drwxr-xr-x. 2 root root 26 Mar 9 2017 gssproxy
drwx--x--x. 3 sssd sssd 19 Mar 9 2017 sssd
-rw-r--r--. 1 root root 2473 Mar 9 2017 passwd-
----------. 1 root root 1356 Mar 9 2017 shadow-
drwxr-xr-x. 2 root root 62 Mar 9 2017 sasl2
drwxr-xr-x. 2 root root 32 Mar 9 2017 setroubleshoot
drwxr-xr-x. 5 root root 76 Mar 9 2017 selinux
drwxr-xr-x. 2 root root 4096 Mar 9 2017 rpm
drwxr-xr-x. 3 root root 97 Mar 9 2017 abrt
-rw-r--r--. 1 root root 6920 Mar 9 2017 kdump.conf
drwxr-xr-x. 3 root root 49 Mar 9 2017 ntp
drwxr-xr-x. 2 root root 49 Mar 9 2017 alsa
drwxr-xr-x. 2 root root 23 Mar 9 2017 hp
drwxr-xr-x. 2 root root 28 Mar 9 2017 certmonger
drwxr-x---. 3 root root 102 Mar 9 2017 audit
drwxr-x---. 3 root root 41 Mar 9 2017 audisp
drwxr-xr-x. 2 root root 68 Mar 9 2017 oddjobd.conf.d
drwxr-xr-x. 2 root root 54 Mar 9 2017 rdma
drwxr-xr-x. 2 root root 25 Mar 9 2017 geoclue
drwxr-xr-x. 2 root root 101 Mar 9 2017 prelink.conf.d
drwxr-xr-x. 6 root root 94 Mar 9 2017 lvm
drwxr-xr-x. 2 root root 32 Mar 9 2017 wpa_supplicant
drwxr-xr-x. 3 root root 41 Mar 9 2017 lsm
drwxr-xr-x. 2 root root 65 Mar 9 2017 unbound
drwxr-xr-x. 2 root root 27 Mar 9 2017 sysctl.d
drwxr-xr-x. 3 root root 4096 Mar 9 2017 ppp
drwxr-xr-x. 2 root root 50 Mar 9 2017 iscsi
drwxr-xr-x. 2 root root 58 Mar 9 2017 samba
drwxr-xr-x. 2 root root 24 Mar 9 2017 UPower
drwxr-xr-x. 4 root root 74 Mar 9 2017 dbus-1
-rw-r--r--. 1 root root 1732 Mar 9 2017 nsswitch.conf
drwxr-xr-x. 4 root root 4096 Mar 9 2017 systemd
drwxr-xr-x. 10 root root 4096 Mar 9 2017 rc.d
lrwxrwxrwx. 1 root root 13 Mar 9 2017 rc.local -> rc.d/rc.local
drwxr-xr-x. 2 root root 22 Mar 9 2017 depmod.d
drwxr-xr-x. 6 root root 4096 Mar 9 2017 libreport
drwxr-xr-x. 3 root root 34 Mar 9 2017 openldap
drwxr-xr-x. 2 root root 22 Mar 9 2017 purple
drwxr-xr-x. 3 root root 59 Mar 9 2017 java
drwxr-xr-x. 3 root root 18 Mar 9 2017 ipa
lrwxrwxrwx. 1 root root 56 Mar 9 2017 favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
drwxr-xr-x. 6 root root 4096 Mar 9 2017 security
drwxr-xr-x. 2 root root 34 Mar 9 2017 python
drwxr-xr-x. 3 root root 36 Mar 9 2017 fonts
drwxr-xr-x. 2 root root 18 Mar 9 2017 ssl
drwxr-xr-x. 2 root root 4096 Mar 9 2017 iproute2
drwxr-xr-x. 2 root root 33 Mar 9 2017 libnl
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc6.d -> rc.d/rc6.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc2.d -> rc.d/rc2.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc4.d -> rc.d/rc4.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc0.d -> rc.d/rc0.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc1.d -> rc.d/rc1.d
lrwxrwxrwx. 1 root root 11 Mar 9 2017 init.d -> rc.d/init.d
drwxr-xr-x. 3 root root 74 Mar 9 2017 skel
lrwxrwxrwx. 1 root root 14 Mar 9 2017 system-release -> centos-release
lrwxrwxrwx. 1 root root 14 Mar 9 2017 redhat-release -> centos-release
drwxr-xr-x. 3 root root 17 Mar 3 2017 ghostscript
drwxr-xr-x. 2 root root 6 Mar 3 2017 exports.d
-rw-r--r--. 1 root root 3390 Mar 3 2017 nfsmount.conf
drwxr-xr-x. 2 root root 6 Mar 3 2017 binfmt.d
drwxr-xr-x. 2 root root 6 Mar 3 2017 modules-load.d
drwxr-xr-x. 5 root root 69 Mar 3 2017 polkit-1
-rw-r--r--. 1 root root 478 Mar 3 2017 ksmtuned.conf
-rw-------. 1 root root 541 Mar 31 2016 anacrontab
-rw-------. 1 root root 0 Mar 31 2016 cron.deny
-rw-r-----. 1 root named 1705 Mar 22 2016 named.conf
drwxr-xr-x. 3 root root 20 Mar 6 2015 pkcs11
drwxr-xr-x. 2 root root 6 Mar 6 2015 oddjob
-rw-r--r--. 1 root root 4922 Mar 6 2015 oddjobd.conf
 
Old 04-15-2020, 05:59 PM   #7
berndbausch
LQ Addict
 
Registered: Nov 2013
Location: Tokyo
Distribution: Mostly Ubuntu and Centos
Posts: 6,316

Rep: Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002Reputation: 2002
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?
 
Old 04-15-2020, 06:03 PM   #8
Mechanikx
Member
 
Registered: Jul 2018
Distribution: Slackware
Posts: 351

Rep: Reputation: 258Reputation: 258Reputation: 258
Quote:
Originally Posted by robert_garcia718 View Post

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.
 
1 members found this post helpful.
Old 04-15-2020, 06:04 PM   #9
robert_garcia718
LQ Newbie
 
Registered: Apr 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-15-2020, 06:09 PM   #10
Dman58
Member
 
Registered: Nov 2010
Location: The Danger Zone
Distribution: Slackware & everything else in a VM
Posts: 294

Rep: Reputation: 31
Seems like those should work. Worked for me. I didn't use quotes
Code:
ls -l /etc | grep Mar
Lists the following:
-r--r----- 1 root root 3174 Mar 25 2016 sudoers
drwxr-xr-x 2 root root 4096 Mar 15 14:55 sudoers.d

So what are your file redirection commands? You didn't list that. The next step would be piping the command for file redirection.
 
2 members found this post helpful.
Old 04-15-2020, 06:13 PM   #11
TB0ne
LQ Guru
 
Registered: Jul 2003
Location: Birmingham, Alabama
Distribution: SuSE, RedHat, Slack,CentOS
Posts: 26,634

Rep: Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965Reputation: 7965
Quote:
Originally Posted by robert_garcia718 View Post
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.
 
1 members found this post helpful.
Old 04-15-2020, 06:14 PM   #12
robert_garcia718
LQ Newbie
 
Registered: Apr 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
Running ls -l /etc | grep Mar gets me this:

[demo@host-2-65 ~]$ ls -l /etc | grep Mar
drwxr-xr-x. 3 root root 97 Mar 9 2017 abrt
-rw-r--r--. 1 root root 12288 Mar 9 2017 aliases.db
drwxr-xr-x. 2 root root 49 Mar 9 2017 alsa
drwxr-xr-x. 2 root root 4096 Mar 9 2017 alternatives
-rw-------. 1 root root 541 Mar 31 2016 anacrontab
drwxr-xr-x. 2 root root 31 Mar 9 2017 at-spi2
drwxr-x---. 3 root root 41 Mar 9 2017 audisp
drwxr-x---. 3 root root 102 Mar 9 2017 audit
drwxr-xr-x. 4 root root 94 Mar 9 2017 avahi
drwxr-xr-x. 2 root root 4096 Mar 9 2017 bash_completion.d
drwxr-xr-x. 2 root root 6 Mar 3 2017 binfmt.d
drwxr-xr-x. 2 root root 22 Mar 9 2017 bluetooth
drwxr-xr-x. 2 root root 12288 Mar 9 2017 brltty
drwxr-xr-x. 2 root root 28 Mar 9 2017 certmonger
drwxr-xr-x. 2 root root 25 Mar 9 2017 cifs-utils
drwxr-xr-x. 2 root root 51 Mar 9 2017 cron.d
drwxr-xr-x. 2 root root 54 Mar 9 2017 cron.daily
-rw-------. 1 root root 0 Mar 31 2016 cron.deny
drwxr-xr-x. 2 root root 21 Mar 9 2017 cron.hourly
drwxr-xr-x. 5 root lp 4096 Mar 9 2017 cups
drwxr-xr-x. 2 root root 33 Mar 9 2017 cupshelpers
drwxr-xr-x. 4 root root 74 Mar 9 2017 dbus-1
drwxr-xr-x. 2 root root 41 Mar 9 2017 default
drwxr-xr-x. 2 root root 22 Mar 9 2017 depmod.d
drwxr-xr-x. 2 root root 6 Mar 3 2017 exports.d
lrwxrwxrwx. 1 root root 56 Mar 9 2017 favicon.png -> /usr/share/icons/hicolor/16x16/apps/fedora-logo-icon.png
drwxr-xr-x. 2 root root 21 Mar 9 2017 fcoe
drwxr-xr-x. 3 root root 17 Mar 9 2017 firefox
drwxr-x---. 5 root root 4096 Mar 9 2017 firewalld
drwxr-xr-x. 3 root root 36 Mar 9 2017 fonts
drwxr-xr-x. 6 root root 101 Mar 9 2017 gdm
drwxr-xr-x. 2 root root 25 Mar 9 2017 geoclue
drwxr-xr-x. 3 root root 17 Mar 3 2017 ghostscript
drwxr-xr-x. 2 root root 6 Mar 9 2017 glusterfs
-rw-r--r--. 1 root root 1016 Mar 9 2017 group-
lrwxrwxrwx. 1 root root 22 Mar 9 2017 grub2.cfg -> ../boot/grub2/grub.cfg
drwx------. 2 root root 4096 Mar 9 2017 grub.d
----------. 1 root root 822 Mar 9 2017 gshadow-
drwxr-xr-x. 3 root root 19 Mar 9 2017 gss
drwxr-xr-x. 2 root root 26 Mar 9 2017 gssproxy
drwxr-xr-x. 2 root root 6 Mar 9 2017 gtk-2.0
-rw-r--r--. 1 root root 813 Mar 9 2017 hba.conf
drwxr-xr-x. 2 root root 23 Mar 9 2017 hp
drwxr-xr-x. 5 root root 86 Mar 9 2017 httpd
lrwxrwxrwx. 1 root root 11 Mar 9 2017 init.d -> rc.d/init.d
drwxr-xr-x. 3 root root 18 Mar 9 2017 ipa
drwxr-xr-x. 2 root root 4096 Mar 9 2017 iproute2
drwx------. 3 root root 93 Mar 9 2017 ipsec.d
drwxr-xr-x. 2 root root 50 Mar 9 2017 iscsi
drwxr-xr-x. 3 root root 59 Mar 9 2017 java
-rw-r--r--. 1 root root 6920 Mar 9 2017 kdump.conf
-rw-r--r--. 1 root root 478 Mar 3 2017 ksmtuned.conf
-rw-r--r--. 1 root root 78366 Mar 9 2017 ld.so.cache
drwxr-xr-x. 2 root root 4096 Mar 9 2017 ld.so.conf.d
drwxr-xr-x. 2 root root 33 Mar 9 2017 libnl
drwxr-xr-x. 6 root root 4096 Mar 9 2017 libreport
drwx------. 4 root root 4096 Mar 9 2017 libvirt
drwxr-xr-x. 2 root root 4096 Mar 9 2017 logrotate.d
drwxr-xr-x. 3 root root 41 Mar 9 2017 lsm
drwxr-xr-x. 6 root root 94 Mar 9 2017 lvm
drwxr-xr-x. 2 root root 84 Mar 9 2017 modprobe.d
drwxr-xr-x. 2 root root 6 Mar 3 2017 modules-load.d
drwxr-xr-x. 2 root root 64 Mar 9 2017 my.cnf.d
-rw-r-----. 1 root named 1705 Mar 22 2016 named.conf
drwxr-xr-x. 8 root root 4096 Mar 9 2017 NetworkManager
-rw-r--r--. 1 root root 3390 Mar 3 2017 nfsmount.conf
-rw-r--r--. 1 root root 1732 Mar 9 2017 nsswitch.conf
drwxr-xr-x. 3 root root 49 Mar 9 2017 ntp
drwxr-xr-x. 2 root root 6 Mar 6 2015 oddjob
-rw-r--r--. 1 root root 4922 Mar 6 2015 oddjobd.conf
drwxr-xr-x. 2 root root 68 Mar 9 2017 oddjobd.conf.d
drwxr-xr-x. 3 root root 34 Mar 9 2017 openldap
drwxr-xr-x. 2 root root 88 Mar 9 2017 PackageKit
drwxr-xr-x. 2 root root 4096 Mar 9 2017 pam.d
-rw-r--r--. 1 root root 2473 Mar 9 2017 passwd-
drwxr-xr-x. 3 root root 20 Mar 6 2015 pkcs11
drwxr-xr-x. 5 root root 69 Mar 3 2017 polkit-1
drwxr-xr-x. 3 root root 4096 Mar 9 2017 ppp
drwxr-xr-x. 2 root root 101 Mar 9 2017 prelink.conf.d
drwxr-xr-x. 2 root root 4096 Mar 9 2017 profile.d
drwxr-xr-x. 2 root root 75 Mar 9 2017 pulse
drwxr-xr-x. 2 root root 22 Mar 9 2017 purple
drwxr-xr-x. 2 root root 34 Mar 9 2017 python
drwxr-xr-x. 3 root root 48 Mar 9 2017 qemu-ga
drwxr-xr-x. 2 root root 49 Mar 9 2017 qemu-kvm
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc0.d -> rc.d/rc0.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc1.d -> rc.d/rc1.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc2.d -> rc.d/rc2.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc3.d -> rc.d/rc3.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc4.d -> rc.d/rc4.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc5.d -> rc.d/rc5.d
lrwxrwxrwx. 1 root root 10 Mar 9 2017 rc6.d -> rc.d/rc6.d
drwxr-xr-x. 10 root root 4096 Mar 9 2017 rc.d
lrwxrwxrwx. 1 root root 13 Mar 9 2017 rc.local -> rc.d/rc.local
drwxr-xr-x. 2 root root 54 Mar 9 2017 rdma
lrwxrwxrwx. 1 root root 14 Mar 9 2017 redhat-release -> centos-release
drwxr-xr-x. 2 root root 74 Mar 9 2017 request-key.d
drwxr-xr-x. 2 root root 4096 Mar 9 2017 rpm
drwxr-xr-x. 2 root root 24 Mar 9 2017 rsyslog.d
drwxr-xr-x. 2 root root 45 Mar 9 2017 rwtab.d
drwxr-xr-x. 2 root root 58 Mar 9 2017 samba
drwxr-xr-x. 2 root root 62 Mar 9 2017 sasl2
drwxr-xr-x. 3 root root 32 Mar 9 2017 scl
drwxr-xr-x. 6 root root 4096 Mar 9 2017 security
drwxr-xr-x. 5 root root 76 Mar 9 2017 selinux
drwxr-xr-x. 2 root root 32 Mar 9 2017 setroubleshoot
----------. 1 root root 1356 Mar 9 2017 shadow-
drwxr-xr-x. 3 root root 74 Mar 9 2017 skel
drwxr-xr-x. 3 root root 71 Mar 9 2017 smartmontools
drwxr-xr-x. 2 root root 4096 Mar 9 2017 ssh
drwxr-xr-x. 2 root root 18 Mar 9 2017 ssl
drwx--x--x. 3 sssd sssd 19 Mar 9 2017 sssd
drwxr-xr-x. 2 root root 27 Mar 9 2017 sysctl.d
drwxr-xr-x. 4 root root 4096 Mar 9 2017 systemd
lrwxrwxrwx. 1 root root 14 Mar 9 2017 system-release -> centos-release
drwxr-xr-x. 2 root root 6 Mar 9 2017 tmpfiles.d
drwxr-xr-x. 2 root root 67 Mar 9 2017 tuned
drwxr-xr-x. 3 root root 51 Mar 9 2017 udev
drwxr-xr-x. 2 root root 65 Mar 9 2017 unbound
drwxr-xr-x. 2 root root 24 Mar 9 2017 UPower
drwxr-xr-x. 4 root root 4096 Mar 9 2017 vmware-tools
drwxr-xr-x. 2 root root 32 Mar 9 2017 wpa_supplicant
drwxr-xr-x. 6 root root 97 Mar 9 2017 X11
drwxr-xr-x. 6 root root 95 Mar 9 2017 yum


Which is what the linuxzoo.net tutorial is looking for. Thanks!
 
Old 04-15-2020, 06:38 PM   #13
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 2,791

Rep: Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201Reputation: 1201
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.
 
Old 04-15-2020, 06:55 PM   #14
robert_garcia718
LQ Newbie
 
Registered: Apr 2020
Posts: 6

Original Poster
Rep: Reputation: Disabled
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.
 
Old 04-15-2020, 07:44 PM   #15
scasey
LQ Veteran
 
Registered: Feb 2013
Location: Tucson, AZ, USA
Distribution: CentOS 7.9.2009
Posts: 5,727

Rep: Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211Reputation: 2211
You need to study the sort man page. Your syntax on the sort is not correct. You’re sorting on the group and the year
 
  


Reply



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
[SOLVED] trouble piping tail -f, grep and cut. disciple218 Linux - Software 7 11-12-2013 06:35 AM
question about piping grep into sed into xargs for geektool weather script alyda Linux - General 5 02-12-2013 03:11 AM
Creating an alias in ksh that uses grep and includes 'grep -v grep' doug248 Linux - Newbie 2 08-05-2012 02:07 PM
Linux piping vs Windows piping emanabela Linux - Newbie 1 05-10-2009 12:58 PM
bash script with grep and sed: sed getting filenames from grep odysseus.lost Programming 1 07-17-2006 11:36 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:40 AM.

Main Menu
Advertisement
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
Open Source Consulting | Domain Registration