Solved

Piping output into a 2nd command

Posted on 2004-04-06
8
423 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
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
what does getent do 3 42
linux / python expert needed 3 79
Linux: disable vim auto-comment 7 64
Problem to start Neon 20 54
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 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…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

760 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

21 Experts available now in Live!

Get 1:1 Help Now