Solved

Piping output into a 2nd command

Posted on 2004-04-06
8
428 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say 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

Suggested Solutions

Title # Comments Views Activity
Linux daemon 11 356
Xubuntu Remove Open Terminal Here Option from Desktop Right Click 7 323
usage of %{name} in rpm spec file 4 54
fsstat very high only on nfs client side rhel 10 107
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…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

733 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