ic205
ic205

Reputation: 131

Why doesn't the cd command work when trying to pipe it with another command

I'm trying to use a pipeline with cd and ls like this:

ls -l | cd /home/user/someDir

but nothing happens.

And I tried:

cd /home/user/someDir | ls -l

It seems that the cd command does nothing while the ls -l will work on the current directory.

The directory that I'm trying to open is valid.

Why is that? Is it possible to pipe commands with cd / ls? Didn't have any problem with other commands while using pipe.

Upvotes: 2

Views: 3085

Answers (1)

Piskvor left the building
Piskvor left the building

Reputation: 92792

cd takes no input and produces no output; as such, it does not make sense to use it in pipes (which take output from the left command and pass it as input to the right command).

Are you looking for ls -l ; cd /somewhere?

Another option (if you need to list the target directory) is:

cd /somewhere && ls -l

The '&&' here will prevent executing the second command (ls -l) if the target directory does not exist.

Upvotes: 11

Related Questions