Neeraj
Neeraj

Reputation: 9155

linux find command output traversing

I need to create a shell script that will simply find files (e.g. *.jpg) under particular source (e.g. /var/www/html/folder1/source/) and have to make some operations with the output returned by the Find command. Below is the command i have written in my script

outputvar = find /var/www/html/folder1/source/ -name \*.jpg

How can i make a traverse operation on a variable that store the output of the find command?

Upvotes: 1

Views: 1994

Answers (2)

You may want to put the output of your find command in a file, e.g.

find /var/www/html/folder1/source/ -name \*.jpg > /tmp/find.out

You could also put that output in a shell variable, e.g.

outfindvar=$(find /var/www/html/folder1/source/ -name \*.jpg)

and then you could iterate on them

for jpgfile in $outfindvar; do 
   ## do something with $jpgfile
done

If you could have files with spaces in their name, be careful. In that case, consider using the -print0 action of find; or perhaps use an auxiliary script for the -exec action.

You really should read the Advanced Bash Scripting Guide

You may want to start your script with

#!/bin/bash -vx

while debugging it, and remove the -vx once your script is working well.

Upvotes: 4

ezod
ezod

Reputation: 7411

How about piping the output to whatever else it is you want to do with it?

find /var/www/html/folder1/source/ -name \*.jpg | other_command

Upvotes: 2

Related Questions