Feeling like an idiot right now. Why does this not work?
echo "/some/directory/path" | xargs -n1 cd
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.Sign up to join this community
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:
cdis a built-in command and
xargscan run only executable files.
cdin a sub-process called from
xargs, 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'