Bash - find recursively in many directories -
i have 2 or more directories path stored in variable - output of find command:
folders="$(find /g -type d -name "jpgtest*")"
note: directory names may have spaces.
assuming there 2 directories: g/jpgtest1 , g/jpgtest2.
how search subdirectories of 2 files of form "*.a",
and remove files in form "*.b" * means: name starts same name of files extension a.
for example: found: g/jpgtest1/test1/j.a
remove: g/jpgtest1/test1/j1.b , don't remove g/jpgtest1/test1/f1.b
and on 2 directories.
a possible solution:
shopt -s globstar nullglob f in $folders/**/*.a ; rm -f "${f%.a}"*.b done
but works 1 directory found in "folders", should change work several directories well.
edit:
any solution when it's in bash script , content of "folders" unknown , , result finding folders older 1 month:
folders="$(find /g -maxdepth 1 -type d -atime +30)"
your problem following: suppose find jpgtest1
, jpgtest2
. expression $folders/**/*.a
yields:
/g/jpgtest1 /g/jpgtest2/**/*.a
which expanded using glob, finding *.a
files under jpgtest2
. try this:
for f in /g/**/jpgtest*/**/*.a ;
if intend use output of find
input, can double for
reason:
for folder in $folders; f in $folder/**/*.a ; rm -f "${f%.a}"*.b done done
the drawback of breaks if folder has whitespace in it. solution read line-by-line (or use ifs
, i'm showing line-by-line solution):
while read folder; f in "$folder"/**/*.a ; rm -f "${f%.a}"*.b done done < <(find /g -maxdepth 1 -type d -atime +30)
Comments
Post a Comment