Solved

Piping output into a 2nd command

Posted on 2004-04-06
8
424 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
  • 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
 
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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
This is used to tweak the memory usage for your computer, it is used for servers more so than workstations but just be careful editing registry settings as it may cause irreversible results. I hold no responsibility for anything you do to the regist…
As a trusted technology advisor to your customers you are likely getting the daily question of, ‘should I put this in the cloud?’ As customer demands for cloud services increases, companies will see a shift from traditional buying patterns to new…

895 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now