LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Piping Output of a Command Like Find (https://www.linuxquestions.org/questions/linux-newbie-8/piping-output-of-a-command-like-find-747560/)

cipher7836 08-14-2009 10:58 AM

Piping Output of a Command Like Find
 
I want to search for a particular file. However, once that file is found I want a long listing of it to see it's permissions. If I do something like this: "find / -name whatever | ls -l" it just gives me the result of "ls -l". What am I doing wrong?

paulsm4 08-14-2009 11:02 AM

Here's one option:
Quote:

find . -name MYFILE -exec ls -l {} \;

pixellany 08-14-2009 12:26 PM

A pipe works only where the command is expecting an input. ls does not expect an input, so it ignores what you are sending it.

jschiwal 08-14-2009 01:05 PM

Another option is to use `xargs'. Since a name might contain white space, that needs to be taken into account.
find . -name FILENAME -print0 | xargs -0 ls -l

There is an "ls" command in find, so this would be easiest:
find . -name FILENAME -ls

Wim Sturkenboom 08-14-2009 11:48 PM

Quote:

Originally Posted by pixellany (Post 3643433)
A pipe works only where the command is expecting an input. ls does not expect an input, so it ignores what you are sending it.

ls can use input (options, specific directory, specific file) so I doubt this is the reason it does not work
grep definitely needs input and it will also not work when it receives data piped from the find command

What am I missing from your post?

pixellany 08-15-2009 02:02 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 3644020)
ls can use input (options, specific directory, specific file) so I doubt this is the reason it does not work
grep definitely needs input and it will also not work when it receives data piped from the find command

What am I missing from your post?

I don't think you missed anything---there is obviously something I don't understand.....:)

Are you saying you cannot pipe the output of find to **anything**?

If a command produces output, does it even know that it is being piped?

pixellany 08-15-2009 02:13 AM

Quote:

grep definitely needs input and it will also not work when it receives data piped from the find command
Works here......so does SED.

Trying to pipe something to ls: Tried echo <string>|ls---does not work.

I am beginning to think I might have been more right than wrong......

Wim Sturkenboom 08-15-2009 05:21 AM

Below an old issue that I encountered long ago with find. Somebody suggested paulsm4's solution (post #2) to overcome the problem. That is why I'm wondering what is going on after reading your post.
Code:

[user@localhost Documents]$ cat abc.txt
hallo
hello
[user@localhost Documents]$ find . -name abc*
./abc.txt
[user@localhost Documents]$ find . -name abc* |grep hello
[user@localhost Documents]$ grep hello *
abc.txt:hello
[user@localhost Documents]$

PS. Taken from a Linpus Lite machine

catkin 08-15-2009 06:53 AM

Quote:

Originally Posted by Wim Sturkenboom (Post 3644194)
Below an old issue that I encountered long ago with find.

What's the issue? It's working as I'd expect unless I am the stick wrong end having.

pixellany 08-15-2009 08:17 AM

Code:

[user@localhost Documents]$ find . -name abc* |grep hello
grep is working on file names.

----
Code:

[user@localhost Documents]$ grep hello *
abc.txt:hello

grep is working on file contents.

pixellany 08-15-2009 08:20 AM

Quote:

Originally Posted by catkin (Post 3644265)
unless I am the stick wrong end having.

Yoda's version of the English translation of an Indian colloquialism??....;)

catkin 08-15-2009 08:42 AM

Quote:

Originally Posted by pixellany (Post 3644339)
Yoda's version of the English translation of an Indian colloquialism??....;)

Origins lost in history but the only suggested origin found that carries any sense of misunderstanding (as opposed to simply being disadvantaged) is in the last paragraph here about a botched Christening. Definitely from British English but surely both Lucas and the Rishis could develop it nicely. I hope this is not enough to have us banished to General :eek:

Wim Sturkenboom 08-16-2009 01:38 AM

pixellany, thanks

I think that the dime has finally fallen; it took about 10 years :o

The solution for me would have been
Code:

grep hello < `find . -name abc*`
hello


::: 08-16-2009 03:41 AM

xargs
 
probably the easiest would be using "xargs". xargs executes a command on every file in a list of file read from the standard input, thus input can be piped to it.
Code:

find /dir -name example.txt | xargs ls -l
this command looks for a file named "examples.txt" in the directory "/dir" and all its subdirectories recursivly. if it finds a file "examples.txt" it executes the command "ls -l" on it.


All times are GMT -5. The time now is 08:22 PM.