If you consider the following file :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads
drwxrwxr-- 5 morins morins 4096 Oct 7 16:40 kdenlive
drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos
-rw-rw-r-- 1 morins morins 337597 Oct 7 16:53 out.ogv
drwxrwxr-- 2 morins morins 4096 Oct 7 20:19 Documents
-rw-r--r-- 1 morins morins 447 Dec 8 19:30 color.txt
-rwxrwxr-- 1 morins morins 408 Dec 8 19:30 color.sh
-rwxrwxr-- 1 morins morins 624 Dec 8 19:30 color4.sh
-rwxrwxr-- 1 morins morins 623 Dec 8 19:30 color3.sh
-rwxrwxr-- 1 morins morins 1126 Dec 8 19:30 color2.sh
-rwxrwxr-- 1 morins morins 3076 Dec 8 19:30 test2
-rwxrwxr-- 1 morins morins 662 Dec 8 19:30 test
-rwxrwxr-- 1 morins morins 72 Dec 8 19:30 scripts.sh
-rwxrwxr-- 1 morins morins 68 Dec 8 19:30 quotes.sh
-rw-r--r-- 1 morins morins 88 Dec 8 19:30 name
-rwxrwxr-- 1 morins morins 134 Dec 8 19:30 format.sh
-rw-r--r-- 1 morins morins 8980 Dec 8 19:30 examples.desktop
-rw-r--r-- 1 morins morins 229 Dec 8 19:30 test.txt
-rw-r--r-- 1 morins morins 229 Dec 8 19:30 test3.txt
-rwxrwxr-- 1 morins morins 4373 Dec 8 19:30 test3
-rw-r--r-- 1 morins morins 229 Dec 8 19:30 test2.txt
-rwx------ 1 morins morins 113 Dec 8 19:55 t
-rwx------ 1 morins morins 228 Dec 30 22:57 test_zenity.sh

You do the command :

1

ls –ltr | grep d
you will get  :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads
drwxrwxr-- 5 morins morins 4096 Oct 7 16:40 kdenlive
drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos
drwxrwxr-- 2 morins morins 4096 Oct 7 20:19 Documents
-rw-r--r-- 1 morins morins 8980 Dec 8 19:30 examples.desktop

Ok you get all the folder , which is what you want , but you also get the file examples.desktop which you don’t want.
So instead you will do :

2

ls –ltr | grep “^d”
you get  :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads
drwxrwxr-- 5 morins morins 4096 Oct 7 16:40 kdenlive
drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos
drwxrwxr-- 2 morins morins 4096 Oct 7 20:19 Documents

The (carret) ^ indicate the beginning of the file , so it means every lines that start with letter ‘d’
Now if you want let say to have all the file that end with an ‘s’.
You will do the grep regex command :

3

ls –ltr | grep “s$”
and you get  :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures

drwxrwxr-- 6 morins morins 4096 Oct 12 2015 Downloads

drwxrwxr-- 2 morins morins 4096 Oct 25 2015 Videos

drwxrwxr-x 2 morins morins 4096 Nov 1 2015 scripts

drwxrwxr-- 2 morins morins 4096 Mar 6 21:11 Documents

It means all lines that contain ‘s’ immediately folowed by the end of the line.
Now let say you want to select all lines with a folder of month feb.
You could do :

4

s –ltr | grep “^d.*Feb”
you would obtain  :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads

That means , give me the lines that start with a ‘d’ , have any charactere many time, folow by ‘Feb’ , don’t forget the ‘F’ in caps is essential , because linux is case sensitive.
Here are some basic symbol definition :
Symbol
Definition
^
Beginning of the line
$
End of the line

.

Any charactere
*
One or many time the preceding charactere or symbol
[]
Selection of possible charactere
you can also do  :

5

ls –ltr | grep “[FND]eb”
the result is  :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Desktop
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Templates
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Public
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Music
drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Downloads

Like that you will get any lines that contain either Feb, Neb or Deb , but as in our list only the folder correspond to that and there is no lines with either Neb or Deb we got the same lines as per the preceding example.
For example if you do  :

6

ls –ltr | grep “[PV]i”
you get :

drwxrwxr-- 2 morins morins 4096 Feb 21 2014 Pictures

drwxrwxr-- 2 morins morins 4096 Oct 7 16:42 Videos

Because it’s the only lines that contain either ‘Pi’ or ‘Vi”.

About the Author

Stephane Morin

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

Sign up for more great content!

>