# problems with xargs and cat file containing *



## edhunter (Jan 28, 2011)

Hello
I am trying to automate some job using xargs, and i get strange result:

I have a file ~ed/temp/templist.txt containing single line (it will contain many, but for examle its only one): 

```
./images/baners/*
```

I execute the following command (in the right directory - where ./images/baners exists)
[CMD=]cat ~ed/temp/templist.txt | xargs -L 1 -t ls[/CMD]

I get this output:

```
ls ./images/baners/*
[red]ls: ./images/baners/*: No such file or directory[/red]
```

When I manualy execute [cmd=]ls ./images/baners/*[/cmd]
Then I get the expected result

```
-rw-rw-r--  1 pulsar  www   41988 Nov  9 11:45 ./images/baners/227.jpg
-rw-rw-r--  1 pulsar  www   42842 Nov 19 10:55 ./images/baners/229.jpg
-rw-rw-r--  1 pulsar  www   39850 Nov 19 10:55 ./images/baners/230.jpg
...
```

If I remove * from the templist.txt file everything works ok. But I need this * symbol. I use ls here only as example.
What I am doing wrong, why I dont get the correct result when I use xargs?

10x in advance


----------



## SirDice (Jan 28, 2011)

The asterisk doesn't get expanded by the shell. Normally the command:
`% ls /images/*`

Will expand the * first before executing the command. 
In your script it gets treated as the literal character * not as a shell glob.


----------



## edhunter (Jan 28, 2011)

Is there any way to make it work with asterix? Or I should avoid using * in scripts ?


----------



## SirDice (Jan 28, 2011)

Not sure if it'll work but you can try escaping it.

Best thing to do is to avoid it where possible.


----------



## edhunter (Jan 28, 2011)

Escaping doesnt work ... 10x, anyway i will avoid using it


----------



## phoenix (Jan 28, 2011)

Try the following.

```
eval $( cat somefile ) | xargs -whatever
```

"$( cat somefile )" will output /some/path/stuff*
eval reads that string, then parses it like a new command, so the shell will expand the *
Then it gets passed through the pipe to xargs.


----------



## jilles@ (Feb 1, 2011)

phoenix said:
			
		

> Try the following.
> 
> ```
> eval $( cat somefile ) | xargs -whatever
> ...



The eval command is probably not needed here as field splitting and pathname generation happens on any unquoted expansion in a context where splitting is performed (such as normal command words and for commands). It may be useful if you want ", ', \, $, ` to have their special meaning.

On the other hand, you need some sort of utility such as echo to output the pathnames rather than trying to execute the first image as a program.

Things to try:

If there are not very many pathnames, you do not need xargs:

```
program $(cat somefile)
```
If you get Argument list too long errors, you need something else.
All characters other than *, ?, [ and the separators (in IFS) can be in pathnames. (Be careful to add -- if the first pathname may start with a hyphen.)

If there may be very many pathnames, but they do not contain some special characters like backslashes:

```
echo $(cat somefile) | xargs program ...
```

If you need to be more general, like the first example:

```
printf '%s\0' $(cat somefile) | xargs -0 program ...
```


----------

