Expanding globs in xargs

54

Solution 1

This is old, but i ran into this issue and found another way to fix it. Have xargs execute bash with -c and your command. For example:

echo 'file otherfile' | tr ' ' '\n' | xargs -I % bash -c "ls %*.txt"

This forces glob expansion.

Solution 2

I was able to get the functionality I wanted by using the -1 flag for ls. Here is how my command looks:

ls -1 {file,otherfile}*.txt | xargs -I% echo mycommand %

Solution 3

What you are doing won't work because the glob is not expanded by the xargs command when it runs, it is expanded by the shell as soon as you hit enter and before any of those commands starts running. So the glob it tries to expand is:

%*.txt

and as there are no files matching that pattern it leaves it alone and then when xargs runs it substitutes it's arguments into that string.

The simplest solution is probably just to write a pattern which matches all the files you are interested in in one go:

ls {file,otherfile}*.txt
Share:
54

Related videos on Youtube

Apollo
Author by

Apollo

Updated on September 18, 2022

Comments

  • Apollo
    Apollo over 1 year

    I have a UIImageView that is the size of the screen. On an iPhone 5, the image size should be 640 x 1136 points (which is 2x the size of the screen in points). On iPhone 6, the size should be 750 x 1134 (again 2x) and on iPhone 6 Plus the size should be 1242 x 2208. I've created 3 different images, each corresponding to the correct size. In Xcode, I added the three images as part of the same asset in the hopes that at runtime the correct image will be chosen. Is this the right way to do this?

    enter image description here

    • Admin
      Admin almost 13 years
      I ended up using two xargs with find: echo 'file otherfile' | tr ' ' '\n' | xargs -I % find . -name "%*.txt" | xargs ls
  • Apollo
    Apollo almost 9 years
    could you explain the PDF method a little bit further?
  • Matteo Gobbi
    Matteo Gobbi almost 9 years
    of course: instead of create 3 different images, you provide just the 1x size but in PDF. Because the PDF format is vectorial, the system will automatically create 2x and 3x or any other format will be inserted from apple in the future. In the asset you can specify if the image is vectorial or not. Enjoy ;)