-3

I read that if you use the | command then it pipes the output of the first command to the input of the second,then why is it working?

Thank you!

find -size 1033c | cd
ido91198
  • 87
  • 5
  • 5
    because `cd` ignores stdin – oguz ismail Aug 25 '19 at 05:16
  • 1
    This would only work if the second command would read from stdin, which is not the case in this case, and if the second command would not be executed in a separate environment. – Cyrus Aug 25 '19 at 05:16
  • 1
    Possible duplicate of [Use output of bash command (with pipe) as a parameter for another command](https://stackoverflow.com/questions/9635705/use-output-of-bash-command-with-pipe-as-a-parameter-for-another-command) – oguz ismail Aug 25 '19 at 05:17
  • 1
    if you want to operate on all the files retirieved by 'find' then use `find -size 1033c -exec cd {} \;` – Krishnom Aug 25 '19 at 05:28
  • 3
    @Krishnom - No that won't work. That will change the directory in a child process ... which will immediately exit! – Stephen C Aug 25 '19 at 05:51
  • 1
    Standard input and arguments are two very different things. – chepner Aug 25 '19 at 15:00

1 Answers1

5

The cd command doesn't read standard input, so anything that you pipe to it will be ignored.

The closest to what your command is (literally) trying to do will be this:

cd `find -size 1033c`

... except that you are liable to 'cd' to a file (which will fail!) or not find a directory with that size ... leading to you (silently) cd-ing to the user's home directory.

And also your find command is missing a directory to search!


Based on your comments, you are trying to cd to the directory containing a file with a given size. If we can assume that there will only ever be one such file, then the following should work:

FILE=`find . -size 1033c -type f` 
cd `dirname $FILE`

If there could be more than one match, then you need to do something like this ... which should cd to the directory with the first matching file.

FILE=`find . -size 1033c -type f -print -quit` 
cd `dirname $FILE`
Stephen C
  • 698,415
  • 94
  • 811
  • 1,216