Xargs doesn’t use the “ls” alias

aixaliashp-uxxargs

On AIX (but this happens on HP-UX as well), I have GNU ls in my path
and it is also aliased as ls.

When I use xargs, it instead uses the standard Unix ls instead of the
alias.

For example (flocate is a function which finds the exact path of
the search subject):

flocate mirrorvg | xargs ls -lh
ls: illegal option -- h
usage: ls [-1ACFHLNRSabcdefgiklmnopqrstuxEUX] [File...]

ls -lh /usr/sbin/mirrorvg
-r-xr-x--- 1 root system 37K apr  3  2014 /usr/sbin/mirrorvg*

Why doesn't xargs use the ls alias?

Best Answer

The command xargs is only able to run commands, not aliases. GNU parallel, however, is able to run functions:

The command must be an executable, a script, a composed
command, or a function. If it is a function you need to export
-f the function first. An alias will, however, not work (see
why http://www.perlmonks.org/index.pl?node_id=484296).

So I would recommend either:

  • Giving xargs the full path to the version of ls you want to use (or an unambiguous name, perhaps gls depending on how it was installed on your system) or, if your shell allows it,

  • Defining ls as a function (function ls { gls "$@"; }; export -f ls in bash) and using GNU parallel instead of xargs (parallel -j1 if you would like to use a single CPU).

Related Question