Solved

Piping output into a 2nd command

Posted on 2004-04-06
8
430 Views
Last Modified: 2010-04-22
Gentlemen:

I'm trying to pipe the output of a find command into a 2nd command, which is to say that I want to cd over to the directory I already found with my find command, something like this:

find / -name mydir1 | cd $1

Does this work?  Do I need to use a different variable substitution?  What is the variable that indicates the piped in input?  Also, What if I'm looking for an individual file, and I want to cd to it's parent directory, like this:

find / -name myfile1 | cd $1

I know that won't work, since it's sending a file, not a directory to cd, but how do I get only the file?  Must I grep it out?
0
Comment
Question by:SuperKarateMonkey
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 3
8 Comments
 
LVL 12

Expert Comment

by:stefan73
ID: 10766910
Hi SuperKarateMonkey,
The problem is that you *could* find several of those.

If you're sure you won't, you could do:
cd `find / -name mydir1`


Cheers,
Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10766950
SuperKarateMonkey,
> Does this work?

No. What you can do with "normal" commands is for example:
find / -name mydir1 | xargs ls -ltr

...but cd isn't a "normal" command: It cannot be external (that is being a separate binary that runs in a separate process), because each process has its own working directory and changeing the working directory first and then exiting the new process will not change the directory of the calling process (the shell).

The same happens when you do cd in a subshell:

(cd some_dir ; ls -l)

This will also not modify the original shells working directory. Sometimes this "subshell cd" comes in handy, as here:

tar -cf - | (cd /tmp; tar -xf - )

...but in your case it won't.

Stefan
0
 
LVL 12

Expert Comment

by:stefan73
ID: 10766975
SuperKarateMonkey,
> What is the variable that indicates the piped in input?
It's not a variable, it's the standard output of the process on the left side of the pipe which becomes the standard input of the process on the right side of the pipe. If you want to set variables, you'd do:

var=`some_command`
echo $var | some_other_command

...but you'd better use...

some_command | some_other_command

...unless you did some modifications on $var inbetween.

Stefan
0
Quiz: What Do These Organizations Have In Common?

Hint: Their teams ended up taking quizzes, too.

 
LVL 12

Expert Comment

by:stefan73
ID: 10767000
SuperKarateMonkey,
> find / -name myfile1 | cd $1

For this, you need two levels of output converted to arguments (assuming you're using sh, bash or ksh):

cd $( dirname $( find / -name myfile1 ) )

Stefan
0
 
LVL 2

Author Comment

by:SuperKarateMonkey
ID: 10767005
I guess what I'm asking is if there's an environment variable to indicate the output of the previous command, or if I can use piping to create one.

As for cd 'find / -name mydir1'

That didn't work.  The shell pukes, reporting:

cd: find / -name mydir1: No suck file or directory
0
 
LVL 2

Author Comment

by:SuperKarateMonkey
ID: 10767059
Stefan:

You wrote:
cd $( dirname $( find / -name myfile1 ) )

YUP!  That's the stuff!  Just to make things clear, though, confirm I have these two postulates correct:

1.  Wrapping a command in () parentheses causes it to be executed as a unit?

2.  Putting a $ dollar sign ahead of said parentheses causes the command to be evaluated not by it's return value, (of usually 0 or 1,) but rather of it's output to standard out?

The reason I ask is that this is also working:

cd $(find / -name myfile1 -printf %h)
0
 
LVL 12

Accepted Solution

by:
stefan73 earned 300 total points
ID: 10767147
SuperKarateMonkey,
> 1.  Wrapping a command in () parentheses causes it to be executed as a unit?

Yes, it will create a new instance of the shell and execute the commands there. You can redirect the output of a sequence of commands this way:

( echo "This is dir_1:" ; ls -l dir_1; echo "dir_1 end" ) > dir_1.out
 


> 2.  Putting a $ dollar sign ahead of said parentheses causes the command
> to be evaluated not by it's return value, (of usually 0 or 1,) but
> rather of it's output to standard out?

Almost: It will use the inner command's output as the outer commands arguments, such as

ls -l $( echo one two three | cut -d' ' -f1,2 )

is equivalent to "ls -l one two"

...those mechanisms are also the reason why there shouldn't be spaces in file names, as they're splitting one argument into two (or more).

You can also use backticks:
echo `ls -l`
is the same as
echo $( ls -l )
...but you cannot nest backticks.

Stefan
0
 
LVL 2

Author Comment

by:SuperKarateMonkey
ID: 10767167
Ohhhhh....  That's why your first answer didn't work.  I was using 'single quotes,' not `back ticks.`

Okey.  That pretty much covers it.  Thanks very much for your help, stefan72.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

726 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question