?
Solved

Piping output into a 2nd command

Posted on 2004-04-06
8
Medium Priority
?
431 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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 1200 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

Visualize your virtual and backup environments

Create well-organized and polished visualizations of your virtual and backup environments when planning VMware vSphere, Microsoft Hyper-V or Veeam deployments. It helps you to gain better visibility and valuable business insights.

Question has a verified solution.

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

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…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 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