I need to move all the videos files in a file tree to their parent folders, for example:

origin:

A--sub1--file1
         file2
   sub2--file3
       --file4
   sub3--file5
B--sub1--file1
         file2
   sub2--file3
       --file4
   sub3--file5
   ...

what I need:

A--file1
   file2
   file3
   file4
   file5
B--file1
   file2
   file3
   file4
   file5
   ...

I am trying to use shell script, and what I am up to is:

for file in `find -name *avi`; do something ; done

I would like to use the file name I get from find -name *avi to get its parent folder's path, which is the part before the second last '/'. But I don't know how to achieve it using shell script, regex may help but I have totally no idea how, any one help?

link|improve this question

feedback

1 Answer

up vote 0 down vote accepted
find -name "*.avi" |
while read -r file; do
    mv -v "$file" "${file%/*/*}"
done
link|improve this answer
thanks a lot, exactly what I need, and now I understand better about the string operation "%" – zhanwu Sep 5 '11 at 10:32
wiki.bash-hackers.org/start – grawity Sep 5 '11 at 10:47
thx for link as well – zhanwu Sep 5 '11 at 10:54
feedback

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.