LinuxQuestions.org
View the Most Wanted LQ Wiki articles.
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 06-24-2009, 04:19 PM   #1
steven19782007
LQ Newbie
 
Registered: Jan 2007
Location: Merseyside
Distribution: Ubuntu 9.04
Posts: 15

Rep: Reputation: 1
piping output of find to another command or loop


Hi

I am trying to run a find command on a directory and then pipe the output to either awk or into a loop for further processing. My find command works fine and outputs line of results, however, when I try to pipe the output to another command or loop, nothing happens, it just freezes. The command I am currently trying is:

find /media/backups/rsnapshots/ -name "CTG\.fp5" | while read i; do ls -l $i; done

I cannot understand what is stopping this from working as the output from the find on its own is simply lines of output, I have done plenty of googling and cannot `find` the answer. Maybe I'm just being a bit dim. Any help would be greatly appreciated.
 
Old 06-24-2009, 04:40 PM   #2
colucix
Moderator
 
Registered: Sep 2003
Location: Bologna
Distribution: CentOS 6.5 OpenSuSE 12.3
Posts: 10,458

Rep: Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941Reputation: 1941
The syntax looks correct. Can you show us the output of the find command by itself?
 
Old 06-24-2009, 05:30 PM   #3
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: always back to Fedora
Posts: 241

Rep: Reputation: 50
The syntax looks ok. You could try this instead :-

find /media/backups/rsnapshots/ -name "CTG\.fp5" -exec ls -l '{}' \;
 
Old 06-24-2009, 07:14 PM   #4
steven19782007
LQ Newbie
 
Registered: Jan 2007
Location: Merseyside
Distribution: Ubuntu 9.04
Posts: 15

Original Poster
Rep: Reputation: 1
output

the output of the find command I was using was this:

/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Wednesday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Friday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Tuesday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Sunday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Thursday/CTG.fp5

Arfa that works great, thanks. What does the " '{}' \ " bit do?

And I am still confused about why my find command won't pipe to a loop or awk.
 
Old 06-24-2009, 07:44 PM   #5
chrism01
Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.5, Centos 5.10
Posts: 16,226

Rep: Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023Reputation: 2023
You could use a for loop, especially if you want to do more complex stuff than just 'ls'
Code:
for file in `find media/backups/rsnapshots/ -name 'CTG.fp5' -type f`
do
    ls $file
done
NB the single quote stops shell interpolation, so you don't need to escape '.'

Here's a good page on the find cmd http://lowfatlinux.com/linux-find-files.html
 
Old 06-24-2009, 08:35 PM   #6
Tinkster
Moderator
 
Registered: Apr 2002
Location: in a fallen world
Distribution: slackware by choice, others too :} ... android.
Posts: 22,965
Blog Entries: 11

Rep: Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865Reputation: 865
Quote:
Originally Posted by chrism01 View Post
You could use a for loop, especially if you want to do more complex stuff than just 'ls'
Code:
for file in `find media/backups/rsnapshots/ -name 'CTG.fp5' -type f`
do
    ls $file
done
NB the single quote stops shell interpolation, so you don't need to escape '.'

Here's a good page on the find cmd http://lowfatlinux.com/linux-find-files.html
Not that . has any special meaning in an argument to
name for find or in shell-globbing if it's not in the first
position of the path, anyway.



Cheers,
Tink
 
Old 06-25-2009, 05:29 PM   #7
ArfaSmif
Member
 
Registered: Oct 2008
Location: Brisbane Australia
Distribution: always back to Fedora
Posts: 241

Rep: Reputation: 50
Quote:
Originally Posted by steven19782007 View Post
the output of the find command I was using was this:

/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Wednesday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Friday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Tuesday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Sunday/CTG.fp5
/media/backups/rsnapshots/weekly.1/mnt/FM_daily_backups/Thursday/CTG.fp5

Arfa that works great, thanks. What does the " '{}' \ " bit do?

And I am still confused about why my find command won't pipe to a loop or awk.
Hi Steven,

The '{}' puts the output of the find command in its place. If you do a "man find" you will see many options for the find command. There are heaps of great things you can do with the find command.

When I run your command on my system (Fedora 11 - find (GNU findutils)4.4.0)I get the following "error" :-

$ find . -name "song-name\.ogg"

find: warning: Unix filenames usually don't contain slashes (though pathnames do). That means that '-name `song-name\.ogg'' will probably evaluate to false all the time on this system. You might find the '-wholename' test more useful, or perhaps '-samefile'. Alternatively, if you are using GNU grep, you could use 'find ... -print0 | grep -FzZ `song-name\.ogg''.
 
1 members found this post helpful.
  


Reply

Tags
awk, find, pipe


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
LXer: Using Bash To Feed Command Output To A While Loop Without Using Pipes! LXer Syndicated Linux News 0 08-06-2008 12:10 PM
piping output of find to vim babu198649 Linux - Newbie 4 07-24-2008 06:25 AM
Piping into a Case Statement embedded in a while loop telecom_is_me Programming 5 07-02-2008 04:45 PM
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


All times are GMT -5. The time now is 11:09 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