0

I have a directory list of debs with different versions. My requirement is to find packages with matching version.

here is what i tried till now

ls debs/* | grep 1.1.16 | egrep -e 'Package1|Package2|package3'

This above command only gives me the o/p of Package1.

Tried all the options from here. But none of them helps.

1
  • What you have should most likely work. Can you show the actual filenames in the debs/ directory? – Inian Dec 26 '19 at 11:12
1

First, the regular warning about not parsing ls. That said, assuming your files are named sanely, you could do:

ls debs/*1.1.16* | grep -E 'Package1|Package2|package3'

The only difference here is that I'm avoiding the 1st grep, which isn't needed since you can directly pass a glob pattern to ls. Then, instead of the deprecated egrep, I'm using grep -E which is the same thing.

However, this is essentially the same as your command. If it didn't work, then you don't have matching files:

$ ls debs/
1.1.16.Package3  Package1.1.1.160  Package1.1.2.16  Package2.1.1.16
$ ls debs/*1.1.16* | grep -E 'Package1|Package2|package3'
debs/Package1.1.1.160
debs/Package2.1.1.16

As expected, that shows the two matching files. And this is exactly what your command would have printed as well:

$ ls debs/* | grep 1.1.16 | egrep -e 'Package1|Package2|package3'
debs/Package1.1.1.160
debs/Package2.1.1.16

Note that since the package3 is lowercase (p instead of P) in the grep pattern, the file 1.1.16.Package3 is not shown.

0

There is a way to do it without paring ls output.

version=1.1.16
for package in debs/*Package1*${version}* \
               debs/*Package2*${version}* \
               debs/*package3*${version}*;
do
  action with $package;
done

This way shell will expand the glob and iterate through matching files. In case no file matches provided patterns, the script will simply skip the loop without any error.

Or try find:

version=1.1.16
find debs/ \( -name "*Package1*${version}*" -o -name "*Package2*${version}*" -o -name "*package3*${version}*" \)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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