Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

What is the role of the $ in the code, ls . | grep '.txt$'

I am new to linux and im learning from a website, and this came up in the grep part of a course

i tried running the same code but without the $ symbol and it displays the same output, so im wondering what the role of the $ is

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

In grep manual (run man grep in the terminal), you may find the following documentation regarding this:

Anchoring
   The caret ^ and the dollar sign $ are meta-characters that respectively match the empty string at the beginning and end of a line.

The intention in the use case you saw is clearly to ensure the filtered .txt filenames don’t include extra characters at the end of the extension.
As you can see here, for example:

$ ls  
file1.txt  file2.txtz

$ ls . | grep '.txt'
file1.txt
file2.txtz

$ ls . | grep '.txt$'
file1.txt

Also, have a look at this.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading