LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (http://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   piping output of find to another command or loop (http://www.linuxquestions.org/questions/linux-newbie-8/piping-output-of-find-to-another-command-or-loop-735377/)

steven19782007 06-24-2009 04:19 PM

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.

colucix 06-24-2009 04:40 PM

The syntax looks correct. Can you show us the output of the find command by itself?

ArfaSmif 06-24-2009 05:30 PM

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

find /media/backups/rsnapshots/ -name "CTG\.fp5" -exec ls -l '{}' \;

steven19782007 06-24-2009 07:14 PM

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.

chrism01 06-24-2009 07:44 PM

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

Tinkster 06-24-2009 08:35 PM

Quote:

Originally Posted by chrism01 (Post 3585317)
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

ArfaSmif 06-25-2009 05:29 PM

Quote:

Originally Posted by steven19782007 (Post 3585296)
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''.


All times are GMT -5. The time now is 10:09 PM.