0

after using find I need to iterate the files

var=`find -name "reg"`
#replace ./ for newline
for file in $var; do 
    #something
done

edit: SOLVED with ${string#*/} it takes away the ./ I can survive with that I think

matias
  • 105
  • 1
  • 10
  • Much easier: `find -name "reg" -exec \;`. Within "your code", you refer to the current findee as `{}`. Even better is to use `find -name "reg" -print | xargs `, if your command supports that style. – Kerrek SB Aug 03 '11 at 16:45
  • 2
    (1.) `-execdir` is safer than `-exec`. (2.) `find ... -print0 | xargs -0 ... ` is safer than `-print ...` – jw013 Aug 03 '11 at 17:04

4 Answers4

2

EDIT:

I am not concerned about how you get the $file, using command line or whatever suits you. I am much more concerned about the #do something part of your question, which I believe is the main question and all those who are posting various find -name 'reg' | xargs ... should think twice before complicating the matters for OP.


use sed
var=`find -name "reg"`
#replace ./ for newline
for file in $var; do 
    sed -i 's|\./|\n|g' $file
done

remove the -i options to get output on the screen, if satisfied output is obtained use -i to actually change the line.
On second read, maybe I am replacing the other way round, perhaps you want to replace newline with ./ ? Its bit complicated

sed -i ':a;N;$!ba;s|\n|./|g' $file

as always, test without -i option and then decide if you want to modify the file.
For explanation of second sed magic, read this SO . Its exactly same, except for the ./ character for replace string.

Community
  • 1
  • 1
wadkar
  • 960
  • 2
  • 15
  • 29
0
find -name "reg" | while read file; do ...; done
jfg956
  • 16,077
  • 4
  • 26
  • 34
0
while IFS= read -r line; do
  var="${line#./}"
  echo "$var"
done < <(find . -name reg -print)
glenn jackman
  • 238,783
  • 38
  • 220
  • 352
0

ok I finally solved it guys, i could delete the ./
string=./blabla/bla

string=${string#*/} #delete stuff until the first / included

echo $string #got blabla/bla

matias
  • 105
  • 1
  • 10
  • and what if your string contains `blah./blabla/bla` ? you will lose the first 'blah.' , and it removes the substring, NOT replace it, I think you did NOT explained your question properly – wadkar Aug 06 '11 at 18:48
  • it will never start with something before ./ find returns ./some/file ./some/other/file – matias Aug 07 '11 at 02:17