Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

I used to run commands like: find /dev | grep sd* to output a list of all the drives attached to my computer. It appears that I now have to add any random character before the star in order to get the same affect as shown below:

sudo find /dev | grep sdp*

I tested this on various Ubuntu 12.04 servers and got the same effect, so I do not think this is just something random on my desktop. Is this a new change, a bug, or has it always been this way and my memory needs replacing?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

You have two problems:

  • Globbing interferes with your regexp
  • Your regexp doesn't match what is should match

Uncovering the globbing mechanism

sudo find /dev | grep sd* doesn't work as before because you have a file or a folder in your current working directory which triggers the globbing from bash. Add a echo in front of your command to see what globbing does to your command

echo grep sd*

By adding a random additional character globbing didn't match the file/folder anymore continued to work as expected.

How to prevent globbing

To prevent globbing from kicking in, you have to put '...' around your term. This will make grep behave as before:

sudo find /dev | grep 'sd*'

How regexp work

Now that the regexp is working as expected let's look at what * means. In short it means "This last character zero or n-times" and this is most probably not what you want. In your case a simple

sudo find /dev | grep sd

should be all you need to match.

share|improve this answer
    
find /dev | grep "sd*" resulted in the same effect, whilst echo find /dev | grep sd* output nothing: img811.imageshack.us/img811/2451/g7a.png –  Programster Nov 20 '13 at 12:31
    
Added more sudoness and suggested another solution. –  MadMike Nov 20 '13 at 12:49
    
nvm, Im pretty sure im getting two commands I use mixed up: ls /dev/sd* and find /dev | grep sd Either of these work as I was expecting. –  Programster Nov 20 '13 at 12:49
2  
Some people, when confronted with a problem, think “I know, I'll use regular expressions.” Now they have two problems. -- Quote –  MadMike Nov 20 '13 at 12:52

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.