Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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?

share|improve this question

1 Answer 1

up vote 1 down vote accepted
find -name "*.avi" |
while read -r file; do
    mv -v "$file" "${file%/*/*}"
done
share|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

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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