linux - Shell: How to a file's specified parent directory's name? -


i beginner shell programming. writing script manipulate found files. needing parent directories' names found files. example,

search_path=/home/test file in `find $search_path -name "pattern"`;         echo $file; done 

in search path there several folders have file pattern,

/home/test/type1/log/pattern /home/test/type2/log/pattern /home/test/type3/log/pattern 

what need find 'pattern' in files, , got grandparent directory's name the 'type' name ...

dirty , quick:

kent$  dirname $(dirname "/home/test/type1/log/pattern") /home/test/type1 

if there no / in filename, sed, awk cut.... 1 sed example:

kent$  echo "/home/test/type1/log/pattern"|sed 's#/[^/]*/[^/]*$##'                                                                                                           /home/test/type1 

edit

type only:

basename & dirname:

kent$  basename $(dirname $(dirname "/home/test/type1/log/pattern"))                                                                                                         type1 

with awk:

kent$  echo "/home/test/type1/log/pattern"|awk -f'/' '$0=$(nf-2)'     type1 

Comments

Popular posts from this blog

blackberry 10 - how to add multiple markers on the google map just by url? -

php - guestbook returning database data to flash -

delphi - Dynamic file type icon -