The command cd
is a built-in because the information about the current directory is tied to a process and only shell built-in can change current directory of the running shell.
There are two problems with your code:
xargs
cannot runcd
becausecd
is a built-in command andxargs
can run only executable files.- Even if you run
cd
in a sub-process called fromxargs
, it will not have any effect on the parent process as explained above.
The solution is to run a sub-shell, inside it run cd
and then you can execute commands in the new current directory.
ls | xargs -L 1 bash -c 'cd "$0" && pwd && ls'