Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

I have a directory like this

mkdir test
cd test
touch file{0,1}.txt otherfile{0,1}.txt stuff{0,1}.txt

I want to run some command such as ls on certain types of files in the directory and have the * (glob) expand to all possibilities for the filename.

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

This command does not expand the glob and tries to look for the literal 'file*.txt'

How do I write a similar command that expands the globs? (I want to use xargs so the command can be run in parallel)

share|improve this question
    
I ended up using two xargs with find: echo 'file otherfile' | tr ' ' '\n' | xargs -I % find . -name "%*.txt" | xargs ls –  Craig Jun 29 '11 at 23:40

3 Answers 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|improve this answer

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.

share|improve this answer

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 %
share|improve this answer

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.