LinuxQuestions.org
Visit Jeremy's Blog.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 08-14-2009, 10:58 AM   #1
cipher7836
Member
 
Registered: Dec 2008
Posts: 35

Rep: Reputation: 15
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?
 
Old 08-14-2009, 11:02 AM   #2
paulsm4
Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Here's one option:
Quote:
find . -name MYFILE -exec ls -l {} \;
 
Old 08-14-2009, 12:26 PM   #3
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.
 
Old 08-14-2009, 01:05 PM   #4
jschiwal
Guru
 
Registered: Aug 2001
Location: Fargo, ND
Distribution: SuSE AMD64
Posts: 15,733

Rep: Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654Reputation: 654
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
 
Old 08-14-2009, 11:48 PM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
Quote:
Originally Posted by pixellany View Post
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?
 
Old 08-15-2009, 02:02 AM   #6
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by Wim Sturkenboom View Post
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?
 
Old 08-15-2009, 02:13 AM   #7
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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......
 
Old 08-15-2009, 05:21 AM   #8
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
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
 
Old 08-15-2009, 06:53 AM   #9
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by Wim Sturkenboom View Post
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.
 
Old 08-15-2009, 08:17 AM   #10
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
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.

Last edited by pixellany; 08-15-2009 at 08:21 AM.
 
Old 08-15-2009, 08:20 AM   #11
pixellany
LQ Veteran
 
Registered: Nov 2005
Location: Annapolis, MD
Distribution: Arch/XFCE
Posts: 17,802

Rep: Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728Reputation: 728
Quote:
Originally Posted by catkin View Post
unless I am the stick wrong end having.
Yoda's version of the English translation of an Indian colloquialism??....
 
Old 08-15-2009, 08:42 AM   #12
catkin
LQ 5k Club
 
Registered: Dec 2008
Location: Tamil Nadu, India
Distribution: Servers: Debian Squeeze and Wheezy. Desktop: Slackware64 14.0. Netbook: Slackware 13.37
Posts: 8,528
Blog Entries: 27

Rep: Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176Reputation: 1176
Quote:
Originally Posted by pixellany View Post
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
 
Old 08-16-2009, 01:38 AM   #13
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Slackware 10.1/10.2/12, Ubuntu 12.04, Crunchbang Statler
Posts: 3,786

Rep: Reputation: 282Reputation: 282Reputation: 282
pixellany, thanks

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

The solution for me would have been
Code:
grep hello < `find . -name abc*`
hello
 
Old 08-16-2009, 03:41 AM   #14
:::
Member
 
Registered: Aug 2009
Distribution: slackware 12.2
Posts: 51

Rep: Reputation: 17
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.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
piping output of find to another command or loop steven19782007 Linux - Newbie 6 06-25-2009 05:29 PM
piping output of find to vim babu198649 Linux - Newbie 4 07-24-2008 06:25 AM
Find command, eliminating unwanted output swamprat Linux - Newbie 7 04-13-2008 06:28 PM
find with -exec: no command output David the H. Linux - Software 5 01-03-2008 01:53 AM
Piping output between processes. C. banan Programming 1 11-12-2006 11:25 PM


All times are GMT -5. The time now is 05:42 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration