Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

If I do:

$ ls -R
.:
4Shared/  Cloud/

./4Shared:
UFAIZLV2R7.part3.rar

./Cloud:
UFAIZLV2R7.part2.rar.part
UFAIZLV2R7.part1.rar.part
UFAIZLV2R7.part4.rar.part

If I want to list the .rar files only, and I use grep, it will show me too the .rar.part files, what is not my wish.
I am solving this using find or ls **/*.rar as told in this thread and they work fine, but I would like to learn if it is possible to do it via grep.

I have tried (thinking about EOL):

ls -R | grep ".rar\n"

with no results.
I think that the problem lies in discover if the greping is found at the end of the line, but I am not sure.

Any help out here, please?

share|improve this question
    
Why would you want to use grep in this case? Why not find? –  devnull Apr 13 at 9:31
    
@devnull, knowing how to detect patterns that are only at the end of a line could be useful in many cases. For example: portability, usage in routers with embedded too-simplistic Linux, usage with UnxUtils for Windows (its find command conflicts with the Windows one) and... learning ;-) . The question is not about "Listing Files in a dir" (that is just a custom example) but rather "Understanding the grep command usage" –  Sopalajo de Arrierez Apr 13 at 12:09
1  
honestly, if you're counting on the end of a line to be your separator, then you should be using ls -1R. –  mikeserv Apr 13 at 14:48
1  
And you don't even need grep. See my answer. –  mikeserv Apr 13 at 15:02
    
@mikeserv, what could happen without the -1R switch. Apparently the results are the same. –  Sopalajo de Arrierez Apr 14 at 1:01

4 Answers 4

The $ anchor matches the end of a line.

ls -R | grep '\.rar$'

You can also use find for this:

find . -name '*.rar'
share|improve this answer

In addition to your question please note that .rar does not only match ".rar" but matches every single character (including .) before the rar. In this case probably not a problem but . must be escaped in regexes.

ls -R | grep "\.rar$"
share|improve this answer
    
Did you mean that .asc is like *asc pattern? So it will match, for example, whereverasc. –  Sopalajo de Arrierez Apr 13 at 0:23
2  
@SopalajodeArrierez No, it does not match every group of characters but every single character, e.g. xrar. –  Hauke Laging Apr 13 at 0:28

You can also instruct grep to look for your string starting at a word boundary. A . is one of these such boundaries.

$ ls -R | grep '\brar$'

Example

Say I have this sample data.

$ ls -1
afile.rar
xrar
UFAIZLV2R7.part1.rar.part
UFAIZLV2R7.part2.rar.part

This command would find only the file with the .rar extension.

$ ls -R | grep '\brar$'
afile.rar

How this works?

The metacharacter \b is an anchor like the caret and the dollar sign. It matches at a position that is called a "word boundary". This match is zero-length.

Situations where this won't work

If you have files that are named blah-rar these will get detected as well.

$ ls -R | grep '\brar$'
afile-rar
afile.rar

That's because characters other than a alphanumerics are typically considered boundary characters, and so would slip past this approach.

share|improve this answer
    
Seems to be the same at first sight, but it is slightly different, indeed. Thanks, @slm. Does it bother if I use double quotes " instead of simple quotes? –  Sopalajo de Arrierez Apr 13 at 1:24
1  
@SopalajodeArrierez - nope works either way. This will find any files that may be named starting w/ .rar. But these won't be an issue with the use of ls -R. Only if you happened to use ls -Ra. –  slm Apr 13 at 1:26
    
Would one of you be interested in explaining this slight difference to the public? –  Hauke Laging Apr 13 at 14:02
    
@HaukeLaging The -P switch to grep in my example. That triggers PCRE interpretation of the argument. –  slm Apr 13 at 14:09
1  
@SopalajodeArrierez - if a file contains a newline character (\n) which is a legal character. The ls -1R will force the files to be displayed in a single column regardless. –  slm Apr 14 at 1:43

Just do :

ls -1R -I"?" -I"??" -I"???" -I"*[!.][!r][!a][!r]"

You don't need grep at all.

NOTE: The above works... except it still gets at least afile-rar and I don't understand why. I'll leave it here, but I'm not proud of it. In any case, as others have said:

find . '*.rar'
share|improve this answer
    
This didn't block files named xrar or afile-rar. –  slm Apr 13 at 15:27
    
@slm - fixed - thank you. –  mikeserv Apr 13 at 15:34
    
I'm still getting the other files in the output. –  slm Apr 13 at 15:36
    
@slm What other files? I think it might be cause they're too short. I just noticed that myself. I fixed that too. –  mikeserv Apr 13 at 15:45
    
The files afile-rar and xrar are still being included in the output. No change with your latest mods either. Pesky problem no? It's fun trying to solve it without the regular methods 8-) –  slm Apr 13 at 16:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.